这三天都在搞使用JPA注解替代Hibernate的映射文件,因为包缺少或冲突的问题,总是不成功,今天终于搞定了,特地写下来分享一下(^-^)。
开发工具:myeclipse7.5
开发环境:JDK1.6,MySQL5.0
使用到框架:spring+Hibernate
(1)使用到的hibernate库类:hibernate-distribution-3.3.1.GA-dist.zip(在解压缩的hibernate-distribution-3.3.1.GA目录中,把hibernate.jar,和librequired下的所用jar包引进到我们项目的lib目录下)、slf4j- log4j12.jar、Hibernate3.2 Annotations& Entity Manager 以及 Hibernate3.2 Core Liabaries。
添加链接mysql数据库所需要的包:commons-dbcp.jar、commons-pool.jar、mysql-connector5.1.6.jar
(2)添加对spring的支持。
(3)使用Mysql创建数据库(我创建的数据库名为xlob,其中一个表名为user_table(userId,userName))。
(4)创建一个bean(User)
package beans; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity(name="user_table") public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) int userId=0; @Column(name="userName") String userName=""; /** * @return the userId */ public int getUserId() { return userId; } /** * @return the userName */ public String getUserName() { return userName; } /** * @param userId the userId to set */ public void setUserId(int userId) { this.userId = userId; } /** * @param userName the userName to set */ public void setUserName(String userName) { this.userName = userName; } }
(5)创建dao(UserDao)
import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import beans.User; public class UserDao extends HibernateDaoSupport { public UserDao() { // TODO Auto-generated constructor stub } public User getUser(int id){ return (User)getHibernateTemplate().get(User.class, id); } public void saveUser(User user) { getHibernateTemplate().save(user); } public List<User>getUserByName(String name){ return getHibernateTemplate().find("from beans.User where user_name=? ",name); } }
(6)编写配置文件(为了可以重用,分成四个文件)
mysql.driverClassName=com.mysql.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/xlob mysql.username=root mysql.password=root mysql.mappingDirectoryLocations=conf/ mysql.dialect=org.hibernate.dialect.MySQLDialect
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:conf/mysqlconfig.properties </value> </list> </property> <property name="fileEncoding" value="utf-8"></property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${mysql.driverClassName}"></property> <property name="url" value="${mysql.url}"></property> <property name="username" value="${mysql.username}"></property> <property name="password" value="${mysql.username}"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="annotatedClasses"> <list> <value>beans.User</value> </list> </property> </bean> <bean id="hibernatejdbcTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="txBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> <!-- 目标为类而不是接口 --> <property name="proxyTargetClass" value="true"></property> <!-- 配置事务管理器 --> <property name="transactionManager" ref="transactionManager" /> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> </props> </property> </bean>
<bean id="userDao" class="dao.UserDao"> <property name="hibernateTemplate" ref="hibernatejdbcTemplate"></property> </bean>
(7)最后一步,检验正确性
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import dao.UserDao; import beans.User; import junit.framework.TestCase; public class UserTest extends TestCase { String[] conf={"conf/beans-config.xml","conf/mysql-config.xml","conf/tx-config.xml"}; ApplicationContext tcx=new ClassPathXmlApplicationContext(conf); UserDao userDao=(UserDao)tcx.getBean("userDao"); public void testGetUser() { User user=userDao.getUser(1); assertEquals("gaoqicheng",user.getUserName()); }
其实,如果添加到hibernate支持包正确,相信剩下的就会比较容易,很快就可以创建成功,如果缺少了必须的包或添加了相冲突的包,后果就像本人,花费了几天的精力去探索,希望可以帮到各位!