< parent>
< groupId> org.springframework.boot</ groupId>
< version> 2.7.5</ version>
< artifactId> spring-boot-starter-parent</ artifactId>
</ parent>
< dependencies>
< 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.2.2</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
</ dependency>
</ dependencies>
package com. southwind. entity ;
import lombok. Data ;
import java. util. Date ;
@Data
public class Student {
private Long id;
private String name;
private Double score;
private Date birthday;
}
创建 StudentRepository.java
package com. southwind. repository ;
import com. southwind. entity. Student ;
import java. util. List ;
public interface StudentRepository {
public List < Student > findAll ( ) ;
public Student findById ( Long id) ;
public void save ( Student student) ;
public void update ( Student student) ;
public void deleteById ( Long id) ;
}
在 resources/mapping 路径下创建 StudentRepository 接口对应的 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" >
< mapper namespace = " com.southwind.repository.StudentRepository" >
< select id = " findAll" resultType = " Student" >
select * from student
</ select>
< select id = " findById" parameterType = " java.lang.Long" resultType = " Student" >
select * from student where id = #{id}
</ select>
< insert id = " save" parameterType = " Student" >
insert into Student(name,score,birthday) values(#{name},#{score},#{birthday})
</ insert>
< update id = " update" parameterType = " Student" >
update student set name = #{name},score = #{score},birthday = #{birthday} where id = #{id}
</ update>
< delete id = " deleteById" parameterType = " java.lang.Long" >
delete from student where id = #{id}
</ delete>
</ mapper>
创建 StudentHandler,注入 StudentRepository
package com. southwind. controller ;
import com. southwind. entity. Student ;
import com. southwind. repository. StudentRepository ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. * ;
import java. util. List ;
@RestController
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) ;
}
}
spring :
datasource :
url : jdbc: mysql: //localhost: 3306/javaee2? useUnicode=true&characterEncoding=UTF- 8
username : root
password : 111111
driver-class-name : com.mysql.cj.jdbc.Driver
mybatis :
mapper-locations : classpath: /mapping/*.xml
type-aliases-package : com.southwind.entity
package com. southwind ;
import org. mybatis. spring. annotation. MapperScan ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
@SpringBootApplication
@MapperScan ( "com.southwind.repository" )
public class Application {
public static void main ( String [ ] args) {
SpringApplication . run ( Application . class , args) ;
}
}