Spring Boot 支持整合以下几种持久层技术

  MyBatis

  Hibernate

  Spring Data

  JdbcTemplate

 

Spring Boot 整合JdbcTemplate

    JdbcTemplate 是 Spring ⾃带的 JDBC 模版组件,底层实现了对 JDBC 的封装,⽤法与 MyBatis 类似。开发者需要⾃定义 SQL 语句,JdbcTemplate 帮助开发者完成数据库连接,SQL 语句的执⾏,以及结果集的解析。

     JdbcTemplate 不⾜之处是灵活性不如 MyBatis,因为 MyBatis 的 SQL 定义在 XML 中,更有利⽤维护和扩展,但是 JdbcTemplate 是以硬编码的⽅式将 SQL 直接写在 Java 代码中,不利⽤扩展和维护。

     JdbcTemplate 对基本的 CRUD 操作提供了良好的⽀持,通过调⽤ query 和 update ⽅法即完成相关操 作,其中 query 是⽤来做查询操作的,update 是⽤来做添加、删除、修改功能。

1、添加pom依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

 

2、配置application.xml

spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatis
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver

 

3、创建实体类

package com.sunjian.entity;

import lombok.Data;

@Data
public class Student {
private Long id;
private String name;
}

 

4、创建接口及实现类

package com.sunjian.repository;

import com.sunjian.entity.Student;

public interface StudentRepository {
public Student findById(Long id);
}
package com.sunjian.repository.impl;

import com.sunjian.entity.Student;
import com.sunjian.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class StudentRepositoryImpl implements StudentRepository {
@Autowired
private JdbcTemplate jdbcTemplate;

@Override
public Student findById(Long id) {
return jdbcTemplate.queryForObject("select * from student where id = ?",new Object[]{id},new BeanPropertyRowMapper<Student>(Student.class));
}
}

 

5、编写controller视图类

package com.sunjian.controller;

import com.sunjian.entity.Student;
import com.sunjian.repository.StudentRepository;
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;

@RestController
public class StudentController {
@Autowired
private StudentRepository studentRepository;

@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") Long id){
return studentRepository.findById(id);
}
}

 

6、启动项目,访问

 

  • query

  1、queryForObject(String sql,Object[] args,RowMapper rowMapper),查询⼀条记录,并且将结果 集封装成 Java 对象。

  2、query(String sql,RowMapper rowMapper),查询⼀组数据,并且将结果集封装成集合对象。 RowMapper 是⼀个接⼝,作⽤就是解析结果集,将 JDBC 查询出的 ResultSet 对象转换为对应的 Java 对象,在调⽤该对象时需要指定⽬标类的结构,泛型。

  • update

  update(String sql, Object... args) 参数:

  1、要执⾏的 SQL 语句。

  2、可变参数 Object... args,满⾜参数的可变性。

  注意:如果,客户端⽤?传参 key-value 形式将数据传给 Spring MVC 服务端,业务⽅法的形参列表不需要添加任何注解即可⾃动封装参数 Java 对象。

  如果,客户端传 JSON 格式数据,业务⽅法的形参列表需要添加 @RequestBody 注解,才能封装参数 Java 对象。

 

Spring Boot 整合 MyBatis

1、pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!-- mybaits -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>

 

2、创建实体类

package com.sunjian.entity;

import lombok.Data;

@Data
public class Student {
private Long id;
private String name;
}

 

3、创建接口repository类

package com.sunjian.repository;

import com.sunjian.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);
}

 

4、在resources目录下创建UserRepository.xml文件,编写SQL语句

<?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.sunjian.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) values(#{name})
</insert>

<update id="update" parameterType="Student">
update student set name = #{name} where id = #{id}
</update>

<delete id="deleteById" parameterType="java.lang.Long">
delete from student where id = #{id}
</delete>
</mapper>

 

5、配置文件application.xml

spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/sunjian2?&useSSL=false&serverTimezone=UTC

mybatis:
mapper-locations: classpath:/mapping/*.xml
type-aliases-package: com.sunjian.entity

 

6、编写controller视图类

package com.sunjian.controller;

import com.sunjian.entity.Student;
import com.sunjian.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);
}
}

 

7、启动类上添加@MapperScan注解,启动,访问

package com.sunjian;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.sunjian.repository")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}

 

 

Spring Boot 整合 Spring Data JPA

1、pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!-- JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>

 

2、application.xml

spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/sunjian2?&useSSL=false&serverTimezone=UTC

jpa:
show-sql: true
properties:
hibernate:
format_sql: true

 

3、编写实体类

package com.sunjian.entity;

import lombok.Data;

import javax.persistence.*;

@Data
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
}

  @Entity  映射数据表

  @Id   主键

  @GeneratedValue    自增

  @Column  普通列

 

4、编写repository接口及实现类

package com.southwind.repository;
import com.southwind.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student,Long> {
  public Student findByName(String name); // 根据规则(代码提示的)编写
}

 

5、编写controller类

package com.sunjian.controller;

import com.sunjian.entity.Student;
import com.sunjian.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).get();
}

@PostMapping("/save")
public void save(@RequestBody Student student){
studentRepository.save(student);
}

@PutMapping("/update")
public void update(@RequestBody Student student){
studentRepository.save(student);
}

@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") Long id){
studentRepository.deleteById(id);
}

@GetMapping("/findByName/{name}")
public Student findByName(@PathVariable("name") String name){
return studentRepository.findByName(name);
}
}

 

6、启动项目,访问

 

 

 

 

  

 

  

 

 

OK.