一、添加Hibernate需要的所有Maven依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
二、添加数据源,配置application.properties文件
server.port=8082
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=false
三、这里我们还是用原始的人肉建数据库,然后在idea配置数据源和生成实体类
连上数据库之后生成实体类
四、给实体类添加注解
package com.zzx.insert.jpa;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "girl")
public class Girl {
@Id
@Column(length = 9)
private int id;
@Column
// @Column(name = "age",columnDefinition = "int(9) DEFAULT NULL comment 'age' ")
private int age;
@Column
// @Column(name = "high",columnDefinition = "int(9) DEFAULT NULL comment 'high' ")
private int high;
public int getId() {
return id;
}
//用于构造Girl对象,此博客中没有起作用
public Girl setId(int id) {
this.id = id;
return this;
}
public int getAge() {
return age;
}
public Girl setAge(int age) {
this.age = age;
return this;
}
public int getHigh() {
return high;
}
public Girl setHigh(int high) {
this.high = high;
return this;
}
}
五、添加Dao类,这里加nativeQuery = true的是原生sql的方式
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Repository
public interface JpaGirlDao extends JpaRepository<Girl,Integer> {
@Query(nativeQuery = true, value = "SELECT * FROM girl where girl.age=?1")
List<Girl> findByAge(int age);
@Transactional
@Modifying
@Query("update Girl set high=?2 where age=?1")
int updateGirl(int age,int high);
@Transactional
@Modifying
@Query(nativeQuery = true,value = "delete from girl where age=?1")
// @Query("delete from Girl where age=?1")
int deleteByAge(int age);
@Transactional
@Modifying
@Query(nativeQuery = true,value = "insert into girl (age, high) values (?1,?2)")
int addGirl(int age,int high);
}
六、直接在控制器调用,由于例子简单,就不写服务层了
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/jpa")
public class JpaGirlController {
private static final int SUCCESS_CODE=1;
@Autowired
JpaGirlDao jpaGirlDao;
/**
* 新增
* @param age
* @param high
* @return
*/
@GetMapping("/insert")
public String add(int age,int high){
int res= jpaGirlDao.addGirl(age,high);
if(res==SUCCESS_CODE){
return "success";
}
return "failure";
}
/**
* 删除
* @param age
* @return
*/
@GetMapping("/del")
public String del (int age){
int res= jpaGirlDao.deleteByAge(age);
if(res==SUCCESS_CODE){
return "success";
}
return "failure";
}
/**
* 修改
* @param age
* @param high
* @return
*/
@GetMapping("/update")
public String updateGirl(int age,int high){
int res=jpaGirlDao.updateGirl(age,high);
if(res==SUCCESS_CODE){
return "success";
}
return "failure";
}
/**
* 删除
* @param age
* @return
*/
@GetMapping("/get")
public List<Girl> getByAge(int age){
List<Girl> list=jpaGirlDao.findByAge(age);
if(CollectionUtils.isEmpty(list)){
return null;
}
return list;
}
}
七、最后就是测试了,在数据库随便写两条数据然后测试如下
成功!