SpringBoot集成MyBatis
Posted if年少有为
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot集成MyBatis相关的知识,希望对你有一定的参考价值。
1.创建Maven工程,pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Mybatis和Spring整合依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2.创建数据表并编写对应实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Student implements Serializable {
private Long id;
private String name;
private Integer age;
}
3.创建StudentRepository接口
public interface StudentRepository {
public List<Student> findAll();
public Student findAllById(Long id);
public void save(Student student);
public void update(Student student);
public void deleteById(Long id);
}
4.在resources/mapping路径下创建StudentRepository接口对应的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">
<mapper namespace="com.sbmybatis.repository.StudentRepository">
<select id="findAll" resultType="com.sbmybatis.entity.Student">
select * from student;
</select>
<select id="findById" parameterType="java.lang.Long" resultType="com.sbmybatis.entity.Student">
select * from student where id = #{id};
</select>
<insert id="save" parameterType="com.sbmybatis.entity.Student">
insert into student(id,name,age) values(#{id},#{name}.#{age});
</insert>
<insert id="update" parameterType="com.sbmybatis.entity.Student">
update student set name=#{name},age=#{age} where id=#{id}
</insert>
<delete id="deleteById" parameterType="java.lang.Long">
delete student where id = #{id}
</delete>
</mapper>
5.创建StudentHandler,将StudentRepository注入进去
@RestController
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping(value = "/findAll")
public List<Student> findAll() {
return studentRepository.findAll();
}
@GetMapping(value = "/findById/{id}")
public Student findById(@PathVariable("id") Long id) {
return studentRepository.findById(id);
}
@PostMapping(value = "/save")
public int save(@RequestBody Student student) {
return studentRepository.save(student);
}
@PutMapping(value = "/update")
public int update(@RequestBody Student student) {
return studentRepository.update(student);
}
@DeleteMapping(value = "/deleteById/{id}")
public int save(@PathVariable("id") Long id) {
return studentRepository.deleteById(id);
}
}
6.配置Springboot(application.yml)
server:
port: 8888
#数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
username: root
password: zxcv
#Mapper文件路径
mybatis:
mapper-locations: classpath:/mapping/*.xml
#设置别名
type-aliases-package: com.sbmybatis.entity
8.创建Application
@SpringBootApplication
/**
* 因为repository不是Spring管理的,所以需要将他扫描添加到Ioc容器中
* 添加@MapperScan(“com.sbmybatis.repository”)注解以后,com.sbmybatis.repository包下面的接口类,在编译之后都会生成相应的实现类
*/
@MapperScan(value = "com.sbmybatis.repository")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
以上是关于SpringBoot集成MyBatis的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot集成mybatis以及自动化测试代码实现
SpringBoot 集成MyBatis-Plus提示反序列化异常:cannot deserialize from Object value (no delegate- or property-bas