接上篇 在上篇文章中我們說過:
ssh整合的關系是:sPRing整合hibernate,struts 整合spring。
在這里我們首先實現第一個關系,spring整合hibernate. 下面用一個例子來敘述。 實現的業務是客戶的注冊功能。
sql代碼:創建一個表,共四個字段。主鍵,用戶名,密碼和年齡。
create table t_user( id int primary key auto_increment, username varchar(50), passWord varchar(32), age int );
四個字段加對應的getter和setter方法
public class User { private Integer id; private String username; private String password; private Integer age;映射文件<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="com.scx.domain.User" table="t_user"> <id name="id"> <generator class="native"></generator> </id> <property name="username"></property> <property name="password"></property> <property name="age"></property> </class></hibernate-mapping>spring提供 HibernateTemplate 用于操作PO對象,類似Hibernate session對象。
public class UserDaoImpl implements UserDao{ //需要spring注入模版 private HibernateTemplate hibernateTemplate; public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } public void save(User user) { hibernateTemplate.save(user); }}只有一個用戶的注冊業務
public class UserServiceImpl implements UserService{ private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } //用戶注冊 public void register(User user) { userDao.save(user); }}最重要的部分就在這里了、 在前面的dao層中我們說了hibernateTemplate需要spring注入。通過學習hibernate我們知道而hibernateTemplate需要從session工廠獲得。所以首先要有一個session工廠bean.session工廠bean要獲得的hibernate.cfg.xml可以通過configLocation參數來設置。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 1.加載hibernate.cfg.xml 配置sessionfactory工廠 *configLocation確定配置文件的位置 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!-- 2.創建模版 *底層使用session,session由sessionfactory獲得 --> <bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 3.dao --> <bean name="userDao" class="com.scx.dao.impl.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- 4.service --> <bean name="userService" class="com.scx.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <!-- 5.1事務管理 HibernateTransactionManager --> <bean name="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 5.2事務詳情 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="register"/> </tx:attributes> </tx:advice> <!-- 5.3aop編程 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.scx.service..*.*(..))"/> </aop:config></beans>使用整合junit的方法進行測試不明白請看這里
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations= "classpath:applicationcontext.xml")public class TestApp { @Autowired private UserService userService; @Test public void test(){ User user=new User(); user.setAge(18); user.setUsername("張三"); user.setPassword("123456"); userService.register(user); }}運行結果:
控制臺成功輸出sql語句。查看數據庫。
結果正確。 到目前完成了spring整合hibernate。
新聞熱點
疑難解答