Spring對JPA提供的支援

來源:互聯網
上載者:User

JPA 本身可以工作,但是Spring呢 也對其提供了支援。

相對於JPA自己工作,Spring支援後代碼量得到了簡化,使得開發人員專註於關係自己的商務邏輯

首先將applicationContext.xml這個Spring的設定檔給貼上來,

內容是

<?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>

在看我們的Student.java這個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;
}

}

對於JPA的核心設定檔persistence.xml 還是不能省略的

內容如下

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

<?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>

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

先看Dao層的StudentDao.java

package org.test.dao;


import org.test.domain.Student;


public interface StudentDao
{
void store(Student student);
}


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

再看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);
}


}

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

再看Service層的  StudentService.java的實現

package org.test.servie;


import org.test.domain.Student;


public interface StudentService
{
void storeService(Student student);
}

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

接著是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);
}

}

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

最後在寫一個測試類別


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"));
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.