1, Pom.xml in the introduction of relevant dependencies
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < Maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</ Maven.compiler.target> </properties> <dependencies><!--Spring data JPA dependent -<dependency> <groupId>org.springframework.data</groupId> <ARTIFACTID>SPRING-DATA-JPA </artifactId> <version>1.11.4.RELEASE</version> </dependency><!--hibernate dependency --<dependency> <groupId>org.hibernate</groupId> <ARTIFACTID>HIBERNATE-CORE</ARTIFAC tid> <version>5.1.7.Final</version> </dependency> <dependency> <groupid& Gt;org.hibernate</groupid> <artifactId>hibernate-entitymanager</artifactId> <version> ;5.1.7.final</version> </dependency><!--mysql dependency --<dependency> <groupId>mysql</groupId> <artifactid>mysql-connector-java</artifacti d> <version>5.1.42</version> </dependency> <dependency> <groupid>ju nit</groupid> <artifactId>junit</artifactId> <version>3.8.1</version> <sco Pe>test</scope> </dependency> </dependencies>
2. JPA Configuration Mate-inf/persistence.xml
<?xml version= "1.0" encoding= "UTF-8"? ><persistence version= "2.1" xmlns= "http://xmlns.jcp.org/xml/ns/ Persistence "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://xmlns.jcp.org/xml/ Ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd "> <persistence-unit name="MYJPA"Transaction-type=" resource_local "> <provider>Org.hibernate.jpa.HibernatePersistenceProvider</provider> <class>com.wyl.bean.user</class> <properties> <property name= "Javax.persistence.jdbc.driver" value= "Com.mysql.jdbc.Driver"/ > <property name= "javax.persistence.jdbc.url" value= "Jdbc:mysql://127.0.0.1:3306/test"/> &L T;property name= "Javax.persistence.jdbc.user" value= "root"/> <property name= "JAVAX.PERSISTENCE.JDBC.PASSW Ord "value=" root "/> <property name=" Hibernate.show_sql "value=" true "/> <pro Perty name= "Hibernate.hbm2ddl.auto" value= "Update"/> </properties> </persistence-unit></persis Tence>
3. Create an Entity object User.class
PackageCom.wyl.bean;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;Importjavax.persistence.Id;Importjavax.persistence.Table; @Entity @table (name= "Users") Public classUser {@Id @GeneratedValue (strategy=generationtype.identity)PrivateInteger ID; @ColumnPrivateString UserName; @ColumnPrivateString Phone; PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; } PublicString Getphone () {returnphone; } Public voidSetphone (String phone) { This. Phone =phone; } @Override PublicString toString () {//TODO auto-generated Method Stub return Super. toString (); }}
4. Create a test class
PackageCom.wyl;ImportJavax.persistence.EntityManager;Importjavax.persistence.EntityManagerFactory;Importjavax.persistence.Persistence;Importorg.junit.Test;ImportCom.wyl.bean.User; Public classJpatest {PrivateEntitymanagerfactory factory = Persistence.createentitymanagerfactory ("MYJPA"); @Test Public voidTestadd () {Entitymanager em=Factory.createentitymanager (); User User=NewUser (); User.setusername ("Wang"); User.setphone ("123422222222"); Em.gettransaction (). Begin (); //Transaction Open Try{em.persist (user); Persisted data em.gettransaction (). commit (); //Transaction Commit}Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); Em.gettransaction (). rollback (); //Transaction Rollback}finally{em.close ();//Close em}} @Test Public voidTestfind () {Entitymanager em=Factory.createentitymanager (); User User= Em.find (User.class, 1); System.out.println (User.getusername ()); Em.gettransaction (). Begin (); //Transaction Open Try{em.merge (user); Em.gettransaction (). commit (); //Transaction Commit}Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); Em.gettransaction (). rollback (); //Transaction Rollback}finally{em.close ();//Close em}} @Test Public voidTestdelete () {Entitymanager em=Factory.createentitymanager (); User User= Em.find (User.class, 1);//Query to entityem.gettransaction (). Begin ();//Transaction Open Try{em.remove (user); //Delete EntityEm.gettransaction (). commit ();//Transaction Commit}Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); Em.gettransaction (). rollback (); //Transaction Rollback}finally{em.close ();//Close em } }}
Using spring Data JPA
in hibernate