1. pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.3.Final</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>9.0.2.0.0</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
2.spring-orm.xml
<!-- 配置EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="test" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"></property>
</bean>
<!-- 配置jpa的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Hibernate对Jpa的实现 -->
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
3. 在工程的src目录下新建META-INF文件夹, 在此文件夹中新建persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@144.131.254.240:1521:nbcsora" />
<property name="hibernate.connection.username" value="papp" />
<property name="hibernate.connection.password" value="papp" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="javax.persistence.validation.mode" value="none" />
</properties>
</persistence-unit>
</persistence
4. UserDao
public interface UserDao {
public User save(User user);
public User get(Long id);
public List query();
}
- UserDaoImpl
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.ecs.model.User;
@Repository
public class UserDaoImpl implements UserDao {
@PersistenceContext(unitName="test")
private EntityManager entityManager;
@Transactional(rollbackFor=Exception.class)
public User save(User user){
entityManager.persist(user);
return null;
}
@Transactional(readOnly=true)
public User get(Long id){
User user = entityManager.find(User.class, id);
return user;
}
@Transactional(readOnly=true)
public List query(){
Query query = entityManager.createNativeQuery("select * from test_user");
List list = query.getResultList();
return list;
}
}
5.springmvc配置中引入配置文件
<context:component-scan base-package="com.ecs.*" />
<import resource="classpath:/jpa/spring-orm.xml"/>
6.测试
public class TestMain {
private ClassPathXmlApplicationContext context = null;
@Before
public void before() {
String configLocation[] = { "classpath*:servlet-context.xml" };
context = new ClassPathXmlApplicationContext(configLocation);
context.start();
}
// @Test
public void save() {
UserDao userService = (UserDao) context.getBean("userDaoImpl");
User user = new User();
user.setName("张三");
user.setAddress("李四");
userService.save(user);
}
// @Test
public void get() {
UserDao userService = (UserDao) context.getBean("userDaoImpl");
User user = userService.get(1190L);
System.out.println(user.getId());
}
@Test
public void query() {
UserDao userService = (UserDao) context.getBean("userDaoImpl");
List list = userService.query();
Map map = new HashMap();
map.put("total", list.size());
map.put("list", list);
System.out.println(JSONObject.toJSONString(map));
}
}