spring_150806_hibernate_non_transaction
添加hibernate的相关jar包!
实体类:
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 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);
} 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 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"> <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>
</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.saveDogPetByDataSource();
//dogPetService.saveDogPetByHibernate();
dogPetService.saveDogPetByHibernate();
}
}
spring_150806_hibernate_non_transaction的更多相关文章
随机推荐
- EF之高级查询
首先我们来看看一个页面 这里面有多选的条件,大于,小于等等,包括每个字段都有 如此多的查询条件,我们的后台该如何实现呢? 难道我们还得每个参数都去判断吗? 那得传多少参数进来才能实现这个页面的功能啊! ...
- Entity Framework SqlFunctions 教你如何在EF调用sqlserver方法的函数存根
今天算是研究了一天的SqlFunctions,请教了几个群的牛人,居然发现大伙对这个都比较陌生, 有的甚至直指EF中是不能调用sqlserver里的方法的. 因为之前搞过linq to sql 里面的 ...
- 阅读verilog程序总结
1.写程序先直接写出时钟信号 //-----------------产生串行时钟scl,为输入时钟的二分频--------- always@(negedge clk)//二分频表示频率小了,周期大了 ...
- 28335timer
/*****************************************************************************Copyright: 2014,TkaiFi ...
- 代码实现Autolayout
代码实现Autolayout的步骤 利用NSLayoutConstraint类创建具体的约束对象 添加约束对象到相应的view上 - (void)addConstraint:(NSLayoutCons ...
- STL之multimap
参见http://www.cplusplus.com/reference/map/multimap/ 多重映射multimap和map映射很相似,但是multimap允许重复的关键字,这使得multi ...
- linux内核分析之fork()
从一个比较有意思的题开始说起,最近要找工作无意间看到一个关于unix/linux中fork()的面试题: #include<sys/types.h> #include<stdio.h ...
- 笔记本显示器坏了,从硬盘安装win7系统
可以装的,从硬盘安装的话,步骤如下:一.将从网上下载的win7旗舰版ISO系统文件存放到D盘. 二.从网上下载虚拟光驱,打开安装后在任务栏右通知区显示“虚拟DAEMON管理器”图标,在我的电脑窗口显示 ...
- 《JavaScript高级程序设计》第5章 引用类型
5.2.2 转换方法 所有对象都有toString()和valueOf()方法调用数组的toString()方法,会返回一个字符串,由数组中的每个项通过逗号连接而成调用valueOf()还是返回数组 ...
- HDU 5597 GTW likes function 欧拉函数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5597 题意: http://bestcoder.hdu.edu.cn/contests/contes ...