整合步骤一、加入jar包,这个大家都会。一般加jar的规则
整合步骤二、在spring里面配置连接dbcp连接池,这一块要用到
commons-collections.jar
commons-logging-1.0.4.jar
commons-dbcp.jar
commons-pool.jar
下面来看一下spring配置文件里面是怎么样来进行配置的:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=wangying" /> <property name="username" value="sa" /> <property name="password" value="bbshop" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1" /> <!-- 连接池的最大值 --> <property name="maxActive" value="500" /> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2" /> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1" /> </bean>
整合步骤三、配置hibernate的sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="myDataSource" /> <property name="packagesToScan"> <list> <value>com.lushuifa.po</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean>
利用packagesToScan来扫描实体bean的位置,这里用到的是AnnotationSessionFactoryBean是注解的方式。 annotatedClasses也可以,但是要精确到某一个特定的类。
整合步骤四、配置hibernate的事务
<!-- 配置事务管理器 --> <bean id="hibernateTransaction" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 提供事务的annotation支持 --> <tx:annotation-driven transaction-manager="hibernateTransaction" />
整合步骤五、提供测试po类
package com.lushuifa.po; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_users_lushuifa") public class Users implements Serializable { private static final long serialVersionUID = 1L; private int id; private String userName; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
整合步骤六、提供测试dao
dao接口 package com.lushuifa.dao; import java.util.List; import com.lushuifa.po.Users; public interface UserDao { /** * 添加用户 * @param user */ public void addUsers(Users user); public void delUsers(int id); public List<Users> getUserList(); public Users findUserById(int id); } dao实现 package com.lushuifa.dao; import java.util.List; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.josql.Query; import org.josql.QueryExecutionException; import org.josql.QueryParseException; import org.springframework.transaction.annotation.Transactional; import com.lushuifa.po.Users; @Transactional public class UserDaoImpl implements UserDao { @Resource private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } /* (non-Javadoc) * @see com.lushuifa.dao.UserDao#addUsers(com.lushuifa.po.Users) */ public void addUsers(Users user) { try { this.sessionFactory.getCurrentSession().persist(user); } catch (Exception e) { e.printStackTrace(); } } /* (non-Javadoc) * @see com.lushuifa.dao.UserDao#delUsers(int) */ public void delUsers(int id) { try { this.sessionFactory.getCurrentSession().delete(this.findUserById(id)); } catch (Exception e) { e.printStackTrace(); } } /* (non-Javadoc) * @see com.lushuifa.dao.UserDao#getUserList() */ public List<Users> getUserList() { List<Users> userList = this.sessionFactory.getCurrentSession().createQuery("from Users").list(); return userList; } /* (non-Javadoc) * @see com.lushuifa.dao.UserDao#findUserById(int) */ public Users findUserById(int id) { Users user = (Users) this.sessionFactory.getCurrentSession().load(Users.class, 1); return user; } /* (non-Javadoc) * @see com.lushuifa.dao.UserDao#findUserForJosql(int) */ public List findUserForJosql(int userid){ Query q = new Query(); List user = null; try { q.parse("select * from Users"); q.setVariable("userid", userid); user =q.execute(this.getUserList()).getResults(); } catch (QueryParseException e) { e.printStackTrace(); } catch (QueryExecutionException e) { e.printStackTrace(); } return user; } public static void main(String[] args) { System.out.println(new UserDaoImpl()); } }
整合步骤六、提供junit类
package com.lushuifa.dao; import javax.annotation.Resource; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lushuifa.po.Users; public class UserDaoTest { private UserDao userDao = getUserDao(); private UserDao getUserDao() { ApplicationContext ac = new ClassPathXmlApplicationContext("spring_lushuifa.xml"); userDao = (UserDao) ac.getBean("userDao"); return userDao; } @Test public void testAddUsers() { Users user = new Users(); user.setUserName("标标网"); userDao.addUsers(user); System.out.println("添加成功"); } @Test public void testDelUsers() { userDao.delUsers(1); System.out.println("删除成功!"); } @Test public void testJosql() { userDao.findUserForJosql(2); } }
到此结束