Springboot整合Mybatis

Springboot整合Mybatis具体步骤如下:

1、创建Springboot项目

创建Springboot项目,添加以下依赖:
Spring Web
MyBatis Framework
MySQL Driver

如下图示:

springboot-07.pngspringboot-07.png

对应pom.xml配置如下:

<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>2.2.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>

2、新增Mapper类及配置

创建Mapper类,添加Mapper配置,如下图示:

springboot-08.pngspringboot-08.png

EmployeeMapper.java Mapper类代码如下:

/**
 * @author whw
 * @Description 员工Mapper类
 * @createTime 2021/9/28 13:59
 */
@Mapper
@Repository
public interface EmployeeMapper {

    /**
     * 查询员工列表
     *
     * @param employee
     * @return
     */
    public List<Employee> selectList(Employee employee);

    /**
     * 新增员工信息
     *
     * @param employee
     * @return
     */
    public int insert(Employee employee);

    /**
     * 更新员工信息
     *
     * @param employee
     * @return
     */
    public int update(Employee employee);

    /**
     * 删除员工信息
     *
     * @param employee
     * @return
     */
    public int delete(Employee employee);
}

EmployeeMapper.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.springboot.study.springboot_mybatis.mapper.EmployeeMapper">

    <resultMap id="EmployeeMap" type="com.springboot.study.springboot_mybatis.entity.Employee" >
        <result column="emp_no" property="empNo" javaType="String" />
        <result column="emp_name" property="empName" javaType="String" />
        <result column="hire_date" property="hireDate" javaType="Date" />
        <result column="create_time" property="createTime" javaType="Date" />
    </resultMap>

    <select id="selectList" parameterType="Employee" resultMap="EmployeeMap" >
        SELECT
            emp_no,
            emp_name,
            gender,
            age,
            hire_date,
            create_time
        FROM
            employee
        <where>
            <if test="empNo != null">
                and emp_no = #{empNo}
            </if>
            <if test="empName != null and empName != ''">
                and emp_name = #{empName}
            </if>
            <if test="gender != null">
                and gender = #{gender}
            </if>
        </where>
    </select>

    <insert id="insert" parameterType="Employee" keyColumn="emp_no" keyProperty="empNo">
        insert into employee(emp_no, emp_name, gender, age, hire_date, create_time)
        values(
        #{empNo}, #{empName}, #{gender}, #{age}, #{hireDate}, #{createTime}
        )
    </insert>

    <update id="update" parameterType="Employee" >
        update employee
        <trim prefix="set" suffixOverrides=",">
            <if test="empName != null and empName != ''">
                emp_name = #{empName},
            </if>
            <if test="gender != null">
                gender = #{gender},
            </if>
            <if test="age != null">
                age = #{age},
            </if>
            <if test="hireDate != null">
                hireDate = #{hireDate},
            </if>
        </trim>
        where emp_no = #{empNo}
    </update>

    <delete id="delete" parameterType="String" >
        delete from employee where emp_no = #{empNo}
    </delete>
</mapper>

yml配置:

mybatis:
  mapper-locations: classpath*:mapper/*Mapper.xml
  type-aliases-package: com.springboot.study.springboot_mybatis.entity

测试接口类SpringMybatisController代码如下:

/**
 * @author whw
 * @Description 接口测试类
 * @createTime 2021/9/28 14:13
 */
@RestController
@RequestMapping("/employee")
public class SpringMybatisController {

    @Autowired
    IEmployeeService employeeService;

    @RequestMapping("/list")
    public List<Employee> selectList(Employee employee) {
        List<Employee> employeeList = employeeService.selectList(employee);
        return employeeList;
    }

    @RequestMapping("/insert")
    public int insert(Employee employee) {
        return employeeService.insert(employee);
    }

    @RequestMapping("/update")
    public int update(Employee employee) {
        return employeeService.update(employee);
    }

    @RequestMapping("/delete")
    public int delete(Employee employee) {
        return employeeService.delete(employee);
    }
}

3、测试增删查改接口

启动SpringbootMybatisApplication.java启动类,测试增删查改接口。至此,Springboot整合Mybatis即完成。

(完)

最后修改于:2022年10月03日 02:45

添加新评论