mybatis学习及上传git


database下的pom.xml添加

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.9</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.7</version>
</dependency>
###########################################
<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

项目细览

中间可能的错误

1、Mapper.xml文件并没有在target/classes/com/spring/www/DAO下

那就是在第一步中的**##后边的内容没有粘贴,导致找不到配置文件**

2、在Mapper.xml文件中mapper的namespace位置并没有到位

mapper namespace="com.spring.www.DAO.BookDAO">

3、在有返回值的情况下要使用

<select id="queryAll" resultType="Book">

运行结果

动态sql的生成

if

<select id="queryAllByIf" resultType="Book" parameterType="String">
	select * from book where true
    <if test="Bname!=null and Bname!=''">
    	and Bname like concat('%',#{name},'%')
    </if>
</select>

如果满足if条件那就将if后边sql语句进行拼接上去生成的sql语句是

Preparing: select * from book where true and Bname like concat('%',?,'%')

注意where之后要要使用一个真值

foreach

适用于传入的参数是一个列表,用于多个值的筛选

<select id="querryAllByforeach" parameterType="List" resultType="Book">
	select * from book where Bisdn in
    <foreach item="item" index="index" collection="list" open="(" separator=","close=")">
    	 #{item}
    </foreach>
</select>

拼接的sql语句是

==> Preparing: select * from book where Bisdn in ( ? , ? )
==> Parameters: 001(String), 002(String)

bind元素

进行模糊查询

<select id="querryAllByBind" parameterType="Book" resultType="Book">
	<bind name="paran_name" value="'%'+Bname+'%'"/>
    	select * from book where Bname like #{paran_name}
</select>

拼接的sql语句是

select * from book where Bname like ?

下面就是代码展示

完整展示

链接:https://pan.baidu.com/s/1ReDMcpGz6WcMM0hq5nxK0Q
提取码:qwer

事务处理

database-context.xml

<!--配置事务管理器-->
<bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
      p:dataSource-ref="dataSource"/>

在SaleService中的OnSale前添加注释@Transactional

添加测试类

TransactionTest

import com.spring.www.Business.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.annotations.Test;

@ContextConfiguration("classpath*:database-context.xml")
public class TransactionTest extends AbstractTransactionalTestNGSpringContextTests {
    @Autowired
    SaleService service;
    @Test
    public void transTest(){
        service.onSale(0.5f);
    }
}

运行结果

这样是不能改变数据库中的值的。这样主要是为了避免”脏数据“影响其他业务

要是就是想看运行的结果,那就要在测试类中添加@Rollback(value = false),这样就能直观的看到测试的结果了

提交远端仓库

创建公开的代码仓库

创建远端仓库

添加git

在上传项目的问价夹下

bush
git init git remote add origin 远端仓库地址

添加git

提交目录

提交目录

推送

上传成功


文章作者: 毛豆不逗比
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 毛豆不逗比 !
  目录
{% include '_third-party/exturl.swig' %} {% include '_third-party/bookmark.swig' %} {% include '_third-party/copy-code.swig' %} + {% include '_custom/custom.swig' %}