spring_150807_hibernate_transaction_annotation
实体类:
package com.spring.model; import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="t_dog")
public class DogPet { private int id;
private String name;
private int age;
private String kind;
private String sex;
private String health; @Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHealth() {
return health;
}
public void setHealth(String health) {
this.health = health;
} public String toString()
{
return id+"--"+name+"--"+kind+"--"+age+"--"+health;
}
}
接口Service:
package com.spring.service; public interface DogPetService {
public void queryAllDogPets();
public void saveDogPetByDataSource();
public void saveDogPetByHibernate();
}
实现类ServiceImpl:
package com.spring.service.impl; import java.util.List;
import java.util.Random; import javax.annotation.Resource; import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional; import com.spring.service.DogPetService;
import com.spring.dao.DogPetDAO;
import com.spring.model.DogPet; @Controller(value="DogPetService")
public class DogPetServiceImpl implements DogPetService{
private DogPetDAO dogPetDAO; public DogPetDAO getDogPetDAO() {
return dogPetDAO;
} @Resource(name="dogPetDAO")
public void setDogPetDAO(DogPetDAO dogPetDAO) {
this.dogPetDAO = dogPetDAO;
} @Override
public void queryAllDogPets() {
List<DogPet> list = dogPetDAO.queryAllDogPets();
if(list != null)
{
for(DogPet d:list)
{
System.out.println(d.toString());
}
}
} public void saveDogPetByDataSource()
{
DogPet d1 = new DogPet();
d1.setId(new Random().nextInt(1000));
d1.setName("dog1");
d1.setAge(4);
d1.setKind("buladuo");
d1.setSex("B");
d1.setHealth("good");
dogPetDAO.saveDogPetByDataSource(d1);
} @Transactional
public void saveDogPetByHibernate()
{
DogPet d1 = new DogPet();
d1.setId(new Random().nextInt(1000));
d1.setName("dog1");
d1.setAge(4);
d1.setKind("buladuo");
d1.setSex("B");
d1.setHealth("good");
dogPetDAO.saveDogPetByHibernate(d1);
}
}
Service调用的DAO:
package com.spring.dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import javax.annotation.Resource;
import javax.sql.DataSource; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service; import com.spring.model.DogPet; @Service(value="dogPetDAO")
public class DogPetDAO { private DataSource dataSource;
private SessionFactory sessionFactory; public List<DogPet> queryAllDogPets()
{
List<DogPet> list = new ArrayList<DogPet>(); DogPet d1 = new DogPet();
d1.setId(1111);
d1.setName("dog1");
d1.setAge(4);
d1.setKind("buladuo");
d1.setSex("B");
d1.setHealth("good");
DogPet d2 = new DogPet();
d2.setId(2222);
d2.setName("dog2");
d2.setAge(3);
d2.setKind("buladuo");
d2.setSex("G");
d2.setHealth("good"); list.add(d1);
list.add(d2); return list;
} public void saveDogPetByDataSource(DogPet dog)
{
Connection connection = null;
try
{
connection = dataSource.getConnection() ;
PreparedStatement pstmt = connection.prepareStatement("insert into t_dog values(?,?,?,?,?,?)");
pstmt.setInt(1, dog.getId());
pstmt.setString(2, dog.getName());
pstmt.setInt(3, dog.getAge());
pstmt.setString(4, dog.getKind());
pstmt.setString(5, dog.getSex());
pstmt.setString(6, dog.getHealth());
pstmt.execute();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
} /*public void saveDogPetByHibernate(DogPet dog)
{
Session session = null;
try
{
session = sessionFactory.openSession();
session.beginTransaction();
session.save(dog);
session.getTransaction().commit();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
session.close();
}
}*/ public void saveDogPetByHibernate(DogPet dog)
{
Session session = sessionFactory.getCurrentSession();
session.save(dog);
} public DataSource getDataSource() {
return dataSource;
} @Resource
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
} public SessionFactory getSessionFactory() {
return sessionFactory;
} @Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} }
配置文件beans.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" 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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/> <context:component-scan base-package="com.spring"></context:component-scan> </beans>
配置文件dao.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 利用annotation配置声明式事物管理 begin-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.spring.model.DogPet</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean> <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!--<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="dogPetServiceOperation"
expression="execution(* com.spring..*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="dogPetServiceOperation" />
</aop:config> --> <tx:annotation-driven transaction-manager="txManager"/> <!-- 利用annotation配置声明式事物管理 end-->
</beans>
test类:
package com.spring.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.service.DogPetService; public class ServiceTest { @Test
public void saveDogPetByHibernate()
{
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml","dao.xml"});
DogPetService dogPetService = (DogPetService)ctx.getBean("DogPetService");
dogPetService.saveDogPetByHibernate();
} }
spring_150807_hibernate_transaction_annotation的更多相关文章
随机推荐
- jsp 项目中 web.xml 的作用
每个 web 应用的 WEB-INF 路径下(而且必须位于该路径)的 web.xml 文件被称为配置描述符. 对于 java web 应用而言,WEB-INF 是一个特殊的文件夹,web 容器会包含该 ...
- wx.request 获取不到post传递的值
微信小程序的wx.request请求,method设为POST并向后台传递数据,但从后台返回的信息来看后台并没有获得传递的数据 wx.request({ url: 'url' ...
- K8S dashboard 创建只读账户
1.创建名字为“Dashboard-viewonly“的Cluster Role,各种资源只给予了list,get,watch的权限.dashboard-viewonly.yaml --- apiVe ...
- 58到家mysql数据库军规及解读分享
一.基础规范 (1)必须使用InnoDB存储引擎 解读:支持事务.行级锁.并发性能更好.CPU及内存缓存页优化使得资源利用率更高 (2)必须使用UTF8字符集 解读:万国码,无需转码,无乱码风险,节省 ...
- ubuntu登陆界面损坏修复
Ubuntu系统从14升16过程中,不小心进入休眠状态.之后Ubuntu桌面界面打不开.进入命令模式,手动修复 网上的答案是这样: 首先,测试桌面环境安装是否完全.sudo apt-get insta ...
- 【算法日记】Dijkstra最短路径算法
上一篇再说广度优先搜索的适合提到了图. 狄克斯拉特算法是在图的基础上增加了 加权图的概念.就是节点和节点之间是有不同距离的 1.算法实例 用Dijkstra算法找出以A为起点的单源最短路径步骤如下 算 ...
- NOIP模拟7
期望得分:100+100+20=220 实际得分:100+95+20=215 T1 洛谷 P1306 斐波那契公约数 #include<cstdio> #include<cstrin ...
- javascript小技巧之with()方法
With()方法平时用得不多,本文用个小例子来学习一下.在这里记录.个人感觉还是很方便的. 有了 With 语句,在存取对象属性和方法时就不用重复指定参考对象,在 With 语句块中,凡是 JavaS ...
- 【BZOJ】2154: Crash的数字表格 莫比乌斯反演
[题意]给定n,m,求Σlcm(i,j),1<=i<=n,1<=j<=m,n,m<=10^7. [算法]数论(莫比乌斯反演) [题解] $$ans=\sum_{i\leq ...
- JQuery对RadioButton和CheckButton的操作
js对RadioButton和CheckButton的操作,在网站开发中会经常遇到,而JQuery操作RadioButton和CheckButton非常便捷.小编觉得网站开发人员有必要熟练掌握.所以小 ...