1.在pom.xml加入配置
<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.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
2.在项目的classpath下新建application.yml文件
server:
port: 8080
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.example.entity
3.新建实体User
public class User {
private Integer id;
private String userName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
4.mapper接口和mapper.xml映射配置
UserMapper.java
@Repository
public interface UserMapper {
User getOne(int id);
//注解形式
@Select("select * from user where id = #{id}")
User getOne2(int id);
public int insert(User user);
}
新建UserMapper.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.example.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.entity.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="userName" jdbcType="VARCHAR" property="userName" />
</resultMap>
<select id="getOne" resultType="com.example.entity.User">
select * from user where id = #{id}
</select>
<insert id="insert">
insert into user(userName) values(#{userName})
</insert>
</mapper>
5.Service层
UserService.java
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public User getOne(int id){
return userMapper.getOne(id);
}
public User getOne2(int id){
return userMapper.getOne2(id);
}
public int insert(User user){
return userMapper.insert(user);
}
}
6.Controller层
UserController.java
@RestController
@RequestMapping("/testMyBatis")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("getOne/{id}")
public String getOne(@PathVariable int id){
return userService.getOne(id).toString();
}
@RequestMapping("getOne2/{id}")
public String getOne2(@PathVariable int id){
return userService.getOne2(id).toString();
}
@RequestMapping("insert")
public int Insert(User user){
return userService.insert(user);
}
}
7.全局配置系统对mapper接口扫描
在Application类上添加配置
@MapperScan("com.example.mapper") //扫描的mapper
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
8.整体目录结构