MyBatis的GitHub仓库
MyBatis官方
starter的命名方式:
- SpringBoot官方的Starter:
spring-boot-starter-*
- 第三方的:
*-spring-boot-starter
引入依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
配置模式:
- 全局配置文件。
SqlSessionFactory:
自动配置好了。SqlSession:
自动配置了SqlSessionTemplate 组合了SqlSession。@Import(AutoConfiguredMapperScannerRegistrar.class)。
Mapper:
只要我们写的操作MyBatis的接口标准了@Mapper
就会被自动扫描进来。 ```java @EnableConfigurationProperties(MybatisProperties.class) : MyBatis配置项绑定类。 @AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class }) public class MybatisAutoConfiguration{ … }
@ConfigurationProperties(prefix = “mybatis”) public class MybatisProperties{ … }
**配置文件**:application.yaml<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/13019604/1618904024865-726e0d8d-6c19-43f0-b782-d9c0ba02f1ef.png#align=left&display=inline&height=199&margin=%5Bobject%20Object%5D&name=image.png&originHeight=199&originWidth=271&size=7658&status=done&style=none&width=271)
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/user
username: root
password: 941941
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
# config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置
configuration:
map-underscore-to-camel-case: true #开启驼峰命名,不能与上面的 config-location: 同时使用。
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">
<!--
namespace:必须有值,自定义的唯一字符串
推荐使用:dao 接口的全限定名称
-->
<mapper namespace="com.wzy.springbootmybatis.dao.PersonDao">
<!--id:接口中的方法名,resultType 与数据库对应的JavaBean类-->
<select id="getPesonOne" resultType="com.wzy.springbootmybatis.pojo.Person">
<!--要执行的 sql 语句,#{id}为站位,可接收参数。-->
select id,username,gender,age,phone from person where id=#{id}
</select>
</mapper>
数据表:
Person:
package com.wzy.springbootmybatis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data //Getter、Setter方法
@NoArgsConstructor//无参构造器
@AllArgsConstructor//全参构造器
public class Person {
private Integer id;
private String username;
private String gender;
private Integer age;
private String phone;
}
Mapper接口 PersonDao:
package com.wzy.springbootmybatis.dao;
@Mapper
public interface PersonDao {
public Person getPesonOne(Integer id);
}
Service层的 PersonServlce:
package com.wzy.springbootmybatis.service;
@Service
public class PersonServlce {
@Autowired
PersonDao personDao;
public Person getSelectPersonOne(Integer id){
return personDao.getPesonOne(id);
}
}
Controller 中的 PerosnController:
package com.wzy.springbootmybatis.controller;
@Controller
public class PerosnController {
@Autowired
PersonServlce personServlce;
@ResponseBody
@RequestMapping(value = "/person",method = RequestMethod.GET)
public Person getPerson(@RequestParam("id") Integer id){
return personServlce.getSelectPersonOne(id);
}
}
测试结果: