當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot整合Mybatis(配置文件)
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot整合Mybatis(配置文件)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、引入jar
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.3</version> </dependency>二、創建JavaBean
public class Employee {private Integer id;private String lastName;private Integer gender;private String email;private Integer dId;public Integer getId() {return id;} ...三、創建Mapper接口
public interface EmployeeMapper {public Employee getEmpById(Integer id);public void insertEmp(Employee employee); }四、搭建XML文件配置
1)、結構:可自行定義
?2)、yml文件中指定mybatis全局配置文件和接口映射文件位置
mybatis:config-location: classpath:mybatis/mybatis-config.xmlmapper-locations: classpath:mybatis/mapper/*.xml?3)、查看官網復制主配置文件模板修改如下
? 網址:http://www.mybatis.org/mybatis-3/
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><settings><setting name="mapUnderscoreToCamelCase" value="true"/></settings> </configuration>?4)、查看官網復制接口映射文件模板修改如下
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zhq.springboot.mapper.EmployeeMapper"><!--查詢--><select id="getEmpById" resultType="com.zhq.springboot.bean.Employee">select * from employee where id=#{id}</select><!--插入--><insert id="insertEmp" >insert into employee(lastName,email,gender,d_id) values(#{lastName},#{email},#{gender},#{d_id})</insert> </mapper>五、編寫接口測試
@GetMapping("emp/{id}")public Employee getEmp(@PathVariable("id") Integer id){Employee emp = employeeMapper.getEmpById(id);return emp;}總結
以上是生活随笔為你收集整理的SpringBoot整合Mybatis(配置文件)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Note For Linux By Je
- 下一篇: 最大加权子矩阵问题