springboot2.x basic tutorial: integrated mybatis best practice

The previous article introduced SpringBoot combined with Jpa to implement database operations. Today, I will introduce the relevant knowledge points of SprigBoot integrated Mybatis.
As a semi-automated ORM framework, Mybatis dynamically splices SQL based on conditions, which is a major advantage. It conforms to the native SQL writing method, which is convenient for developers to write complex SQL statements flexibly.
The configuration of SpringBoot integrated Mybatis is quite simple, and the tutorial will give common writing methods for Mysql data CURD, paging, and batch operations.

SpringBoot configuration Mybatis

Introduce dependencies

<!--mybatis依赖-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>
<!--分页依赖-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

Configure data source

Here we use springboot's default data connection pool hikari, known as the fastest database connection pool, and allowMultiQueries=true to enable batch updates.

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/spring?autoReconnect=true&useSSL=false&characterEncoding=utf-8&failOverReadOnly=false&allowMultiQueries=true
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    hikari:
      maximum-pool-size: 50
      connection-test-query: SELECT 1
      #1分钟后释放
      idle-timeout: 600000
      minimum-idle: 10
      auto-commit: true
      validation-timeout: 250

Configure Mybatis parameters

There are two configurations here, one is configured directly in springboot's default application.yml, the other is extracted separately, and the second is used in this article.

mybatis:
  #指定配置mybatis参数配置地址
  config-location: classpath:mybatis-config.xml
  #指定接口xml地址
  mapper-locations: classpath:mappers/*.xml

The configuration of mybatis-config.xml is posted here:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 全局参数 -->
    <settings>
        <!-- 使全局的映射器启用或禁用缓存。 -->
        <setting name="cacheEnabled" value="true"/>
        <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
        <setting name="aggressiveLazyLoading" value="true"/>
        <!-- 是否允许单条sql 返回多个数据集  (取决于驱动的兼容性) default:true -->
        <setting name="multipleResultSetsEnabled" value="true"/>
        <!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true -->
        <setting name="useColumnLabel" value="true"/>
        <!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。  default:false  -->
        <setting name="useGeneratedKeys" value="true"/>
        <!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分  FULL:全部  -->
        <setting name="autoMappingBehavior" value="PARTIAL"/>
        <!-- 这是默认的执行类型  (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新)  -->
        <!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE -->
        <setting name="defaultExecutorType" value="SIMPLE"/>
        <!-- 数据库超过25000秒仍未响应则超时 -->
        <setting name="defaultStatementTimeout" value="25000"/>
        <!-- 使用驼峰命名法转换字段。 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- 设置本地缓存范围 session:就会有数据的共享  statement:语句范围 (这样就不会有数据的共享 ) defalut:session -->
        <setting name="localCacheScope" value="SESSION"/>
        <!-- 设置但JDBC类型为空时,某些驱动程序 要指定值,default:OTHER,插入空值时不需要指定类型 -->
        <setting name="jdbcTypeForNull" value="NULL"/>
        <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
        <setting name="aggressiveLazyLoading" value="false"/>
        <setting name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"/>
    </settings>
    <typeAliases>
        <!--批量别名,默认为别名的规范就是对应包装类的类名首字母变为小写-->
        <package name="vip.codehome.springboot.tutorials.entity"/>
    </typeAliases>
    <plugins>
        <!--pagehelper分页插件-->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 使用MySQL方言的分页 -->
            <property name="helperDialect" value="sqlserver"/><!--如果使用mysql,这里value为mysql-->
            <property name="pageSizeZero" value="true"/>
        </plugin>
    </plugins>
</configuration>

Add, delete, modify, and check the best wording

For this table, give the common dynamic SQL writing of Mybatis. First edit the Mapper layer interface, and pay attention to the @Mapper annotation.

@Mapper
@Repository
public interface UserMapper {
	//插入
    int insert(UserDO userDO);
	//分页查询
    List<UserDO> select(UserDO userDO);
	//更新
    int update(UserDO userDO);
	//删除
    int delete(UserDO userDO);
	//批量插入
    int insertBatch(List<UserDO> list);
	//批量更新
    int updateBatch(List<UserDO> list);
	//批量删除
    int deleteBatch(Long[] array);
}

The mapper-locations is configured as classpath:mappers/*.xml, that is, the xml file corresponding to the mapper is placed in the src/main/resources/mappers directory,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="vip.codehome.springboot.tutorials.mapper.UserMapper">
    <sql id="base">
        id,name,account,age,forbidden,login_time,passwd
    </sql>
    <insert id="insert" parameterType="userDO">
        insert into tb_user(
        <trim suffixOverrides=",">
            id,
            account,
            passwd,
            name,
            <if test="age!=null">
                age,
            </if>
            <if test="forbidden!=null and forbidden!=''">
                forbidden,
            </if>
            <if test="loginTime!=null">
                login_time,
            </if>
        </trim>
        )values (
        <trim suffixOverrides=",">
            #{id},
            #{account},
            #{passwd},
            #{name},
            <if test="age!=null">
                #{age},
            </if>
            <if test="forbidden!=null and forbidden!=''">
                #{forbidden},
            </if>
            <if test="loginTime!=null">
                #{loginTime},
            </if>
        </trim>
        )
    </insert>
    <select id="select" resultType="userDO">
        select
        <include refid="base"/>
        from tb_user
        <where>
            <if test="account!=null and account!=''">
                and account=#{account}
            </if>
            <if test="name!=null and name!=''">
                and name like concat('%',#{name},'%')
            </if>
        </where>
    </select>
    <update id="update" parameterType="userDO">
        update tb_user
        <set>
            <if test="name!=null and name!=''">
                name=#{name},
            </if>
            <if test="age!=null">
                age=#{age}
            </if>
        </set>
        where id=#{id}
    </update>

    <delete id="delete" parameterType="userDO">
        delete from tb_user where id=#{id}
    </delete>
    <insert id="insertBatch" parameterType="list" useGeneratedKeys="true">
        <selectKey resultType="long" keyProperty="id" order="AFTER">
            SELECT
            LAST_INSERT_ID()
        </selectKey>
        insert into tb_user(
        account,passwd,name,age,forbidden,login_time
        )values
        <foreach collection="list" item="user" index="index" separator=",">
            (#{user.account},
            #{user.passwd},
            #{user.name},
            #{user.age},
            #{user.forbidden},
            #{user.loginTime})
        </foreach>

    </insert>
    <update id="updateBatch" parameterType="list">
        <foreach collection="list" item="user" index="index" open="" close="" separator=";">
            update tb_user
            <set>
                <if test="name!=null and name!=''">
                    name=#{name},
                </if>
                <if test="age!=null">
                    age=#{age}
                </if>
            </set>
            where id=#{id}
        </foreach>
    </update>
    <delete id="deleteBatch" parameterType="long">
        delete from tb_user
        where id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>

Unit test case

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
    @Autowired
    UserMapper userMapper;
    //插入
    @Test
    public void testAdd(){
        UserDO userDO=new UserDO();
        userDO.setPasswd("codehome");
        userDO.setAccount("codehome");
        userDO.setName("name");
        userDO.setForbidden(true);
        userDO.setLoginTime(LocalDateTime.now());
        userMapper.insert(userDO);
    }
    //分页
    @Test
    public void page(){
        PageHelper.startPage(0,10);
        UserDO userDO=new UserDO();
        userDO.setName("n");
       PageInfo<UserDO> userDOPage= new PageInfo<>(userMapper.select(userDO));
    }
    //更新
    @Test
    public void update(){
        UserDO userDO=new UserDO();
        userDO.setId(1L);
        userDO.setName("编程之家");
        userMapper.update(userDO);
    }
    //删除
    @Test
    public void delete(){
        UserDO userDO=new UserDO();
        userDO.setId(1L);
        userMapper.delete(userDO);
    }
    //批量插入
    @Test
    public void testAddBatch(){
        UserDO userDO=new UserDO();
        userDO.setPasswd("codehome");
        userDO.setAccount("codehome");
        userDO.setName("name");
        userDO.setForbidden(true);
        userDO.setLoginTime(LocalDateTime.now());
        UserDO userDO1=new UserDO();
        userDO1.setPasswd("codehome");
        userDO1.setAccount("codehome");
        userDO1.setName("name");
        userDO1.setForbidden(true);
        userDO1.setLoginTime(LocalDateTime.now());
        userMapper.insertBatch(Arrays.asList(userDO,userDO1));
    }
    //批量更新
    @Test
    public void testUpdateBatch(){
        UserDO userDO=new UserDO();
        userDO.setPasswd("codehome1");
        userDO.setAccount("codehome1");
        userDO.setName("name1");
        userDO.setId(1L);
        userDO.setForbidden(true);
        userDO.setLoginTime(LocalDateTime.now());
        UserDO userDO1=new UserDO();
        userDO.setId(2L);
        userDO1.setPasswd("codehome2");
        userDO1.setAccount("codehome2");
        userDO1.setName("name2");
        userDO1.setForbidden(true);
        userDO1.setLoginTime(LocalDateTime.now());
        userMapper.insertBatch(Arrays.asList(userDO,userDO1));
    }
    @Test
    public void deleteBatch(){
        userMapper.deleteBatch(new Long[]{1L,2L});
    }
}

The examples cited here use tags such as if, set, where, foreach, trim, sql, etc. For a more detailed explanation, please refer to the official dynamic SQL. The
above is the configuration of springboot integrated Mybatis, and common CURD and batch operations, and more Space is limited, you can refer to the link given in the article.
A thousand miles begins with a single step. This is the fourteenth article of the SpringBoot tutorial series. All project source codes can be downloaded from my GitHub .

Guess you like

Origin blog.csdn.net/github_35592621/article/details/108249548
Recommended