前置
- idea
- jdk 8u202
- tomcat 8.5.82
新建项目
- file-> new -> project -> spring initialzr-> netx-> dependencies-> web-> srping web
整个项目已经准备完毕了 直接测试
com.example.springboot_hibernate.controller/TestController
点击查看代码
package com.example.springboot_hibernate.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("/test")
public String hello() {
return "test spring boot!";
}
}
启动项目 浏览器测试
http://localhost:8080/test
增加hibernate 支持
pom.xml
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot_hibernate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_hibernate</name>
<description>springboot_hibernate</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
resources/application.properties
点击查看代码
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
model
com.example.springboot_hibernate.model.User
点击查看代码
package com.example.springboot_hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "user")//数据库的表名
public class User implements Serializable {
/**
* 必须加ID、GeneratedValue 否则报错
*/
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age;
private String email;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
'}';
}
public User(Integer id, String name, Integer age, String email) {
this.id = id;
this.name = name;
this.age = age;
this.email = email;
}
public User(String name) {
this.name = name;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
dao
com.example.springboot_hibernate.dao.CriteriaDao
点击查看代码
package com.example.springboot_hibernate.dao;
import com.example.springboot_hibernate.model.User;
import java.util.List;
public interface CriteriaDao {
List<User> criCondition(String name, Integer pageIndex, Integer pageCount);
}
com.example.springboot_hibernate.dao.HibernateHqlDao
点击查看代码
package com.example.springboot_hibernate.dao;
import com.example.springboot_hibernate.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface HibernateHqlDao extends JpaRepository<User, Long> {
@Query("FROM User WHERE name LIKE %:name%")
List<User> selectLike(String name);
}
com.example.springboot_hibernate.dao.MyHibernateHqlDao
点击查看代码
package com.example.springboot_hibernate.dao;
import com.example.springboot_hibernate.model.User;
import java.util.List;
public interface MyHibernateHqlDao {
List<User> selectCondition(String name, Integer pageIndex, Integer pageCount);
boolean updateUser(User user);
User selectAndUpdate(User user);
}
com.example.springboot_hibernate.dao.MyHibernateSqlDao
点击查看代码
package com.example.springboot_hibernate.dao;
import com.example.springboot_hibernate.model.User;
import java.util.List;
public interface MyHibernateSqlDao {
List<User> selectCondition(String name, Integer pageIndex , Integer pageCount);
boolean updateUser(User user);
}
com.example.springboot_hibernate.dao.impl.CriteriaDaoImpl
点击查看代码
package com.example.springboot_hibernate.dao.impl;
import com.example.springboot_hibernate.dao.CriteriaDao;
import com.example.springboot_hibernate.model.User;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
@Repository
public class CriteriaDaoImpl implements CriteriaDao {
@Resource
private EntityManager entityManager;
@Override
public List<User> criCondition(String name, Integer pageIndex, Integer pageCount) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(User.class);
//创建查询条件
Root<User> userRoot = criteriaQuery .from(User.class);
Predicate name1 = criteriaBuilder.like(userRoot.get("name"), "%" + name + "%");
//in查询
Predicate email = criteriaBuilder.in(userRoot.get("email").in(new Object[]{"[email protected]","[email protected]"}));
//多条件拼接
criteriaBuilder.and(name1,email);
//将条件对象封装进入查询对象
Query query=entityManager.createQuery(criteriaQuery);
query.setFirstResult((pageIndex-1)*pageCount+1);
query.setMaxResults(pageCount);
List resultList = query.getResultList();
return resultList;
}
}
com.example.springboot_hibernate.dao.impl.HibernateHqlDaoImpl
点击查看代码
package com.example.springboot_hibernate.dao.impl;
import com.example.springboot_hibernate.dao.MyHibernateHqlDao;
import com.example.springboot_hibernate.model.User;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import java.util.List;
@Repository
public class HibernateHqlDaoImpl implements MyHibernateHqlDao {
//通过注入将EntityManager注入到数据层
@Resource
private EntityManager entityManager;
@Override
public List<User> selectCondition(String name, Integer pageIndex, Integer pageCount) {
Query query = entityManager.createQuery("select name from User where name like :name1");
query.setParameter("name1", "%" + name + "%");
query.setFirstResult((pageIndex - 1) * pageCount + 1);
query.setMaxResults(pageCount);
List list = query.getResultList();
return list;
}
//如果执行新增等需要事务管理的操作,需要 @Transactional注解打开事务管理
@Transactional
@Override
public boolean updateUser(User user) {
Query query = entityManager.createQuery("update User set name = :name,age = :age,email = :email where id = :id");
query.setParameter("name", user.getName()).setParameter("age", user.getAge()).setParameter("email", user.getEmail()).setParameter("id", user.getId());
int i = query.executeUpdate();
if (i <= 0) {
return false;
}
return true;
}
//因为会对数据库进行修改,所以事务管理必不可少
@Transactional
@Override
public User selectAndUpdate(User user) {
TypedQuery<User> query = entityManager.createQuery("select new User(id,name,age,email) from User where id =?1", User.class);
query.setParameter(1, user.getId());
User user1 = query.getSingleResult();
return user1;
}
}
com.example.springboot_hibernate.dao.impl.MyHibernateSqlDaoImpl
点击查看代码
package com.example.springboot_hibernate.dao.impl;
import com.example.springboot_hibernate.dao.MyHibernateSqlDao;
import com.example.springboot_hibernate.model.User;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
@Repository
public class MyHibernateSqlDaoImpl implements MyHibernateSqlDao {
@Resource
private EntityManager entityManager;
@Override
public List<User> selectCondition(String name,Integer pageIndex ,Integer pageCount) {
Query query = entityManager.createNativeQuery("select * from user where name like ?",User.class);
query.setParameter(1,"%"+name+"%");
query.setFirstResult((pageIndex-1)*pageCount+1);
query.setMaxResults(pageCount);
List resultList = query.getResultList();
return resultList;
}
@Transactional
@Override
public boolean updateUser(User user) {
Query query = entityManager.createNativeQuery("update user set name = :name,age = :age,email = :email where id = :id");
query
.setParameter("name",user.getName())
.setParameter("age",user.getAge())
.setParameter("email",user.getEmail())
.setParameter("id",user.getId());
int i = query.executeUpdate();
if(i<=0){
return false;
}
return true;
}
}
service
com.example.springboot_hibernate.service.CriteriaService
点击查看代码
package com.example.springboot_hibernate.service;
import com.example.springboot_hibernate.model.User;
import java.util.List;
public interface CriteriaService {
List<User> criCondition(String name, Integer pageIndex, Integer pageCount);
}
点击查看代码
package com.example.springboot_hibernate.service;
import com.example.springboot_hibernate.model.User;
import java.util.List;
public interface HibernateHqlService {
List<User> selectAll();
List<User> selectLike(String name);
List<User> selectCondition(String name, Integer pageIndex, Integer pageCount);
boolean updateUser(User user);
User selectAndUpdate(User user);
}
点击查看代码
package com.example.springboot_hibernate.service;
import com.example.springboot_hibernate.model.User;
import java.util.List;
public interface HibernateSqlService {
List<User> selectCondition(String name, Integer pageIndex , Integer pageCount);
boolean updateUser(User user);
}
com.example.springboot_hibernate.service.impl.CriteriaServiceImpl
点击查看代码
package com.example.springboot_hibernate.service.impl;
import com.example.springboot_hibernate.dao.CriteriaDao;
import com.example.springboot_hibernate.model.User;
import com.example.springboot_hibernate.service.CriteriaService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class CriteriaServiceImpl implements CriteriaService {
@Resource
private CriteriaDao oldcriteriaDao;
@Override
public List<User> criCondition(String name, Integer pageIndex, Integer pageCount) {
List<User> users = oldcriteriaDao.criCondition(name, pageIndex, pageCount);
return users;
}
}
点击查看代码
package com.example.springboot_hibernate.service.impl;
import com.example.springboot_hibernate.dao.HibernateHqlDao;
import com.example.springboot_hibernate.dao.MyHibernateHqlDao;
import com.example.springboot_hibernate.model.User;
import com.example.springboot_hibernate.service.HibernateHqlService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class HibernateHqlServiceImpl implements HibernateHqlService {
@Resource
private HibernateHqlDao hibernateHqlDao;
@Resource
private MyHibernateHqlDao myHibernateHqlDao;
@Override
public List<User> selectAll() {
List<User> users = hibernateHqlDao.findAll();
return users;
}
@Override
public List<User> selectLike(String name) {
List<User> users = hibernateHqlDao.selectLike(name);
return users;
}
@Override
public List<User> selectCondition(String name, Integer pageIndex, Integer pageCount) {
List<User> users = myHibernateHqlDao.selectCondition(name, pageIndex, pageCount);
return users;
}
@Override
public boolean updateUser(User user) {
boolean result = myHibernateHqlDao.updateUser(user);
return result;
}
@Override
public User selectAndUpdate(User user) {
User user1 = myHibernateHqlDao.selectAndUpdate(user);
return user1;
}
}
点击查看代码
package com.example.springboot_hibernate.service.impl;
import com.example.springboot_hibernate.dao.MyHibernateSqlDao;
import com.example.springboot_hibernate.model.User;
import com.example.springboot_hibernate.service.HibernateSqlService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class HibernateSqlServiceImpl implements HibernateSqlService {
@Resource
private MyHibernateSqlDao myHibernateSqlDao;
@Override
public List<User> selectCondition(String name,Integer pageIndex ,Integer pageCount) {
List<User> users = myHibernateSqlDao.selectCondition(name,pageIndex,pageCount);
return users;
}
@Override
public boolean updateUser(User user) {
boolean result = myHibernateSqlDao.updateUser(user);
return result;
}
}
controller
com.example.springboot_hibernate.controller.CriteriaController
点击查看代码
package com.example.springboot_hibernate.controller;
import com.example.springboot_hibernate.model.User;
import com.example.springboot_hibernate.service.CriteriaService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("cri")
public class CriteriaController {
@Resource
private CriteriaService oldcriteriaService;
@RequestMapping("cricondition")
public Object criCondition(String name, Integer pageIndex, Integer pageCount) {
List<User> users = oldcriteriaService.criCondition(name, pageIndex, pageCount);
return users;
}
}
点击查看代码
package com.example.springboot_hibernate.controller;
import com.example.springboot_hibernate.model.User;
import com.example.springboot_hibernate.service.HibernateHqlService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/hql")
public class HibernateHqlController {
@Resource
private HibernateHqlService hibernateHqlService;
@RequestMapping("selectall")
public Object selectAll() {
List<User> users = hibernateHqlService.selectAll();
return users;
}
@RequestMapping("selectlike")
public Object selectLike(String name) {
List<User> users = hibernateHqlService.selectLike(name);
return users;
}
@RequestMapping("selectcondition")
public Object selectCondition(String name, Integer pageIndex, Integer pageCount) {
List<User> users = hibernateHqlService.selectCondition(name, pageIndex, pageCount);
return users;
}
@RequestMapping("update")
public Object updateUser(User user) {
boolean result = hibernateHqlService.updateUser(user);
return result;
}
@RequestMapping("selectAndUpdate")
public Object selectAndUpdate(User user) {
User user1 = hibernateHqlService.selectAndUpdate(user);
return user1;
}
}
点击查看代码
package com.example.springboot_hibernate.controller;
import com.example.springboot_hibernate.model.User;
import com.example.springboot_hibernate.service.HibernateSqlService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("sql")
public class HibernateSqlController {
@Resource
private HibernateSqlService hibernateSqlService;
@RequestMapping("selectCondition")
public Object selectAll(String name, Integer pageIndex, Integer pageCount) {
List<User> users = hibernateSqlService.selectCondition(name, pageIndex, pageCount);
return users;
}
@RequestMapping("update")
public Object updateUser(User user) {
boolean result = hibernateSqlService.updateUser(user);
return result;
}
}