选择了,web,MySQL,JPA组件作为我们开发必备组件
JPA(Java Persistence API)是Sun官方提出的Java持久化规范,用来方便大家操作数据库。
真正干活的可能是Hibernate,TopLink等等实现了JPA规范的不同厂商,默认是Hibernate。
配置数据源以及JPA
配置DataSource以及JPA
用application.yml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/test?useUniCode=true&characterEncoding=utf8&useSSL=false
driverClassName: com.mysql.jdbc.Driver
username: admin
password: admin
jpa:
database: MySQL
show-sql: true
hibernate:
naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
show-sql用来在控制台输出JPA自语句。
创建实体
我们根据数据库中的字段对应创建一个UserEntity来作为对应操作 实体类注解 主键生成策略
package com.cxy.entity;
import javax.persistence.*;
import java.io.Serializable;
@Entity //这是一个实体Bean
@Table(name = "t_user") //指定了Entity所要映射带数据库表,缺少采用类名
public class UseEntity implements Serializable { //实现序列化
@Id //将属性定为主键列
@GeneratedValue
//主键生成策略,JPA自动选,Mysql为@GenerationType(strategy = GenerationType.IDENTITY)//主键自增生成策略,Mysql
@Column(name = "t_id")
private Long id;
@Column(name = "t_name")
private String name;
@Column(name = "t_age")
private int age;
@Column(name = "t_address")
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
创建JPA
实体类已经创建完成了,接着需要使用SpringDataJPA来完成数据库操作,新建名字叫做jpa的package,然后创建UserJPA接口并且继承SpringDataJPA内的接口作为父类,当然还有序列化接口
package com.cxy.jpa;
import com.cxy.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;//简单数据操作
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;//复杂查询
import java.io.Serializable;//序列化接口
public interface UserJPA extends JpaRepository<UserEntity,Long>,JpaSpecificationExecutor<UserEntity>,Serializable{
}
继承了JpaRepository接口(SpringDataJPA提供的简单数据操作接口)、JpaSpecificationExecutor(SpringDataJPA提供的复杂查询接口)、Serializable(序列化接口)。
不需要做其他的任何操作了,因为SpringBoot以及SpringDataJPA会为我们全部搞定,SpringDataJPA内部使用了类代理的方式让继承了它接口的子接口都以spring管理的Bean的形式存在,也就是说我们可以直接使用@Autowired注解在spring管理bean使用,现在数据库层几乎已经编写完成。
控制层编写查询方法
UserController中,然后创建一个查询,添加,更新,删除方法
package com.cxy.controller;
import com.cxy.jpa.UserJPA;
import com.cxy.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserJPA userJPA;
/**
* 查询用户列表方法
*
* @return
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
public List<UserEntity> list() throws Exception{
return userJPA.findAll(); //SpringDataJPA为我们提供的内置方法,它可以查询表内所有的数据
}
/**
*增加,更新用户方法
* @Param entity
* @return
*/
@RequestMapping(value = "/save",method = RequestMethod.GET)
public UserEntity save(UserEntity entity) throws Exception{
return userJPA.save(entity); //userJPA.save方法可以执行添加也可以执行更新,
// 如果需要执行持久化的实体存在主键值则更新数据,如果为0则添加数据。
}
/**
* 删除用户方法
*
* @return
*/
@RequestMapping(value = "/delete",method = RequestMethod.GET)
public List<UserEntity> delete(Long id) throws Exception
{
userJPA.delete(id);
return userJPA.findAll();
}
}
常见bug
1.启动失败的问题('hibernate.dialect' not set)
Hibernate SQL方言没有设置导致的,在properties文件中增加下面这行:
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
2.The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one时区错误
application.properties 的adtasource.url加上
&serverTimezone=GMT%2B8