--------------------------------------------------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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 可以通过标注的形式加事务 -->
<context:annotation-config/>
<!--dataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/myhibernate"/>
<property name="username" value="root"/>
<property name="password" value="1"/>
<property name="connectionProperties">
<props>
<!-- 初始10个连接 -->
<prop key="initialSize">10</prop>
</props>
</property>
</bean>
<!--sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>model/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- hibernate事务 -->
<bean id="tm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="tm"/>
<!-- hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置dao -->
<bean id="userDao" class="dao.impl.UserDaoImpl">
<property name="ht" ref="hibernateTemplate"></property>
</bean>
<!-- 配置service -->
<bean id="userService" class="service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
-----------------------------------------------------Service层--------------------------------------------------------------------------------
package service.impl;
import model.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import service.UserService;
import dao.UserDao;
/**
* <业务层>
*
* @author snowday88
*/
public class UserServiceImpl implements UserService
{
/**
* 注入userDao
*/
private UserDao userDao;
/**
* 保存用户
*
* @param user user对象
*/
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {java.lang.Exception.class})
public void addUser(User user) throws Exception
{
userDao.saveUser(user);
throw new Exception();
}
/**
* 对userDao注入
*
* @param userDao 对userDao进行赋值
*/
public void setUserDao(UserDao userDao)
{
this.userDao = userDao;
}
}
-------------------------------------------------------------------dao层-----------------------------------------------------------
package dao.impl;
import java.util.List;
import model.User;
import org.springframework.orm.hibernate3.HibernateTemplate;
import dao.UserDao;
/**
* <UserDao实现类>
*
* @author snowday88
*/
public class UserDaoImpl implements UserDao
{
//hibernateTemplate
private HibernateTemplate ht;
/**
* <根据id删除>
*
* @param id id标识
*/
public void deleteById(Integer id)
{
Object obj = ht.get(User.class, id);
ht.delete(obj);
}
/**
* <查询所有的id>
*
* @return 返回用户集合
*/
@SuppressWarnings("unchecked")
public List<User> findAll()
{
return ht.find("from User");
}
/**
* <根据id查找用户对象>
*
* @param id id标识
* @return 返回用户对象
*/
public User findUserById(Integer id)
{
return (User)ht.get(User.class, id);
}
/**
* <修改用户>
*
* @param user 用户对象
*/
public void modifyUser(User user)
{
ht.saveOrUpdate(user);
}
/**
* <保存用户>
*
* @param user 用户对象
*/
public void saveUser(User user) throws Exception
{
ht.save(user);
}
/**
* 返回 ht
*
* @return 返回 ht
*/
public HibernateTemplate getHt()
{
return this.ht;
}
/**
* 对ht进行赋值
*
* @param ht 对ht进行赋值
*/
public void setHt(HibernateTemplate ht)
{
this.ht = ht;
}
}
-------------------------------------------------------model-----------------------------------------------------------
package model;
import java.io.Serializable;
/**
* <User实体类>
*
* @author snowday88
*/
public class User implements Serializable
{
/**
* 序列化标识id
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String userName;
private String password;
private Integer deptId;
/**
* <默认构造函数>
*/
public User()
{
}
/**
* 返回 id
*
* @return 返回 id
*/
public Integer getId()
{
return this.id;
}
/**
* 对id进行赋值
*
* @param id 对id进行赋值
*/
public void setId(Integer id)
{
this.id = id;
}
/**
* 返回 userName
*
* @return 返回 userName
*/
public String getUserName()
{
return this.userName;
}
/**
* 对userName进行赋值
*
* @param userName 对userName进行赋值
*/
public void setUserName(String userName)
{
this.userName = userName;
}
/**
* 返回 password
*
* @return 返回 password
*/
public String getPassword()
{
return this.password;
}
/**
* 对password进行赋值
*
* @param password 对password进行赋值
*/
public void setPassword(String password)
{
this.password = password;
}
/**
* 返回 deptId
*
* @return 返回 deptId
*/
public Integer getDeptId()
{
return this.deptId;
}
/**
* 对deptId进行赋值
*
* @param deptId 对deptId进行赋值
*/
public void setDeptId(Integer deptId)
{
this.deptId = deptId;
}
}
------------------------------------------------------测试类--------------------------------------------------------------------------------
package test;
import model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
/**
* <测试类>
*
* @author snowday88
*/
public class Test
{
public static void main(String[] args) throws Exception
{
// 初始化容器
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 从容器中获取userService
UserService userService = (UserService)context.getBean("userService");
// 测试
User user = new User();
user.setUserName("RunException");
user.setPassword("123");
user.setDeptId(2);
userService.addUser(user);
}
}