src/main/java/com/example/demo/controller/DepartmentController.java
package com.example.demo.controller; package com.example.demo.controller; import com.example.demo.mapper.DepartmentMapper; import com.example.demo.pojo.Department; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DepartmentController { @Autowired private DepartmentMapper departmentMapper; @GetMapping("/dept/{id}") public Department getDepartment(@PathVariable("id") Integer id){ return departmentMapper.getById(id); } @GetMapping("/dept") public Department insertDepartment(Department department){ departmentMapper.insert(department); return department; } }
src/main/java/com/example/demo/DemoApplication.java
package com.example.demo; package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; // 自动扫描mapper接口,不用每个mapper都添加@Mapper注解 @MapperScan(value = {"com.example.demo.mapper"}) @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
整合 MyBatis(二)-配置版 MyBatis
文档:
https://mybatis.org/mybatis-3/zh/index.html
application.yml
mybatis: # 指定全局配置文件路径 config-location: classpath:mybatis/mybatis-config.xml # 指定mapper文件路径 mapper-locations: classpath:mybatis/mapper/*.xml
src/main/resources/mybatis/mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--开启驼峰命名自动映射--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
src/main/resources/mybatis/mapper/EmployeeMapper.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.example.demo.mapper.EmployeeMapper"> <select id="getById" resultType="com.example.demo.pojo.Employee"> select * from employee where id = #{id} </select> <insert id="insert"> insert into employee (name, age, sex, birth, department_id) values (#{name}, #{age}, #{sex}, #{birth}, #{department_id}) </insert> <delete id="deleteById"> delete from employee where id = #{id} </delete> </mapper>
src/main/java/com/example/demo/mapper/EmployeeMapper.java
package com.example.demo.mapper; import com.example.demo.pojo.Employee; // @Mapper 或@MapperScan 将接口扫描装配到容器中 public interface EmployeeMapper { public Employee getById(Integer id); public int deleteById(Integer id); public void insert(Employee employee); }
src/main/java/com/example/demo/controller/DepartmentController.java
package com.example.demo.controller; package com.example.demo.controller; import com.example.demo.mapper.DepartmentMapper; import com.example.demo.mapper.EmployeeMapper; import com.example.demo.pojo.Department; import com.example.demo.pojo.Employee; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DepartmentController { @Autowired private EmployeeMapper employeeMapper; @GetMapping("/getEmp/{id}") public Employee getEmployee(@PathVariable("id") Integer id){ return employeeMapper.getById(id); } }
SpringData JPA
SpringData 为我们提供使用同一的 API 来对数据访问层进行操作
JPA: Java Persistence API
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.3.1.RELEASE</version> </dependency>
配置文件
spring:
jpa:
hibernate:
# 更新或创建表结构
ddl-auto: update
# 控制台打印sql
show-sql: true
JAP:ORM Object Relation Mapping
编写实体类与数据表进行映射
package com.example.demo.entity; import javax.persistence.*; // 使用JPA注解配置映射关系 @Entity // 实体类 @Table(name = "tbl_user") // 指定表名 public class User { @Id // 主键 @GeneratedValue(strategy = GenerationType.IDENTITY) // 自增 private Integer id; @Column(name = "last_name", length = 50) private String lastName; @Column // 默认类名=属性名 private String email; }
创建 repository
package com.example.demo.repository; import com.example.demo.entity.User; import org.springframework.data.jpa.repository.JpaRepository; // 继承JpaRepository来完成对数据库的操作 public interface UserRepository extends JpaRepository<User, Integer> { }
Controller
package com.example.demo.controller; import com.example.demo.entity.User; import com.example.demo.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.Optional; @RestController public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/user/{id}") public User getUser(@PathVariable("id") Integer id){ Optional<User> user = userRepository.findById(id); if(user.isPresent()){ return user.get(); } else{ return null; } } @GetMapping("/user") public User insertUser(User user){ User savedUser = userRepository.save(user); return savedUser; } }