The support that spring provides to JPA

Source: Internet
Author: User

JPA itself works, but spring also provides support for it.

As opposed to JPA's own work, the amount of code that spring supports is simplified, allowing developers to focus on their business logic

First, Applicationcontext.xml this spring's configuration file,

Content is

<?xml version= "1.0" encoding= "UTF-8"?>
<!---Dispatcherservlet application context for Petclinic ' s Web tier. -->
<beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:p= "http://www.springframework.org/schema/p" 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"
Xmlns= "Http://www.springframework.org/schema/beans"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " >


<context:component-scan base-package= "Org.test"/>


<tx:annotation-driven transaction-manager= "TransactionManager"/>




<bean id= "Entitymanagerfactory"
class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
</bean>


<bean id= "TransactionManager" class= "Org.springframework.orm.jpa.JpaTransactionManager" >
<property name= "Entitymanagerfactory" ref= "Entitymanagerfactory"/>
</bean>


</beans>

Looking at our Student.java, this pojo.

--------------------------------------------------------------------------------------------------------------- ----------------


Package org.test.domain;


Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;
Import Javax.persistence.Id;
Import javax.persistence.Table;


@Entity
@Table (name= "student")
public class Student
{
private int id;

private String name;

Public Student ()
{

}

Public Student (String name)
{
THIS.name = name;
}


@Id
@GeneratedValue (Strategy=generationtype.auto)
public int getId ()
{
return ID;
}


public void setId (int id)
{
This.id = ID;
}


Public String GetName ()
{
return name;
}


public void SetName (String name)
{
THIS.name = name;
}

}

For JPA's core configuration files, Persistence.xml still cannot be omitted.

The contents are as follows

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------

<?xml version= "1.0" encoding= "UTF-8"?>
<persistence xmlns= "http://java.sun.com/xml/ns/persistence" version= "2.0" >
<persistence-unit name= "Jpatest" transaction-type= "resource_local" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>


<class>org.test.domain.Student</class>
<properties>
<property name= "Hibernate.connection.driver_class" value= "Com.mysql.jdbc.Driver"/>
<property name= "Hibernate.connection.url" value= "Jdbc:mysql://localhost:3306/test"/>
<property name= "Hibernate.connection.username" value= "root"/>
<property name= "Hibernate.connection.password" value= ""/>
<property name= "Hibernate.dialect" value= "Org.hibernate.dialect.MySQL5Dialect"/>
<property name= "Hibernate.show_sql" value= "true"/>
<property name= "Hibernate.format_sql" value= "true"/>
<property name= "Hibernate.use_sql_comments" value= "false"/>
<property name= "Hibernate.hbm2ddl.auto" value= "Update"/>
</properties>
</persistence-unit>
</persistence>

--------------------------------------------------------------------------------------------------------------- --------------------------------------------

First look at the DAO layer Studentdao.java

Package Org.test.dao;


Import org.test.domain.Student;


public interface Studentdao
{
void Store (Student Student);
}


--------------------------------------------------------------------------------------------------------------- --------------------------------------------

Then look at the implementation class of Studentdao Studentdaoimpl.java

Package Org.test.dao;


Import Javax.persistence.EntityManager;
Import Javax.persistence.PersistenceContext;
Import javax.transaction.Transactional;


Import Org.springframework.stereotype.Repository;
Import org.test.domain.Student;


@Repository ("Studentdao")
public class Studentdaoimpl implements Studentdao
{


@PersistenceContext
Private Entitymanager em;

Public Entitymanager Getem ()
{
return em;
}
public void Setem (Entitymanager em)
{
This.em = em;
}
@Override

@Transactional
public void Store (Student Student)
{
Em.persist (student);
}


}

--------------------------------------------------------------------------------------------------------------- --------------------------------------------

Look at the realization of the Studentservice.java of service layer again

Package Org.test.servie;


Import org.test.domain.Student;


public interface Studentservice
{
void Storeservice (Student Student);
}

--------------------------------------------------------------------------------------------------------------- --------------------------------------------

And then the realization of Studentserviceimpl.java.

Package Org.test.servie;


Import javax.transaction.Transactional;


Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;
Import Org.test.dao.StudentDao;
Import org.test.domain.Student;


@Service ("Studentservice")
public class Studentserviceimpl implements Studentservice
{


@Autowired
Private Studentdao Studentdao;

@Transactional
@Override
public void Storeservice (Student Student)
{
Studentdao.store (student);
}

}

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------

Finally writing a test class


Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import org.test.domain.Student;
Import Org.test.servie.StudentService;


public class Main
{
public static void Main (string[] args)
{
ApplicationContext ac = new Classpathxmlapplicationcontext ("Applicationcontext.xml");

Studentservice Studentservice = Ac.getbean ("Studentservice", Studentservice.class);

Studentservice.storeservice (New Student ("Sunwukong"));
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.