pom.xml
<parent>
<artifactId>spring-boot-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.4.RELEASE</version>
</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>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
application.yml配置数据库和myBatis映射
spring:
datasource:
url: jdbc:mysql://localhost:3306/mbtest?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: hsp
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:/mapping/*.xml
type-aliases-package: com.entity
实体类:
import java.util.Date;
@Data
public class Student {
private Long id;
private String name;
private Double score;
private Date birthday;
}
repository
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);
}
对应的映射:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.repository.StudentRepository">
<!-- <resultMap id="StudentResultMap" type="com.example.model.Student">-->
<!-- <id property="id" column="id"/>-->
<!-- <result property="name" column="name"/>-->
<!-- <result property="score" column="score"/>-->
<!-- <result property="birthday" column="birthday"/>-->
<!-- </resultMap>-->
<select id="findById" parameterType="java.lang.Long" resultType="Student">
SELECT * FROM student WHERE id = #{id}
</select>
<select id="findAll" resultType="Student">
SELECT * FROM student
</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="Long">
DELETE FROM student WHERE id = #{id}
</delete>
</mapper>
controller:
@RestController
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findAll")
private 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);
}
}
启动类
@SpringBootApplication
@MapperScan("com.repository")
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}