SpringBoot-mybatis

Posted swpCode

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot-mybatis相关的知识,希望对你有一定的参考价值。

springboot集成mybatis有两种方式:注解方式和xml配置方式。

项目的相关配置:

1、添加依赖

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.3.2</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>

 2、application.properties 配置数据源

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=***

 

一、注解方式

UserMapper:

@Mapper // 可以在Applicationz @MapperScan()
public interface UserMapper {

    @Select("SELECT * FROM tb_user")
    @Results({
            @Result(property = "userName", column = "user_name", jdbcType = JdbcType.VARCHAR),
            @Result(property = "userPassword", column = "user_password", jdbcType = JdbcType.VARCHAR),
            @Result(property = "userSex", column = "user_sex", jdbcType = JdbcType.CHAR),
            @Result(property = "userBirthday", column = "user_birthday", jdbcType = JdbcType.DATE)
    })
    List<User> getAllUser();

    @Select("SELECT * FROM tb_user WHERE uid = #{uid}")
    @Results({
            @Result(property = "userName", column = "user_name", jdbcType = JdbcType.VARCHAR),
            @Result(property = "userPassword", column = "user_password", jdbcType = JdbcType.VARCHAR),
            @Result(property = "userSex", column = "user_sex", jdbcType = JdbcType.CHAR),
            @Result(property = "userBirthday", column = "user_birthday", jdbcType = JdbcType.DATE)
    })
    User getUser(int uid);

    @Insert("INSERT INTO tb_user(user_name,user_password,user_sex,user_birthday) VALUES(#{userName}, #{userPassword}, #{userSex}, #{userBirthday})")
    int insertUser(User user);

    @Update("UPDATE tb_user SET user_name = #{userName} WHERE uid = #{uid}")
    int updateUser(User user);

    @Delete("DELETE FROM tb_user WHERE uid = #{uid}")
    int deleteUser(User user);
}

 测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class AnnotionTest {

    @Autowired
    private UserMapper mapper;

    @Test
    public void getAllUser(){
        List<User> users = mapper.getAllUser();
        System.out.println(users);
    }

    @Test
    public void getUser(){
        User user = mapper.getUser(3);
        System.out.println(user);
    }

    @Test
    public void insertUser(){
        User user = new User();
        user.setUserName("王杰");
        user.setUserPassword("123456546313154546");
        user.setUserSex(‘2‘);
        user.setUserBirthday(new Date());

        int result = mapper.insertUser(user);
        System.out.println(result);
    }

    @Test
    public void updateUser(){
        User user = new User();
        user.setUid(3);
        user.setUserName("刘冷");

        int result = mapper.updateUser(user);
        System.out.println(result);
    }

    @Test
    public void deleteUser(){
        User user = new User();
        user.setUid(3);

        int result = mapper.deleteUser(user);
        System.out.println(result);
    }

}

 

二、xml配置方式

application.properties 中添加配置:

mybatis.mapper-locations=classpath:/mybatis/mapper/*.xml //映射文件的位置

 Application.java中添加注解:

@MapperScan("com.swp.mybatisxml.mapper") // mapper接口的位置

 添加映射文件 UserMapper.xml:

<?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="com.swp.mybatisxml.mapper.UserMapper">

    <resultMap id="UserMap" type="com.swp.mybatisxml.model.User">
        <id property="uid" column="uid" jdbcType="INTEGER"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="userPassword" column="user_password" jdbcType="VARCHAR"/>
        <result property="userSex" column="user_sex" jdbcType="CHAR"/>
        <result property="userBirthday" column="user_birthday" jdbcType="DATE"/>
    </resultMap>

    <sql id="Base_Column">
        uid, user_name, user_password, user_sex, user_birthday
    </sql>

    <select id="getAllUser" resultMap="UserMap">
        SELECT
        <include refid="Base_Column"/>
        FROM tb_user
    </select>

    <select id="getUser" resultMap="UserMap" parameterType="java.lang.Integer">
        SELECT
        <include refid="Base_Column"/>
        FROM tb_user WHERE uid = #{uid}
    </select>

    <insert id="insertUser" parameterType="com.swp.mybatisxml.model.User">
        INSERT INTO tb_user(user_name,user_password,user_sex,user_birthday) VALUES(#{userName}, #{userPassword}, #{userSex}, #{userBirthday})
    </insert>

    <update id="updateUser" parameterType="com.swp.mybatisxml.model.User">
        UPDATE tb_user SET
        <if test="userName != null">user_name = #{userName}</if>
        WHERE uid = #{uid}
    </update>

    <delete id="deleteUser" parameterType="java.lang.Integer">
        DELETE FROM tb_user WHERE uid = #{uid}
    </delete>

</mapper>

 

以上是关于SpringBoot-mybatis的主要内容,如果未能解决你的问题,请参考以下文章

springboot-mybatis配置问题

SpringBoot-mybatis

springboot-mybatis配置(xml)/springboot-jpa配置

springBoot-mybatis+druid多数据源

springboot-mybatis多数据源以及踩坑之旅

第三方短信验证接口