一、配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lin</groupId>
<artifactId>mybatis_test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis_test1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
<build>
<!-- 可以读取xml文件 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、application.yml
# 数据库配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/springBoot?serverTimezone=UTC
username: root
password: 147250
driver-class-name: com.mysql.cj.jdbc.Driver
# 整合MyBatis
mybatis:
# 配置mapper
mapper-locations: classpath:/com/lin/mybatis_test1/mapper/xml/*.xml
# 配置实体类前缀
type-aliases-package: com.lin.mybatis_test1.entity
configuration:
# 打印语句
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 自动转换驼峰
map-underscore-to-camel-case: true
三、Student
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private Long id;
private String name;
private Integer age;
private LocalDate birthday;
}
四、StudentRepository
添加@Repository注解
@Repository
public interface StudentRepository {
List<Student> findAll();
Student findById(long id);
void deleteById(long id);
void save(Student student);
void update(Student student);
}
五、StudentRepository.xml
在resources文件夹中新建mapping文件夹,用于放置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.lin.repository.StudentRepository">
<select id="findAll" resultType="com.lin.entity.Student">
select * from student
</select>
<select id="findById" resultType="com.lin.entity.Student" parameterType="long">
select * from student where id= #{id}
</select>
<insert id="save" parameterType="com.lin.entity.Student">
insert into student(name,age,birthday) values(#{name},#{age},#{birthday})
</insert>
<update id="update" parameterType="com.lin.entity.Student">
update student set name=#{name},age=#{age},birthday=#{birthday} where id=#{id}
</update>
<delete id="deleteById" parameterType="long">
delete from student where id=#{id}
</delete>
</mapper>
六、StudentHandler
@RestController
@RequestMapping("/student")
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findAll")
public List<Student> findAll(){
return studentRepository.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") long id){
return studentRepository.findById(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
studentRepository.save(student);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
studentRepository.update(student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") long id){
studentRepository.deleteById(id);
}
}
七、application
添加@MapperScan注解,添加对mapper包的扫描
@MapperScan("com.lin.repository")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}