作者:yinghccll | 来源:互联网 | 2023-05-31 18:16
1. 添加依赖Jar文件。除了必要的spring和Hibernate依赖外,下面的jar必不可少, 否则会抛出异常“No Persistence provider for EntityManager named *”
org.hibernate
hibernate-entitymanager
4.2.8.Final
2. spring-jpa-hibernate.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
3. 在类路径下创建文件夹“META-INF”, 加入文件persistence.xml。 完整的路径是 classpath:META-INF/persistence.xml, 这是jpa的默认配置文件。
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
4. Entity对象
package com.myproject.example.vo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Account {
@Id
@GeneratedValue //generated automatically
private long id;
@Column(length=20, nullable=false)
private String username;
@Column(length=20, nullable=false)
private String password;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
5.Dao文件
package com.myproject.example.dao.jpa;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import com.myproject.example.dao.AccountDao;
import com.myproject.example.vo.Account;
@Repository
public class AccountDaoBean implements AccountDao {
@PersistenceContext
EntityManager em;
@Override
public Account queryAccountById(int id) {
return em.find(Account.class, id);
}
@SuppressWarnings("unchecked")
@Override
public List queryAccount(Account account) {
Query q = em.createQuery("select a from Account a where a.username like ?");
q.setParameter(1, "%"+account.getUsername()+"%");
return q.getResultList();
}
@Override
public int updateAccount(Account account) {
em.merge(account);
return 0;
}
@Override
public int insertAccount(Account account) {
em.persist(account);
return 0;
}
@Override
public int deleteAccount(int id) {
em.remove(em.getReference(Account.class, id));
return 0;
}
}
6. Service对象包含事务管理
package com.myproject.example.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.myproject.example.dao.AccountDao;
import com.myproject.example.vo.Account;
@Service
@Transactional
public class AccountService {
@Resource
private AccountDao accountDao;
//don't need to add transaction in query method
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public Account Login(String username, String password){
System.out.println(username + " want to login.");
Account account = new Account();
account.setUsername(username);
account.setPassword(password);
List list= accountDao.queryAccount(account);
if(list.size()==1)
return list.get(0);
else
return null;
}
//in method which already has "throws", we must add rollbackFor if want to rollback for this exception,
//otherwise the method will not be rollback
@Transactional(rollbackFor=Exception.class)
public void reqister(Account account) {
accountDao.insertAccount(account);
//throw new RuntimeException("==no register");
}
}
7.web.xml
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
contextConfigLocation
classpath:spring.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springmvc
*.do
/jsp/index.jsp
openEntityManagerInViewFilter
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
openEntityManagerInViewFilter
/*
8.单元测试
@Test
public void test(){
try {
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-jpa-hibernate.xml");
AccountService accountService =(AccountService)ac.getBean("accountService");
Account account = new Account();
account.setUsername("tom");
account.setPassword("117");
accountService.reqister(account);
} catch (Exception e) {
e.printStackTrace();
}
}