创建项目,添加依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.hibernate.version>5.0.7.Final</project.hibernate.version> </properties> <dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- hibernate对jpa的支持包 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${project.hibernate.version}</version> </dependency> <!-- c3p0 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>${project.hibernate.version}</version> </dependency> <!-- log日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- Mysql and MariaDB --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies>
在resources文件夹下创建文件夹META-INF
然后在META-INF下创建persistence.xml 并进行配置
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <!--配置持久化单元 name:持久化单元名称 transaction-type:事务类型 RESOURCE_LOCAL:本地事务管理 JTA:分布式事务管理 --> <persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL"> <!--配置JPA规范的服务提供商 --> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <properties> <!-- 数据库驱动 --> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <!-- 数据库地址 --> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa" /> <!-- 数据库用户名 --> <property name="javax.persistence.jdbc.user" value="root" /> <!-- 数据库密码 --> <property name="javax.persistence.jdbc.password" value="root" /> <!--jpa提供者的可选配置:我们的JPA规范的提供者为hibernate,所以jpa的核心配置中兼容hibernate的配 --> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit> </persistence>
创建实体类,并添加注解
import javax.persistence.*; @Entity//作用:指定当前类是实体类 @Table(name = "cst_customer")//指定实体类和表之间的对应关系 public class Customer { @Id//声明当前私有属性为主键 //配置主键的生成策略 //GenerationType.IDENTITY mysql使用 //GenerationType.SEQUENCE orcale使用 @GeneratedValue(strategy= GenerationType.SEQUENCE) @Column(name="cust_id") private Long custId; @Column(name = "cust_name") private String custName; @Column(name = "cust_Source") private String custSource; @Column(name = "cust_industry") private String custIndustry; @Column(name = "cust_level") private String custLevel; @Column(name = "cust_address") private String custAddress; @Column(name = "cust_phone") private String custPhone; public Long getCustId() { return custId; } public void setCustId(Long custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustIndustry() { return custIndustry; } public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } }
之后就可以开始测试了
@Test public void run1(){ //创建实体管理类工厂 EntityManagerFactory myJpa = Persistence.createEntityManagerFactory("myJpa"); //创建实体管理类 EntityManager entityManager = myJpa.createEntityManager(); //获取事务对象 EntityTransaction transaction = entityManager.getTransaction(); //开启事务 transaction.begin(); //模拟获取数据 Customer customer = new Customer(); customer.setCustName("**"); customer.setCustLevel("**"); customer.setCustAddress("***"); //执行保存 entityManager.persist(customer); //提交事务 transaction.commit(); //关闭资源 entityManager.close(); myJpa.close(); }
附加 Jpql语句
查询所有
From user
查询所有并倒序排列
From user order by userId desc
统计查询
Select count(id) from user
分页查询
String jpql = “From user”;
Query query = entityManager.createQuery(jpql);
query.setFirstResult(0);//开始的索引
query.setMaxResults(5);//每页显示的条数
模糊查询
String jpql = “From user where username like ?”;
Query query = entityManager.createQuery(jpql);
query.setParameter(1,"%虾%");