一、正向工程
所谓的逆向工程是由数据库表生成实体类,而正向工程与之对应,指的是由实体类生成数据库表。
Spring-jpa.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<context:component-scan base-package="com.qianfeng.service"/>
<context:component-scan base-package="com.qianfeng.dao"/>
<!--配置druid数据源-->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${url}"/>
<property name="driverClassName" value="${driver}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pass}"/>
</bean>
<!--配置适配器,用来设置数据库方言以及是否需要显示SQL语句-->
<bean id="adapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!--设置数据库方言MySQL8,同时还设置了数据库引擎innoDB-->
<property name="databasePlatform" value="org.hibernate.dialect.MySQL8Dialect"/>
<property name="showSql" value="true"/>
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="ds"/>
<property name="packagesToScan" value="com.qianfeng.entity"/>
<property name="jpaVendorAdapter" ref="adapter"/>
<property name="jpaProperties">
<props>
<!--设置是否格式化sql语句-->
<prop key="hibernate.format_sql">true</prop>
<!--设置hbm to ddl自动创建数据库表,值有四个:update、create、create-drop、validate-->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!--配置JPA的事务管理器-->
<bean id="jtx" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<!--配置事务的注解驱动-->
<tx:annotation-driven proxy-target-class="false" transaction-manager="jtx"/>
<jpa:repositories base-package="com.qianfeng.dao" entity-manager-factory-ref="emf" transaction-manager-ref="jtx"/>
</beans>
<prop key="hibernate.hbm2ddl.auto">update</prop>的作用是帮我们创建数据库。除了update还有三个值:validate:加载hibernate时,验证创建数据库表结构
create:每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop:加载hibernate时创建,退出时删除表结构
update:加载hibernate自动更新数据库结构
实际开发中一般常用validate与update。
默认创建的数据库引擎是MyISAM,不支持事务,所以会创建失败,解决办法如下:
<property name="databasePlatform" value="org.hibernate.dialect.MySQL8Dialect"/>
二、条件查询
JPA给我们封装好了数据库查询的很多方法,不需要我们手动书写sql语句,但是方法名有一定的规范:
IEmloyeeDao
package com.qianfeng.dao;
import com.qianfeng.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.io.Serializable;
import java.util.List;
@Repository
public interface IEmployeeDao extends JpaRepository<Employee, Serializable> {
List<Employee> findByEmpName(String empName);
List<Employee> findByEmpNameOrSalary(String empName,double salary);
List<Employee> findByEmpNameLike(String empName);
}
方法名findBy后面是必须跟属性名相同,第一个属性的字母不区分大小写,后面的单词首字母要区分大小写。可以添加的关键字有And、Or、Like、GreaterThan、LessThan、Between、OrderBy、Asc、Desc、In等。使用模糊查询Like需要在字符串前后都拼接%。
package com.qianfeng;
import com.qianfeng.entity.Employee;
import com.qianfeng.service.IEmployeeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-jpa.xml")
public class TestEmployee {
@Resource
private IEmployeeService employeeService;
@Test
public void testSaveEmpUpdate(){
Employee e = new Employee();
e.setEmpName("王五");
e.setSalary(3000);
Employee employee = employeeService.saveEmployee(e);
System.out.println(employee);
}
@Test
public void testSaveEmpCreate(){
Employee e = new Employee();
e.setEmpName("张三");
e.setSalary(2000);
Employee employee = employeeService.saveEmployee(e);
System.out.println(employee);
}
@Test
public void testSaveEmpCreateAndDrop(){
Employee e = new Employee();
e.setEmpName("张三");
e.setSalary(2000);
Employee employee = employeeService.saveEmployee(e);
System.out.println(employee);
}
@Test
public void testFindByEmpName(){
List<Employee> employees = employeeService.findByEmpName("张三");
for (Employee employee : employees) {
System.out.println(employee);
}
}
@Test
public void testFindByEmpNameOrSalary(){
List<Employee> employees = employeeService.findByEmpNameAndSalary("张三",2000);
for (Employee employee : employees) {
System.out.println(employee);
}
}
@Test
public void testFindAllByPageable(){
Pageable page = PageRequest.of(0,2);
Page<Employee> employees = employeeService.findAll(page);
List<Employee> content = employees.getContent();
for (Employee employee : content) {
System.out.println(employee);
}
}
@Test
public void testFindByEmpNameLike(){
List<Employee> employees = employeeService.findByEmpNameLike("%".concat("三").concat("%"));
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
分页查询首先得到的结果是一个Page集合,然后返回的content是List集合。












网友评论