hibernate3.2与spring2.5集成
来源:互联网 发布:广东省大数据局 编辑:程序博客网 时间:2024/06/07 22:16
--------------------------------------------------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);
}
}
- hibernate3.2与spring2.5集成
- Spring2.5 + Hibernate3.2 集成实例(CTO)
- Java : Hibernate3.2 与 Spring2.5整合
- struts1.2 + hibernate3.2 + Spring2.5集成配置
- Struts1.2&Spring2.5&Hibernate3.2集成---非注解方式
- Struts1.2&Spring2.5&Hibernate3.2集成---全注解方式
- Spring2.5+Hibernate3.2+Struts2.0+jotm2.0集成配置
- Struts2.1&Hibernate3.2&Spring2.5集成[基于Annotation]--代码
- Struts2.1&Hibernate3.2&Spring2.5集成[基于Annotation]--配置文件
- netbeans6.1环境下struts2+spring2.5+hibernate3.2集成
- Spring2.0+Struts1.x+Hibernate3.2集成
- hibernate3+spring2集成配置
- Struts2.1&Hibernate3.2&Spring2.5集成[基于Annotation]--Spring2.5单元测试
- Struts2.1&Hibernate3.2&Spring2.5集成[基于Annotation]--案例结构
- Struts2.1&Hibernate3.2&Spring2.5集成[基于Annotation]--运行效果
- struts2+spring2.5+hibernate3集成的简单实例
- struts1.2+spring2.0+hibernate3.1集成开发详解
- ibatis2与Spring2.5集成
- LayoutInflater解析xml布局文件
- JSONP解决JS跨域
- 校准触摸屏即时生效
- TCP三次握手连接及seq和ack号的正确理解
- C++实现String类
- hibernate3.2与spring2.5集成
- mpg123
- 在xp,win7下程序运行时可以修改它的文件名,不知道为什么这样,估计要看一下内核相关的内容。
- OutOfMemory:PermGen Space异常的处理和分析
- 网络工程师基础知识(网络设备的选择和基本配置)
- Fedora14下 mysql更改密码
- synergy共享键盘鼠标
- 施密特触发器
- ShellExecute运行word dot文件