【SpringBoot系列05】SpringBoot之整合Mybatis

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xp541130126/article/details/81670418

前言

上一篇博客中,我们完成了springboot 使用spring data jpa但是在我们实际工作中,可能大部分的同学还是使用mybatis比较多,所以今天我们在这里实现一下springboot使用mybatis实现对user表的增删改查并且进行单元测试

实现

1、添加mybaits依赖

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
   </dependencies>

并且在yml中配置一下数据源跟mybatis的配置。

server:
  port: 8989
mybatis:
  mapper-locations: classpath:/mybaits/mapper/*.xml
  config-location:  classpath:/mybatis/config/mybatis-config.xml
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
    driver-class-name: com.mysql.jdbc.Driver

在上面的配置中

mybatis:
  mapper-locations: classpath:/mybaits/mapper/*.xml
  config-location:  classpath:/mybatis/config/mybatis-config.xml
  • mapper-locations 是配置我们mapper.xml文件的位置,我们把它配置在/src/main/resource/mybatis/mapper目录下。
  • config-locations 是配置mybatis-confg.xml文件的位置。我们把它配置在/src/main/resource/mybatis/config目录下。

接下来我们在resource目录下新建mybatis/mapper目录跟mybatis/config目录,并且在config目录下新建mybatis-config.xml
具体代码如下

<?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="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.yukong.chapter4.entity"/>
    </typeAliases>
</configuration>

主要是配置了一下开启mybatis的驼峰命名转换跟entity的别名。
接下来就是建表写实体类。
1、 建表

如果有读过上一篇【SpringBoot系列04】SpringBoot之使用JPA完成简单的rest api并且实际操作过的同学就不用建表了,可以跳过建表这一步,没有的同学需要新建一个test数据库并且新建user

create table test.user
(
    id bigint not null AUTO_INCREMENT comment '主键' primary key,
    age int null comment '年龄',
    password varchar(32) null comment '密码',
    sex int null comment '性别',
    username varchar(32) null comment '用户名'
);

新建entity包 并且编写对应的实体类 User.java

public class User {


    private Long id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 性别 1=男 2=女 其他=保密
     */
    private Integer sex;


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

3、编写mapper接口
接下里我们新建repository包 新建UserMapper.java接口 并且定义增删改查的方法声明

public interface UserMapper {

    /**
     * 新增用户
     * @param user
     * @return
     */
    int save (User user);

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    int update (User user);

    /**
     * 根据id删除
     * @param id
     * @return
     */
    int deleteById (int id);

    /**
     * 根据id查询
     * @param id
     * @return
     */
    User selectById (int id);

    /**
     * 查询所有用户信息
     * @return
     */
    List<User> selectAll ();
}

跟我们之前定义的mapper.xml文件的位置,我们在resource/mybatis/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.yukong.chapter4.repository.UserMapper">

    <resultMap id="SysUserResultMap" type="User">
        <id property="id" column="id" javaType="java.lang.Long" jdbcType="BIGINT"/>
        <result property="username" column="USERNAME" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result property="password" column="PASSWORD" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result property="sex" column="SEX" javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result property="age" column="AGE" javaType="java.lang.Integer" jdbcType="INTEGER"/>
    </resultMap>
    <delete id="deleteById">
        delete from user where id=#{id}
    </delete>

    <select id="selectAll"  resultMap="SysUserResultMap">
        select * from user
    </select>


    <insert id="save" parameterType="User" >
        insert into user
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="id != null" >
                id,
            </if>
            <if test="username != null" >
                username,
            </if>
            <if test="password != null" >
                password,
            </if>

            <if test="sex != null" >
                sex,
            </if>
            <if test="age != null" >
                age,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="id != null" >
                #{id,jdbcType=BIGINT},
            </if>
            <if test="username != null" >
                #{username,jdbcType=VARCHAR},
            </if>
            <if test="password != null" >
                #{password,jdbcType=VARCHAR},
            </if>
            <if test="sex != null" >
                #{sex,jdbcType=INTEGER},
            </if>
            <if test="age != null" >
                #{age,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>


    <update id="update" parameterType="User" >
        update user
        <set >
            <if test="username != null" >
                username = #{username,jdbcType=VARCHAR},
            </if>
            <if test="password != null" >
                password = #{password,jdbcType=VARCHAR},
            </if>
            <if test="sex != null" >
                sex = #{sex,jdbcType=INTEGER},
            </if>
            <if test="age != null" >
                sex = #{age,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>


    <select id="selectById" resultMap="SysUserResultMap">
        select
        *
        from user
        where id = #{id,jdbcType=BIGINT}
    </select>
</mapper>

这时候mybatis的配置还不算成功,我们需要把mapper的路径暴露给spring 让它来扫描管理,所以我们需要在Chapter4Application.java文件上加上注解
至此。我们mybaits的接口算是编写完毕,接下来我们来测试一下把。
4、测试
这次我们用spring boot的单元测试来测试我们的接口。
idea中 使用快捷键新建测试类ctrl+shift+t
image.png
勾选需要测试的方法,并且点击ok
这时候会在src/test/java目录下对应的包路径新建对应的测试文件
代码如下

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class UserMapperTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void save() {
        User user = new User();
        user.setUsername("zzzz");
        user.setPassword("bbbb");
        user.setSex(1);
        user.setAge(18);
        // 返回插入的记录数 ,期望是1条 如果实际不是一条则抛出异常
        Assert.assertEquals(1,userMapper.save(user));
    }

    @Test
    public void update() {
        User user = new User();
        user.setId(1L);
        user.setPassword("newpassword");
        // 返回更新的记录数 ,期望是1条 如果实际不是一条则抛出异常
        Assert.assertEquals(1,userMapper.update(user));
    }

    @Test
    public void selectById() {
        Assert.assertNotNull(userMapper.selectById(1L));
    }

    @Test
    public void deleteById() {
        Assert.assertEquals(1,userMapper.deleteById(1L));
    }
}

使用spring boot测试需要在测试类上解释注解
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)

另外我们这里使用Assert断言来判断实际结果跟预期结果是否一致,如果不一致则抛出异常。
image.png

右键类旁边的运行按钮,一次测试所有的方法

结果如下
image.png
所有的方法通过测试,springboot于mybatis整合成功。

猜你喜欢

转载自blog.csdn.net/xp541130126/article/details/81670418