Spring Boot整合MyBatis有两种方式,即注解和配置文件,这两种方式的基础环境和配置都是一样的。
基础配置准备
1、引入MyBatis依赖
新建一个SpringBoot项目,勾选需要的starter场景。
<!--引入SpringBoot的MyBatis starter-->
<!--该starter是MyBatis开发,用于兼容SpringBoot-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
2、配置数据源
配置数据源相关属性,数据源依然采用Druid,该部分的配置见上一章节,这里不再赘述。
3、创建数据库
创建数据库、数据表等,测试项目是否能连接上,创建数据表对应的Java Bean。
/**
* 数据表student对应的Java Bean,数据表中有id,name等字段
**/
public class Student {
public Integer id;
public String name;
// 此处省略get方法、set方法、构造函数等
}
方法一:注解版
Controller层
@RestController
public class StudentController {
@Autowired
public StudentMapper studentMapper;
@GetMapping("/student/{id}")
public Student getSudent(@PathVariable("id") Integer id){
return studentMapper.getStudentById(id);
}
@GetMapping("/insertStudent")
public Student insertStudent(Student student){
studentMapper.insertStudent(student);
return student;
}
}
Mapper层
// 指定这是一个操作数据库的mapper
@Mapper
public interface StudentMapper {
@Select("select * from student where id=#{id}")
public Student getStudentById(Integer id);
@Delete("delete from student where id=#{id}")
public int deleteStudentById(Integer id);
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into student(name) values(#{name})")
public int insertStudent(Student student);
@Update("update student set name=#{name} where id=#{id}")
public int updateStudent(Student student);
}
Configuration 配置
// 自定义MyBatis配置规则,给容器中添加一个 ConfigurationCustomizer
@Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer(){
@Override
public void customize(Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
Spring Boot启动类
// 使用MapperScan批量扫描所有的Mapper接口
@MapperScan(value = "com.gmd.springboot.mapper")
@SpringBootApplication
public class SpringBootMyBatis0421Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootMyBatis0421Application.class, args);
}
}
方法二:配置文件版
mybatis:
config-location: classpath:mybatis/mybatis-config.xml ## 指定全局配置文件的位置
mapper-locations: classpath:mybatis/mapper/*.xml ## 指定sql映射文件的位置
<!--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>
<!--StudentMapper.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.zzz.springboot.mapper.StudentMapper">
<select id="getStudent" resultType="com.gmd.springboot.Student">
SELECT * FROM student WHERE id=#{id}
</select>
<insert id="insertStudent">
INSERT INTO student(name) VALUES (#{name})
</insert>
</mapper>
更多使用参照:
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/