67,549
社区成员




package test;
import java.sql.SQLException;
import java.util.Date;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.surveypark.bean.User;
import com.surveypark.service.UserService;
public class TestUserService {
private static UserService us = null;
@BeforeClass
public static void iniUserService(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
us = (UserService) ac.getBean("userService");
}
@Test
public void testInsertUser() throws SQLException{
User user = new User();
user.setEmail("123@qq.com");
user.setPassword("123456");
user.setNickName("stone");
user.setName("yunxi");
user.setRegDate(new Date());
us.saveEntity(user);
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 分散配置 -->
<context:component-scan base-package="com.surveypark.dao.impl,com.surveypark.service.impl,com.surveypark.listener,com.surveypark.struts2.action" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name = "driverClass" value = "com.mysql.jdbc.Driver"/>
<property name = "jdbcUrl" value = "jdbc:mysql://localhost:3306/surveypark"/>
<property name = "user" value = "root"/>
<property name = "password" value = ""/>
<property name = "maxPoolSize" value = "10"/>
<property name = "minPoolSize" value = "2"/>
<property name = "initialPoolSize" value = "3"/>
<property name = "acquireIncrement" value = "2"/>
</bean>
<!-- 本地会话工厂bean(Spring整合hibernate的核心入口) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/surveypark/bean</value>
</list>
</property>
</bean>
<!-- hibernate事务管理器,用来在servece层面上实现事务管理,而且达到平台无关性 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事物通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 写操作 -->
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/>
<!-- 读操作 -->
<tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" />
</tx:attributes>
</tx:advice>
<!-- aop配置 -->
<aop:config>
<!-- pointcut表达式要仔细学习 -->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Service.*(..))"/>
</aop:config>
</beans>
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
</session-factory>
</hibernate-configuration>