2 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
public void insert(Person person)
{
Session session=this.getSession();
Transaction tran=session.beginTransaction();
session.save(person);
tran.commit();
}

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
applicationContext.xml:
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/login</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>moon/vo/Person.hbm.xml</value></list>
</property></bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="persondao" class="moon.dao.operate">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<!-- 聲明一個(gè) Hibernate 3 的 事務(wù)管理器供代理類自動(dòng)管理事務(wù)用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="persondaoProx"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<!-- 注意這個(gè)屬性, 必須為 true 使用 CGLIB 才不用強(qiáng)制編寫被代理類的接口 -->
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="target">
<ref local="persondao" />
</property>
<property name="transactionAttributes">
<props>
<!-- 這里的方法簽名可以精確到方法, 先懶惰一下全配置上 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
test類:
public class test {
public static void main(String[] args)
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Person p=new Person();
p.setId("id8457564");
p.setUsername("admin");
p.setPassword("password");
operate op=(operate)ctx.getBean("persondaoProx");
op.insert(p);
}
}
添加回答
舉報(bào)