标签:测试方法 com cat connect aop key manager ons mod
第一步:导包
写一个映射文件以及实体类
package oyb.domain; public class User { private Integer id; private String username; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", username=‘" + username + ‘\‘‘ + ", age=" + 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 package="oyb.domain"> <class name="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>
dao层
package oyb.dao; import oyb.domain.User; public interface IUserDao { public void save(User user); }
package oyb.dao.impl; import org.springframework.orm.hibernate3.HibernateTemplate; import oyb.dao.IUserDao; import oyb.domain.User; public class UserDaoImpl implements IUserDao { //停供set方法由spring注入 private HibernateTemplate hibernateTemplate; public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } @Override public void save(User user) { this.hibernateTemplate.save(user); } }
service层
package oyb.service; import oyb.domain.User; public interface IUserService { public void save(User user); }
package oyb.service.impl; import oyb.dao.IUserDao; import oyb.domain.User; import oyb.service.IUserService; public class UserService implements IUserService { private IUserDao userDao; public void setUserDao(IUserDao userDao) { this.userDao = userDao; } @Override public void save(User user) { userDao.save(user); } }
hibernate的配置文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 1、配置数据库连接的4个参数 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sshdemo</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <!-- 2、是否显示sql语句 --> <property name="show_sql">true</property> <!-- 3、是否格式化sql语句 --> <property name="format_sql">true</property> <!-- 4、Hiberante映射与DDl语句的策略 update【常用】: 如果数据库没有表,会创建表 hibernate.--> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 配置JavaBean与表的映射文件 --> <mapping resource="oyb/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!--xmlns xml namespace:xml命名空间--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p ="http://www.springframework.org/schema/p" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userDao" class="oyb.dao.impl.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <bean id="userService" class="oyb.service.impl.UserService"> <property name="userDao" ref="userDao"></property> </bean> </beans>
单元测试类
package oyb.test; 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 oyb.domain.User; import oyb.service.IUserService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class test { @Autowired private IUserService userService; @Test public void test(){ User user = new User(); user.setUsername("oyb"); userService.save(user); } }
运行之后的结果:
但是查看数据库并没有数据
原因是因为保存用户是要提交事务的,所以还需在Spring里配置hibernate的事务管理器
在applicationContxt配置事务
<!--配置事务--> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--配置事务详情--> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save"/> </tx:attributes> </tx:advice> <!--AOP编程--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* oyb.service..*.*(..))"></aop:advisor> </aop:config>
运行测试方法
查看数据库,已经存入了数据
简化hibernate的配置文件,在applicationContext中配置hibernate.cfg.xml,修改applicationContext.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <!--xmlns xml namespace:xml命名空间--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p ="http://www.springframework.org/schema/p" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--dataSource--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sshdemo"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>--> <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="oyb/domain/*.hbm.xml"/> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userDao" class="oyb.dao.impl.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <bean id="userService" class="oyb.service.impl.UserService"> <property name="userDao" ref="userDao"></property> </bean> <!--配置事务--> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--配置事务详情--> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save"/> </tx:attributes> </tx:advice> <!--AOP编程--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* oyb.service..*.*(..))"></aop:advisor> </aop:config> </beans>
运行测试类:
整合Struts
编写UserAction
package oyb.action; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import oyb.domain.User; import oyb.service.IUserService; public class UserAction extends ActionSupport implements ModelDriven<User> { private User user =new User(); //默认会根据名称自动注入 /** * 在action中使用Service注意: * 1.不需要在spring的配置文件中注入service * 2.会根据spring中的id的名字注入到当前属性中 * 3.在action中service要提供set方法 */ private IUserService userService; public void setUserService(IUserService userService) { this.userService = userService; } public String save(){ //实际开发中这里接收参数 System.out.println(user); userService.save(user); return SUCCESS; } @Override public User getModel() { return user; } }
配置Struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true"/> <package name="default" namespace="/" extends="struts-default"> <action name="user_*" class="oyb.action.UserAction" method="{1}"> <result name="success">/success.jsp</result> </action> </package> </struts>
测试结果
标签:测试方法 com cat connect aop key manager ons mod
原文地址:https://www.cnblogs.com/ouyangbo/p/10620469.html