SpringBoot学习之路(X3)- 整合MyBatis
SpringBoot学习之路(X3)- 整合MyBatis
应用创建
1. 还是使用Spring initalizr创建应用
还需要选中web模块
应用创建完成,现在查看pom文件中依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<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.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2. 图表查看mybatis-spring-boot-starter引用依赖
查看mybatis-spring-boot-starter又依赖了那些:
应用配置
1)、配置druid数据源
查看上一篇章:SpringBoot学习之路(X2)- JDBC数据访问、自动配置原理、druid数据源配置
2)、创建数据库、表
这里使用本地(localhost)的student学生表
3)、创建javaBean
package com.yhhy.springboot.bean;
import java.util.Date;
public class Student {
private Integer id;
private String name;
private String pwd;
private String md5;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getMd5() {
return md5;
}
public void setMd5(String md5) {
this.md5 = md5;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
4)、创建Controller
package com.yhhy.springboot.controller;
import com.yhhy.springboot.bean.Student;
import com.yhhy.springboot.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
//这里就不写实现类了
@Autowired
StudentMapper studentMapper;
//PathVariable 取值
@GetMapping("/student/{id}")
public Student getStudent(@PathVariable("id") Integer id){
return studentMapper.getStudentById(id);
}
@GetMapping("/student")
public Student insertStudent(Student student){
studentMapper.insertStudent(student);
return student;
}
}
这里的日期使用http参数传送有点问题,之后再修改
5)、创建 Mapper
1)、注解版 - Mapper
package com.yhhy.springboot.mapper;
import com.yhhy.springboot.bean.Student;
import org.apache.ibatis.annotations.*;
@Mapper
public interface StudentMapper {
/**
* 查询一个学生
* @param id
* @return
*/
@Select("select * from student where id = #{id}")
public Student getStudentById(Integer id);
/**
* 删除
* @param id
* @return 返回受影响的行数
*/
@Delete("delete from student where id=#{id}")
public Integer delStudentById(Integer id);
/**
* 添加
* @param student
* @return
*/
//使用自动生成的主键,并指定那个属性,用于添加记录后取主键id所用
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into student(name,pwd,md5,date) values(#{name},#{pwd},#{md5},#{date})")
public Integer insertStudent(Student student);
@Update("update student set name=#{name},pwd=#{pwd},md5=#{md5},date=#{date} where id = #{id}")
public Integer updaeStudent(Student student);
}
2)、配置文件版 - Mapper
3)、扫描mapper包下文件,省略各个mapper接口上的@Mapper注解
在启动类上添加扫描:
@MapperScan(value = “com.yhhy.springboot.mapper”)
注释掉mapper上的注解@Mapper,测试结果依然可以;
6)、自定义MyBatis的配置规则
例如:
在mysql中的字段为stu_name ,而实体类中的属性为stuName,怎么才能映射上?
1)、创建配置类,重写ConfigurationCustomizer接口的customize方法
package com.yhhy.springboot.config;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
//此注解使用的是全类名,因为上面已经导入了org.apache.ibatis.session.Configuration
@org.springframework.context.annotation.Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer() {
@Override
public void customize(Configuration configuration) {
//自定义的驼峰命名规则
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
2)、结果测试
可以映射上;