Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate JPA instance in Spring Framework

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to integrate JPA instances in the Spring framework. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Hibernate 3.2supports JPA annotations, and Spring integration with JPA is optional.

The entity class MyUser uses JPA annotations to map to the database table myUser, as shown below:

Package org.shirdrn.entity;import java.util.Date;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Transient;@Entitypublic class MyUser {private Long id;private String userName;private String password;private String gender;private Integer age;private Integer birthYear;private String addr;private String email;@Id@GeneratedValue (strategy = GenerationType.IDENTITY) public Long getId () {return id;} public void setId (Long id) {this.id = id @ Column (name= "userName") public String getUserName () {return userName;} public void setUserName (String userName) {this.userName = userName;} @ Column (name= "password") public String getPassword () {return password;} public void setPassword (String password) {this.password = password;} @ Column (name= "gender") public String getGender () {return gender;} public void setGender (String gender) {this.gender = gender;} @ Column (name= "age") public Integer getAge () {return age } public void setAge (Integer age) {this.age = age;} @ Column (name= "addr") public String getAddr () {return addr;} public void setAddr (String addr) {this.addr = addr;} @ Column (name= "email") public String getEmail () {return email;} public void setEmail (String email) {this.email = email;} @ Transientpublic Integer getBirthYear () {return new Integer (2008-age);} public void setBirthYear (Integer age) {this.birthYear = new Integer (2008-age);}}

Where birthYear is not a field in the database, using the @ Transient annotation of JPA, this member will be ignored when mapping.

The DAO APIs of the persistence layer are as follows:

Package org.shirdrn.dao;import java.util.List;import org.shirdrn.entity.MyUser;import org.springframework.transaction.annotation.Transactional;@Transactionalpublic interface MyUserDAO {public void createMyUser (MyUser myUser); public void deleteMyUser (MyUser myUser); public void updateMyUser (MyUser myUser); public List

Here, JPA annotations are used to declare transactions.

The DAO implementation class, as follows:

Package org.shirdrn.dao.impl;import java.util.List;import org.shirdrn.dao.MyUserDAO;import org.shirdrn.entity.MyUser;import org.springframework.orm.jpa.support.JpaDaoSupport;public class MyUserDAOImpl extends JpaDaoSupport implements MyUserDAO {public void createMyUser (MyUser myUser) {getJpaTemplate () .persist (myUser);} public void deleteMyUser (MyUser myUser) {MyUser dbMyUser = getJpaTemplate () .find (MyUser.class, myUser.getId ()); getJpaTemplate () .remove (dbMyUser) } public void updateMyUser (MyUser myUser) {MyUser dbMyUser = getJpaTemplate (). Find (MyUser.class, myUser.getId ()); if (myUser.getUserName ()! = null) {dbMyUser.setUserName (myUser.getUserName ());} if (myUser.getAddr ()! = null) {dbMyUser.setAddr (myUser.getAddr ());} getJpaTemplate (). Merge (dbMyUser);} @ SuppressWarnings ("unchecked") public List@SuppressWarnings ("unchecked") public List

Because it inherits JpaDaoSupport, the index needs to get a JpaTemplate to access the database, and inject an org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean into the configuration file of Spring.

The contents of the configuration file applicationContext.xml for Spring are as follows:

Under the META-INF directory, add a persistence.xml configuration file, as shown below:

Org.hibernate.ejb.HibernatePersistence

Org.shirdrn.entity.MyUser

Then, test the add, delete, modify and search operation, as follows:

Insert record:

Packageorg.shirdrn.test;importorg.shirdrn.dao.MyUserDAO;importorg.shirdrn.entity.MyUser;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassTestCreateMyUser {publicstaticvoidmain (String [] args) {MyUserDAOmyUserDAO= (MyUserDAO) ctx.getBean ("myUserDAOImpl"); MyUsermyUser=newMyUser (); myUser.setUserName ("JohnXa"); myUser.setPassword ("123456"); myUser.setGender ("male"); myUser.setAge (newInteger (25)); myUser.setAddr ("NewYork"); myUser.setEmail ("john@hotmail.com") MyUserDAO.createMyUser (myUser);}}

Delete record:

Packageorg.shirdrn.test;importorg.shirdrn.dao.MyUserDAO;importorg.shirdrn.entity.MyUser;importorg.springframework.context.ApplicationContext;importorg.springframework.ClassPathXmlApplicationContext;publicclassTestDeleteMyUser {publicstaticvoidmain (String [] args) {ApplicationContextctx=newClassPathXml ("applicationContext.xml"); MyUserDAOmyUserDAO= (MyUserDAO) ctx.getBean ("myUserDAOImpl"); MyUsermyUser=newMyUser (); myUser.setId (newLong (29)); myUserDAO.deleteMyUser (myUser)

Modify the record:

Packageorg.shirdrn.test;importorg.shirdrn.dao.MyUserDAO;importorg.shirdrn.entity.MyUser;importorg.springframework.context.ApplicationContext;importorg.springframework.context.ClassPathXmlApplicationContext

PublicclassTestUpdateMyUser {

Publicstaticvoidmain (String [] args) {

ApplicationContextctx=newClassPathXmlApplicationContext ("applicationContext.xml")

MyUserDAOmyUserDAO= (MyUserDAO) ctx.getBean ("myUserDAOImpl")

MyUsermyUser=newMyUser ()

MyUser.setId (newLong (28))

MyUser.setAddr ("Beijing")

MyUserDAO.updateMyUser (myUser)

}

}

Query record:

Packageorg.shirdrn.test;importjava.util.List;importorg.shirdrn.dao.MyUserDAO;importorg.shirdrn.entity.MyUser;importorg.springframework.context.ApplicationContext;importorg.springframework.context.ClassPathXmlApplicationContext;publicclassTestQueryAllMyUser {publicstaticvoidmain (String [] args) {ApplicationContextctx=newClassPathXmContext ("applicationContext.xml"); MyUserDAOmyUserDAO= (MyUserDAO) ctx.getBean ("myUserDAOImpl") This is the end of List's article on "how to integrate JPA instances in Spring framework". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report