Spring框架中Dao支持总结:spring中普通类使用hibernate
Dao 的支持类可以有好多,如: JdbcDaoSupport , HibernateDaoSupport ,JdoDaoSupport等,下面对最常用的HibernateDaoSupport与JdbcDaoSupport做一小总结:
一、在Spring框架中实现连接数据库方式:
1、、借助类HibernateDaoSupport的简约化实现:
借助这个类必须需要HIbernate框架,用到HIbernate里提供的HibernateTemplate模板,从而进行增、删、改、查操作。
TestA.java:
public class TestA extends HibernateDaoSupport{
public void test(){
HibernateTemplate template = this.getHibernateTemplate();
List list = template.find("from User");
for(int i=0;i<list.size();i++){
User u = (User)list.get(i);
System.out.println(u.getUserid()+" "+u.getEmail());
}
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
TestA t = (TestA)ctx.getBean("testa");
t.test();
}
}
一、在Spring框架中实现连接数据库方式:
1、、借助类HibernateDaoSupport的简约化实现:
借助这个类必须需要HIbernate框架,用到HIbernate里提供的HibernateTemplate模板,从而进行增、删、改、查操作。
TestA.java:
public class TestA extends HibernateDaoSupport{
public void test(){
HibernateTemplate template = this.getHibernateTemplate();
List list = template.find("from User");
for(int i=0;i<list.size();i++){
User u = (User)list.get(i);
System.out.println(u.getUserid()+" "+u.getEmail());
}
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
TestA t = (TestA)ctx.getBean("testa");
t.test();
}
}
需要在Spring的配置文件applicationContext.xml中控制反转用到连接数据库中的类是注入SessionsFactory,如下:
applicationContext.xml:
<bean id="testa" class="com.sun.demo.TestA">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
applicationContext.xml:
<bean id="testa" class="com.sun.demo.TestA">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
2、、借助类JdbcDaoSupport的简约化实现:
借助这个类不需要HIbernate框架,也不需要有ORM的对象关联映射。但它和HibernateDaoSupport很类似,有JdbcTemplate来实现增、删、改、查操作。
TestA.java:
public class TestA extends JdbcDaoSupport{
public void test(){
int n = this.getJdbcTemplate().queryForInt("select max(userid) from User");
System.out.println(n);
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
TestA ta = (TestA)ctx.getBean("testa");
ta.test();
}
}
借助这个类不需要HIbernate框架,也不需要有ORM的对象关联映射。但它和HibernateDaoSupport很类似,有JdbcTemplate来实现增、删、改、查操作。
TestA.java:
public class TestA extends JdbcDaoSupport{
public void test(){
int n = this.getJdbcTemplate().queryForInt("select max(userid) from User");
System.out.println(n);
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
TestA ta = (TestA)ctx.getBean("testa");
ta.test();
}
}
需要在Spring的配置文件applicationContext.xml中控制反转用到连接数据库中的类是注入DataSource,如下:
applicationContext.xml:
<bean id="testa" class="com.sun.demo.TestA">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
</bean>
二、编程式事务管理、声明式事务管理中HibernateDaoSupport与JdbcDaoSupport的应用:
1、编程式事务管理:
1>、JdbcDaoSupport编程式事物管理,必须继承JdbcDaoSupport类,得到JdbcTemplate从而实现事务管理;
TestA.java:
public class TestA extends JdbcDaoSupport{
public void test(){
PlatformTransactionManager ptm = new DataSourceTransactionManager(this.getDataSource());
TransactionTemplate tst = new TransactionTemplate(ptm);
tst.execute(new TransactionCallback(){
public Object doInTransaction(TransactionStatus status) {
getJdbcTemplate().update("update User set password='1230' where userid='1001'");
getJdbcTemplate().update("update User set email='4444444444444444444444444444444444' where userid='1002'");
getJdbcTemplate().update("update User set phone='phone'");
return null;
}
});
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
TestA t = (TestA)ctx.getBean("testa");
t.test();
}
}
applicationContext.xml:
<bean id="testa" class="com.sun.demo.TestA">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
</bean>
二、编程式事务管理、声明式事务管理中HibernateDaoSupport与JdbcDaoSupport的应用:
1、编程式事务管理:
1>、JdbcDaoSupport编程式事物管理,必须继承JdbcDaoSupport类,得到JdbcTemplate从而实现事务管理;
TestA.java:
public class TestA extends JdbcDaoSupport{
public void test(){
PlatformTransactionManager ptm = new DataSourceTransactionManager(this.getDataSource());
TransactionTemplate tst = new TransactionTemplate(ptm);
tst.execute(new TransactionCallback(){
public Object doInTransaction(TransactionStatus status) {
getJdbcTemplate().update("update User set password='1230' where userid='1001'");
getJdbcTemplate().update("update User set email='4444444444444444444444444444444444' where userid='1002'");
getJdbcTemplate().update("update User set phone='phone'");
return null;
}
});
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
TestA t = (TestA)ctx.getBean("testa");
t.test();
}
}
需要在Spring的配置文件applicationContext.xml中控制反转用到连接数据库中的类是注入DataSource,如下:
applicationContext.xml:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
... ...
<property name="defaultAutoCommit">
<value>false</value>
</property>
</bean>
<bean id="testa" class="com.sun.demo.TestA">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
</bean>
2>、HibernateDaoSupport编程式事物管理,必须继承HibernateDaoSupport类,得到HibernateTemplate从而实现事务管理;
applicationContext.xml:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
... ...
<property name="defaultAutoCommit">
<value>false</value>
</property>
</bean>
<bean id="testa" class="com.sun.demo.TestA">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
</bean>
2>、HibernateDaoSupport编程式事物管理,必须继承HibernateDaoSupport类,得到HibernateTemplate从而实现事务管理;
TestB.java:
public class TestB extends HibernateDaoSupport{
public void test(){
HibernateTransactionManager hm = new HibernateTransactionManager(this.getSessionFactory());
TransactionTemplate ttm = new TransactionTemplate(hm);
public class TestB extends HibernateDaoSupport{
public void test(){
HibernateTransactionManager hm = new HibernateTransactionManager(this.getSessionFactory());
TransactionTemplate ttm = new TransactionTemplate(hm);
ttm.execute(new TransactionCallback(){
public Object doInTransaction(TransactionStatus status) {
User u1 = new User();
u1.setUserid("3001");
u1.setPassword("3001");
getHibernateTemplate().save(u1);
User u2 = new User();
u2.setUserid("3001");
u2.setPassword("3002");
getHibernateTemplate().save(u2);
return null;
}
});
}
public Object doInTransaction(TransactionStatus status) {
User u1 = new User();
u1.setUserid("3001");
u1.setPassword("3001");
getHibernateTemplate().save(u1);
User u2 = new User();
u2.setUserid("3001");
u2.setPassword("3002");
getHibernateTemplate().save(u2);
return null;
}
});
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
TestB t = (TestB)ctx.getBean("testb");
t.test();
}
}
需要在Spring的配置文件applicationContext.xml中控制反转用到连接数据库中的类是注入SessionsFactory,如下:
applicationContext.xml:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
... ...
<property name="defaultAutoCommit">
<value>false</value>
</property>
</bean>
<bean id="testb" class="com.sun.demo.TestB">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
2、声明式事务管理:
1>、JdbcDaoSupport声明式样事物管理,必须继承JdbcDaoSupport类;
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
... ....
<property name="defaultAutoCommit">
<value>false</value>
</property>
</bean>
需要在Spring配置文件中配置声明事务处理,用JdbcDaoSupport提供的DataSourceTransactionManager如下:
... ....
<property name="defaultAutoCommit">
<value>false</value>
</property>
</bean>
需要在Spring配置文件中配置声明事务处理,用JdbcDaoSupport提供的DataSourceTransactionManager如下:
<!-- 事务管理配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
</bean>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<!-- 事务管理属性 -->
<property name="transactionAttributes">
<props>
<prop key="updateUser">PROPAGATION_REQUIRED</prop>
</props>
<!-- 事务管理目标bean -->
</property>
<property name="target" ref="userdao"></property>
</bean>
<bean id="userdao" class="com.sun.demo.UserDao">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
</bean>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<!-- 事务管理属性 -->
<property name="transactionAttributes">
<props>
<prop key="updateUser">PROPAGATION_REQUIRED</prop>
</props>
<!-- 事务管理目标bean -->
</property>
<property name="target" ref="userdao"></property>
</bean>
<bean id="userdao" class="com.sun.demo.UserDao">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
</bean>
IUserDao.java://定义接口
public interface IUserDao {
public void updateUser();
}
UserDao.java:
public class UserDao extends JdbcDaoSupport implements IUserDao{
public void updateUser(){
this.getJdbcTemplate().update("update User set password='password1' where userid='1001'");
this.getJdbcTemplate().update("update User set password='password9999999999999999999999999999999' where userid='1002'");
}
}
TestA.java://调用updateUser()方法
public class{
public void test(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserDao u = (IUserDao)ctx.getBean("baseTransactionProxy");
u.updateUser();
}
public static void main(String[] args) {
TestA t = new TestA();
t.test();
}
}
2>、HibernateDaoSupport声明式事物管理,必须继承HibernateDaoSupport类;
IUserDao.java://定义接口
public interface IUserDao {
public void updateUser();
}
public interface IUserDao {
public void updateUser();
}
UserDao.java:
public class UserDao extends HibernateDaoSupport implements IUserDao{
public void updateUser(){
User u1 = new User();
u1.setUserid("1001");
u1.setPassword("5001");
this.getHibernateTemplate().update(u1);
User u2 = new User();
u2.setUserid("1003");
u2.setPassword("20015555555555555555555555555555555555555555555");
this.getHibernateTemplate().update(u2);
}
}
public class UserDao extends HibernateDaoSupport implements IUserDao{
public void updateUser(){
User u1 = new User();
u1.setUserid("1001");
u1.setPassword("5001");
this.getHibernateTemplate().update(u1);
User u2 = new User();
u2.setUserid("1003");
u2.setPassword("20015555555555555555555555555555555555555555555");
this.getHibernateTemplate().update(u2);
}
}
测试:
public class TestA{
public void test(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserDao userdao= (IUserDao)ctx.getBean("userproxy");
userdao.updateUser();
}
public static void main(String[] args) {
TestA t = new TestA();
t.test();
}
}
需要在Spring配置文件中配置声明事务处理,用JdbcDaoSupport提供的HibernateTransactionManager如下:
applicationContext.xml:
<!-- 事务管理配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<!-- 事务管理属性 -->
<property name="transactionAttributes">
<props>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
<!-- 事务管理目标bean -->
</property>
</bean>
<bean id="userdao" class="com.sun.demo.UserDao">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
<bean id="userproxy" parent="baseTransactionProxy">
<property name="target" ref="userdao"></property>
</bean>
applicationContext.xml:
<!-- 事务管理配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<!-- 事务管理属性 -->
<property name="transactionAttributes">
<props>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
<!-- 事务管理目标bean -->
</property>
</bean>
<bean id="userdao" class="com.sun.demo.UserDao">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
<bean id="userproxy" parent="baseTransactionProxy">
<property name="target" ref="userdao"></property>
</bean>
关注微信公众号福利!!!
回复关键字「666」获取一份最新 Java 架构资料,你要的都有!
回复关键字「Java」获取JVM, 多线程等Java技术系列教程;
回复关键字「spring」获取Spring, Spring Boot, Spring Cloud教程;
回复关键字「架构」获取分布式、微服务、架构、高并发等系列干货;
回复关键字「面试」获取各种 Java 面试题及答案、面试实战经验;