SpringBoot之Mybatis

这里使用SpringBoot与Mybatis整合,并通过简单例子同时使用Mybatis的注解版和配置版

第一步:创建工程

使用IDEA的spring initializr创建工程,选中web、mysql、jdbc、mybatis模块
这里写图片描述

第二步:配置文件

application.yml

spring:
  datasource:
    username: root
    password: Root!!2018
    url: jdbc:mysql://192.168.3.18/springboot_mybatis?autoReconnect=true&useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
# 加载mybatis配置文件
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

注解版

对Department的操作使用注解版

DepartmentMapper.java

@Mapper
public interface DepartmentMapper {

    @Select("select * from department where id=#{id}")
    Department getDepartmentById(Integer id);

    @Options(useGeneratedKeys = true, keyColumn = "id")
    @Insert("insert into department(departmentName) values(#{departmentName})")
    int insertDepartment(Department department);

    @Delete("delete from department where id=#{id}")
    int deleteDepartment(Integer id);

    @Update("update department set departmentName=#{departmentName} where id=#{id}")
    int updateDepartment(Department department);
}

DepartmentController.java

@RestController
public class DepartmentController {

    @Autowired
    private DepartmentMapper departmentMapper;

    @GetMapping("/department/{id}")
    public Department getDepartment(@PathVariable("id") Integer id){
        return departmentMapper.getDepartmentById(id);
    }

    @PostMapping("/department")
    public Department insertDepartment(Department department){
        departmentMapper.insertDepartment(department);
        return department;
    }

    @DeleteMapping("/department/{id}")
    public Map<String, Object> deleteDepartment(@PathVariable("id") Integer id){
        departmentMapper.deleteDepartment(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "OK");
        return map;
    }

    @PutMapping("/department")
    public Department updateDepartment(Department department){
        departmentMapper.updateDepartment(department);
        return department;
    }
}

配置版

对Employee的操作使用配置版

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>
</configuration>

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="cn.yujiago.springboot.mapper.EmployeeMapper">

    <select id="getEmployeeById" resultType="cn.yujiago.springboot.pojo.Employee">
        select * from employee where id = #{id}
    </select>

    <insert id="insertEmployee">
        insert into employee(lastName, email, gender, d_id) values(#{lastName}, #{email}, #{gender}, #{dId})
    </insert>
</mapper>

EmployeeController.java

@RestController
public class EmployeeController {

    @Autowired
    private EmployeeMapper employeeMapper;

    @GetMapping("/employee/{id}")
    public Employee getEmployeeById(@PathVariable("id") Integer id){
        return employeeMapper.getEmployeeById(id);
    }

    @PostMapping("/employee")
    public int insertEmployee(Employee employee){
        return employeeMapper.insertEmployee(employee);
    }

}

猜你喜欢

转载自blog.csdn.net/Code_shadow/article/details/81253387
今日推荐