67,513
社区成员
发帖
与我相关
我的任务
分享
package test;
import www.am8.manage.TestManageImpl;
public class SSHTest {
public static void main(String args[])
{
TestManageImpl tmi=new TestManageImpl();
tmi.shiwuSaveTest();
}
}
package www.am8.manage;
import java.util.Calendar;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import www.am8.iface.TestManage;
import www.am8.pojo.Test;
import www.am8.pojo.TestDAO;
public class TestManageImpl implements TestManage {
private TestDAO iTestDAO;
public TestDAO getiTestDAO() {
return iTestDAO;
}
public void setiTestDAO(TestDAO iTestDAO) {
this.iTestDAO = iTestDAO;
}
public void shiwuSaveTest() {
Resource res =new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(res);
//从BeanFactory获取UserDAO
TestDAO testDAO = (TestDAO) factory.getBean("TestDAO");
Test t = new Test();
Calendar c = Calendar.getInstance();
t.setTestId(21);
t.setShiJian(c.getTime());
t.setVar2("中文!!");
t.setChangZhengShu(23551);
t.setShiJian2("");
testDAO.save(t);
//testDAO.getHibernateTemplate().getSessionFactory().close();
}
}
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="TestDAO" class="www.am8.pojo.TestDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="TestManageImpl" class="www.am8.manage.TestManageImpl">
<property name="iTestDAO">
<ref bean="TestDAO" />
</property>
</bean>
<bean id="spring" class="www.am8.springUtil.BeansUtil" />
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!--事务传播特性为required -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置那些类的方法进行事务管理,当前com.sy.crm.service包中的子包,
类中所有方法需要,还需要参考tx:advice的设置 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* www.am8.manage.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
</beans>
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- bonecp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.1.3:1521:am8</value>
</property>
<property name="username">
<value>am8</value>
</property>
<property name="password">
<value>english</value>
</property>
<property name="initialSize">
<value>0</value>
</property>
<property name="maxActive">
<value>30</value>
</property>
<property name="maxIdle">
<value>10</value>
</property>
<property name="defaultAutoCommit">
<value>true</value>
</property>
</bean>
<!-- 配置sessionFactory, 为Hibernate配置属性 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config >
<aop:pointcut id="allManagerMethod" expression="execution(* www.am8.manage.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
<bean id="TestManageImpl" class="www.am8.manage.TestManageImpl">
<property name="iTestDAO">
<ref bean="TestDAO" />
</property>
</bean>
<bean id="TestDAO" class="www.am8.pojo.TestDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="GuanLianDAO" class="www.am8.pojo.GuanLianDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">jdbc:oracle:thin:@192.168.1.3:1521:am8</property>
<property name="connection.username">am8</property>
<property name="connection.password">english</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="defaultAutoCommit">true</property>
<property name="myeclipse.connection.profile">8amtest</property>
<mapping resource="www/am8/pojo/Test.hbm.xml" />
</session-factory>
</hibernate-configuration>