目录
1、MyBatis配置
1.1、导入mybatis官方starter
1.2、myBatis自动引入依赖
1.3、MyBatis配置文件
1.3.1、注解模式:直接在springboot的配置文件 application.yaml 中进行配置
1.3.2、配置模式:创建 MyBatis 全局配置文件,进行配置
2、MyBatis的使用
2.1、配置模式
2.1.1、编写mapper接口。标注@Mapper注解
2.1.2、编写sql映射文件并绑定mapper接口
2.1.3、在application.yaml中指定Mapper配置文件的位置
2.1.4、mapper 方法调用
2.2、注解模式
2.2.1、编写Mapper接口并标注@Mapper注解
2.2.2、直接在mapper接口上添加注解,表示接口的sql语句、参数属性等
2.2.3、mapper 方法调用 同2.1.4小节
2.3、MyBatis使用时的小问题
2.3.1、数据库变量为下划线命名,java对应属性名为驼峰命名
1、MyBatis配置
1.1、导入mybatis官方starter
方法一:手动导入
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
方法二:在创建project时,选择Mybatis Framework
1.2、myBatis自动引入依赖
1.3、MyBatis配置文件
1.3.1、注解模式:直接在springboot的配置文件 application.yaml 中进行配置
# 配置mybatis规则
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置
configuration:
# 变量名下划线转驼峰
map-underscore-to-camel-case: true
1.3.2、配置模式:创建 MyBatis 全局配置文件,进行配置
1)在 application.yaml 中声明全局配置文件位置
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置
2)创建MyBatis全局配置文件 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>
2、MyBatis的使用
数据库crud所使用的对象 city :
package com.uclass.thymeleaf.springbootthymeleaf.bean;
import lombok.Data;
@Data
public class City {
private Long id;
private String name;
private String state;
private String country;
}
2.1、配置模式
2.1.1、编写mapper接口。标注@Mapper注解
@Mapper
public interface CityMapper {
public City getById(Long id);
public void insertCity(City city);
}
2.1.2、编写sql映射文件并绑定mapper接口
绑定mapper接口:
- <mapper namespace="com.uclass.thymeleaf.springbootthymeleaf.mapper.CityMapper">
- namespace 中为 mapper 接口的全路径表示
<?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.uclass.thymeleaf.springbootthymeleaf.mapper.CityMapper">
<!-- public void insertCity(City city);-->
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into city(`name` ,`state`, `country`) values (#{name}, #{state}, #{country})
</insert>
</mapper>
2.1.3、在application.yaml中指定Mapper配置文件的位置
sql映射文件位置:mapper-locations: classpath:mybatis/mapper/*.xml
# 配置mybatis规则
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置
configuration:
map-underscore-to-camel-case: true
2.1.4、mapper 方法调用
1)编写service层代码直接调用mapper接口
@Service
public class CityService {
@Autowired
CityMapper cityMapper;
public City getById(Long id) {
return cityMapper.getById(id);
}
public void saveCity(City city) {
cityMapper.insertCity(city);
}
}
2)controller层接收映射,调用service层方法,并返回操作结果
@Slf4j
@Controller
public class IndexController {
@Autowired
CityService cityService;
@PostMapping("/city")
public City saveCity(City city) {
cityService.saveCity(city);
return city;
}
@ResponseBody
@GetMapping("/city")
public City getCityById(@RequestParam("id") Long id){
return cityService.getById(id);
}
}
2.2、注解模式
2.2.1、编写Mapper接口并标注@Mapper注解
@Mapper
public interface CityMapper {
public City getById(Long id);
public void insertCity(City city);
}
2.2.2、直接在mapper接口上添加注解,表示接口的sql语句、参数属性等
@Mapper
public interface CityMapper {
@Select("select * from city where id = #{id}")
public City getById(Long id);
@Insert("insert into city(`name` ,`state`, `country`) values (#{name}, #{state}, #{country})")
@Options(useGeneratedKeys = true, keyProperty = "id")
public void insertCity(City city);
}
2.2.3、mapper 方法调用 同2.1.4小节
2.3、MyBatis使用时的小问题
2.3.1、数据库变量为下划线命名,java对应属性名为驼峰命名
1)mybatis默认没有开启驼峰命名策略
当直接进行查询时,有些字段会出现查询为空的情况
2)配置Mybatis,开启驼峰命名策略
注意:两种配置方式只能选一个,否则会起冲突
方法一:在全局配置文件中开启:
<?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>
方法二:在springboot配置文件 application.yaml中开启
# 配置mybatis规则
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置
configuration:
map-underscore-to-camel-case: true
最终查询结果: