JPA初试: JPA 方便快捷,操作对象
1.Maven依赖
<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.0</modelVersion>
<groupId>com.jdbc.oo</groupId>
<artifactId>JdbcUtil</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 继承spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<!--
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
-->
</exclusions>
</dependency>
<!-- jdbcTemplate工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<!--引入JPA的依赖关系-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
</project>
2.application.properties配置 SpringDataJPA 自动创建数据表
server.port=8089
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/happy_code?serverTimezone=GMT%2B8&&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username: root
spring.datasource.password: 123456
spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.database=mysql
spring.jpa.show-sql=true
3.实体类,注意是 javax.persistence下的注解
package com.mxy.springboot.entity;
import javax.persistence.*;
@Entity
@Table(name="t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "t_id")
private Long id;
@Column(name = "t_name")
private String name;
@Column(name = "t_age")
private String age;
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 String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name = "t_address")
private String address;
}
4.JPA类
package com.mxy.springboot.jpa;
import com.mxy.springboot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.io.Serializable;
public interface UserJPA extends JpaRepository<User,Long>,JpaSpecificationExecutor<User>,Serializable {
}
5.调用UserJPA中的方法
package com.mxy.springboot.controller;
import com.mxy.springboot.entity.User;
import com.mxy.springboot.jpa.UserJPA;
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;
@RestController
@RequestMapping(value = "/user")
public class JPAController {
@Autowired
private UserJPA userJPA;
/**
* 数据新增或更新,save方法可以执行添加也可以执行更新,如果需要执行持久化的实体存在主键值则更新数据,如果不存在则添加数据。
*/
@RequestMapping(value = "/save", method = RequestMethod.GET)
public User save(User user) {
return userJPA.save(user);
}
}
6.看效果,啦啦啦,德玛西亚