1.依赖
pom.xml
<dependencies>
<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>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.配置数据库连接
yaml配置:
# 数据库连接相关配置
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/stu?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
3.创建对应数据库的实体类
4.创建mapper接口类
peoper.java
@Mapper
@Repository
public interface PeoperMapper {
List<Peoper> queryAllPeoper();
Peoper queryPeoperById(@Param("id") int id);
int insertPeoper(Peoper peoper);
int updatePeoper(Peoper peoper);
int deletePeoperById(@Param("id") int id);
}
注意,mapper接口,应添加一下注解
@Mapper
@Repository
或在入口类上添加扫描注解
@MapperScan("com.xzb.mapper")
5.让springboot绑定mybatis配置文件,或更改其他配置,如:别名等.
yaml配置:
# mybatis绑定配置文件,或更改其他配置,如:别名等
mybatis:
type-aliases-package: com.xzb.pojo #别名
mapper-locations: classpath:mybatis/mapper/*.xml #绑定mapper
6.编写对应于接口的mapper.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接口-->
<mapper namespace="com.xzb.mapper.PeoperMapper">
<select id="queryAllPeoper" resultType="peoper">
select *
from stu.peoper;
</select>
<select id="queryPeoperById" resultType="peoper" parameterType="int">
select *
from stu.peoper
where id = #{id};
</select>
<insert id="insertPeoper" parameterType="peoper">
insert into stu.peoper (name, age)
values (#{name},#{age});
</insert>
<update id="updatePeoper" parameterType="peoper">
update stu.peoper
set name = #{name},age = #{age}
where id = #{id};
</update>
<delete id="deletePeoperById" parameterType="int">
delete from stu.peoper where id = #{id}
</delete>
</mapper>
7.测试
@RestController
public class MybatisController {
@Autowired
PeoperMapper peoperMapper;
@GetMapping("/all")
public List<Peoper> queryAllPeoper(){
List<Peoper> peopers = peoperMapper.queryAllPeoper();
return peopers;
}
@GetMapping("/qby/{id}")
public Peoper queryPeoperById(@PathVariable("id") int id){
Peoper peoper = peoperMapper.queryPeoperById(id);
return peoper;
}
@GetMapping("/insert")
public String insertPeoper(Peoper peoper){
peoperMapper.insertPeoper(peoper);
return "insert-ok";
}
@GetMapping("/update")
public String updatePeoper(Peoper peoper){
peoperMapper.updatePeoper(peoper);
return "update-ok";
}
@GetMapping("/delete/{id}")
public String deletePeoperById(@PathVariable("id") int id){
peoperMapper.deletePeoperById(id);
return "delete-ok";
}
}