SpringBoot操作数据库有多种方式,如
JDBC直接操作:太古老了,没人愿意这样玩
Mybatis插件:比较时髦,比较适合sql复杂,或者对性能要求高的应用,因为sql都是自己写的。
Spring-data-jpa: 使用hibernate作为实现,基本上不需要写sql,因为sql都是统计的,总是会产生多余的查询,性能上相对而言会低,但不绝对,影响性能的因素是多种的,这里说的性能是 从最终的查询的sql来对比的,毕竟生成的sql没有经过深思熟虑写出来的性能好。
JdbcTemplate:spring在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。Spring-data-jpa引入的时候,JdbcTemplate必然会被引入的。
当然还有其他中间件,主流使用的就是Mybatis和Spring-data-jpa。
品茗IT-SpringBoot专题-同步发布
品茗IT 提供在线支持:
一键快速构建Spring项目工具
一键快速构建SpringBoot项目工具
一键快速构建SpringCloud项目工具
一站式Springboot项目生成
如果大家正在寻找一个java的学习环境,或者在开发中遇到困难,可以加入我们的java学习圈,点击即可加入,共同学习,节约学习时间,减少很多在学习中遇到的难题。
需要同时引入数据库的connector和数据源datasource。和使用mybatis一样,只需要替换Mybatis引入的jar依赖即可。
依赖如下:
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-dbcp2artifactId>
dependency>
完整的依赖如下所示:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>cn.pomitgroupId>
<artifactId>testjpaartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.4.RELEASEversion>
parent>
<name>testjpaname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-dbcp2artifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
server.port=8080
spring.application.name=pomit
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/boot?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=cff
spring.datasource.password=123456
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.dbcp2.max-wait-millis=60000
spring.datasource.dbcp2.min-idle=20
spring.datasource.dbcp2.initial-size=2
spring.datasource.dbcp2.validation-query=SELECT 1
spring.datasource.dbcp2.connection-properties=characterEncoding=utf8
spring.datasource.dbcp2.validation-query=SELECT 1
spring.datasource.dbcp2.test-while-idle=true
spring.datasource.dbcp2.test-on-borrow=true
spring.datasource.dbcp2.test-on-return=false
#JPA Configuration:
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.implicit-strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
这里面,包含了数据库连接信息、数据源的连接池配置信息、jpa配置信息。
spring.jpa.hibernate.ddl-auto属性,是对表的操作:
spring.jpa.hibernate.naming.implicit-strategy和spring.jpa.hibernate.naming.physical-strategy是对表和实体字段映射的默认处理方式。
实体名称映射到数据库中时,分成两个步骤:
PhysicalNamingStrategy和ImplicitNamingStrategy的区别:
所以,这里的配置,映射到表字段时,所有点都被下划线替换,骆驼情况也被下划线替换。默认情况下,所有表名都以小写生成
假设我们有一张这个表user_role :
实体:
package cn.pomit.testboot.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user_role")
public class UserRole {
@Id
private int id;
@Column(name = "user_name")
private String userName;
private String role;
private String phone;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setRole(String role) {
this.role = role;
}
public String getRole() {
return role;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
}
UserRoleDao:
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import cn.pomit.testboot.domain.UserRole;
public interface UserRoleDao extends CrudRepository<UserRole, Integer> {
List<UserRole> findByRole(String role);
}
可以看到,使用Spring-data-jpa就是就这么简单,只需要继承CrudRepository即可。
同时,Spring-data-jpa还支持findBy + 字段(And 字段)进行查询。deleteBy + 字段(And 字段)进行删除。
分页等其他操作后续再讲。
UserRoleService:
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.pomit.testboot.domain.UserRole;
import cn.pomit.testboot.mapper.UserRoleDao;
@Service
public class UserRoleService {
@Autowired
UserRoleDao userRoleDao;
public List<UserRole> selectAll() {
return (List<UserRole>) userRoleDao.findAll();
}
public void saveTest(UserRole userRole) {
userRoleDao.save(userRole);
}
@Transactional
public void update(Integer id, String phone) {
UserRole userRole = userRoleDao.findById(id).orElse(null);
if (userRole != null) {
userRole.setPhone(phone);
userRoleDao.save(userRole);
}
}
@Transactional
public void delete(Integer id) {
userRoleDao.deleteById(id);
}
public List<UserRole> findByRole(String role) {
return userRoleDao.findByRole(role);
}
}
MybatisRest:
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.pomit.testboot.domain.UserRole;
import cn.pomit.testboot.service.UserRoleService;
@RestController
@RequestMapping("/db")
public class TestJpaRest {
@Autowired
UserRoleService userRoleService;
@RequestMapping(value = "/query")
public List<UserRole> query() {
return userRoleService.selectAll();
}
@RequestMapping(value = "/testFind")
public List<UserRole> testFind() {
return userRoleService.findByRole("ADMIN");
}
@RequestMapping(value = "/save")
public String save() {
UserRole userRole = new UserRole();
userRole.setRole("TEST");
userRole.setUserName("TEST");
userRole.setPhone("3424234");
userRoleService.saveTest(userRole);
return "0000";
}
@RequestMapping(value = "/update")
public String update() {
userRoleService.update(4, "454");
return "0000";
}
@RequestMapping(value = "/delete")
public String delete() {
userRoleService.delete(4);
return "0000";
}
}
package cn.pomit.testboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JpaApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}