三大框架-ssh整合(二)
spring整合hibernate--没有产生hibernate.cfg.xml文件,而是将其中的内容放在了applicationContext.xml中
applicationContext.xml文件内容如下:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 1.1加载properties文件 --> <!-- 1.2 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///ee19_spring_day03"></property> <property name="user" value="root"></property> <property name="password" value="123"></property> </bean> <!-- 1.3配置 LocalSessionFactoryBean,获得SessionFactory * configLocation确定配置文件位置 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 1)dataSource 数据源 2)hibernateProperties hibernate其他配置项 3) 导入映射文件 mappingLocations ,确定映射文件位置,需要“classpath:” ,支持通配符 【】 <property name="mappingLocations" value="classpath:com/itheima/domain/User.hbm.xml"></property> <property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property> mappingResources ,加载执行映射文件,从src下开始 。不支持通配符* <property name="mappingResources" value="com/itheima/domain/User.hbm.xml"></property> mappingDirectoryLocations ,加载指定目录下的,所有配置文件 <property name="mappingDirectoryLocations" value="classpath:com/itheima/domain/"></property> mappingJarLocations , 从jar包中获得映射文件 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.current_session_context_class">thread</prop> </props> </property> <property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property> </bean> <!-- 3 dao --> <bean id="userDao" class="com.itheima.UserDao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 4 service --> <bean id="userService" class="com.itheima.UserService.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <!-- 5 事务管理 --> <!-- 5.1 事务管理器 :HibernateTransactionManager --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 5.2 事务详情 ,给ABC进行具体事务设置 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="register"/> </tx:attributes> </tx:advice> <!-- 5.3 AOP编程,ABCD 筛选 ABC --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service..*.*(..))"/> </aop:config> </beans>
下面是domain类,UserDao类,UserService类,以及测试类的实现代码
首先User类也即bean先省略;
UserDao类如下所示:
import com.itheima.domain.User; public interface UserDao { public void save(User user); }
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.itheima.domain.User; // 底层需要SessionFactory,自动创建HibernateTemplate模板 public class UserDaoImpl extends HibernateDaoSupport implements UserDao { @Override public void save(User user) { this.getHibernateTemplate().save(user); } }
UserService类
import com.itheima.domain.User; public interface UserService { public void register(User user); }
import com.itheima.UserDao.UserDao; import com.itheima.domain.User; public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void register(User user) { // TODO Auto-generated method stub userDao.save(user); } }
测试类:
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.itheima.UserService.UserService; import com.itheima.domain.User; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:applicationContext.xml") public class TestApp { @Autowired private UserService userService; @Test public void demo01(){ User user = new User(); user.setUsername("jack"); user.setPassword("1234"); user.setAge(18); userService.register(user); } }
注:之前导入的39个.jar文件需要导入进去,同时需要创建一个和user类属性相同的t_user表
CREATE TABLE t_user( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50), PASSWORD VARCHAR(32), age INT );
准备工作准备完成即可进行测试。