spring_150904_hibernatetemplate
实体类:
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();
}
实现类ServiceImpl:
package com.spring.service.impl; import java.util.List; 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());
}
}
} }
Service调用的DAO:
package com.spring.dao; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component; import com.spring.model.DogPet; @Component
public class DogPetDAO
{
private HibernateTemplate hibernateTemplate; public List<DogPet> queryAllDogPets()
{
List<DogPet> list = hibernateTemplate.find("from DogPet");
return list;
} public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
} @Autowired
public void setHibernateTemplate(@Qualifier(value="hibernateTemplate") HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
} }
配置文件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--> <!--
1、继承HibernateDaoSupport类,第一种数据库访问方式 设置HibernateDaoSupport抽象类
<bean id="hibernateDaoSupport"
class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
abstract="true">
<property name="sessionFactory" ref="sessionFactory" />
</bean> dao的操作的bean
<bean id="dogPetDAO" class="com.spring.dao.DogPetDAO" parent="hibernateDaoSupport" /> 2、继承HibernateDaoSupport类,第二种数据库访问方式
<bean id="dogPetDAO" class="com.spring.dao.DogPetDAO" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean> </beans>
test类:
package com.spring.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.service.DogPetService; public class ServiceTest { public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml","dao.xml"});
DogPetService dogPetService = (DogPetService)ctx.getBean("DogPetService");
dogPetService.queryAllDogPets();
} }
spring_150904_hibernatetemplate的更多相关文章
随机推荐
- discuz开发,登录次数过多,锁定解决方法
到数据库里的表找到pre_common_failedlogin 和pre_ucenter_failedlogins清空里面的内容即可. truncate table pre_common_failed ...
- 题解 P2762 【太空飞行计划问题】
P2762 太空飞行计划问题 题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,-,Em},和进 ...
- Flex布局之box-flex
box-flex的写法比flex的写法要复杂一些,兼容性的前缀要多带几个,真希望有一天flex布局能够纳入w3c标准啊! <!DOCTYPE html> <html> < ...
- CSS3实现鼠标hover的过渡效果
我想让鼠标放在div上就让它旋转变大,离开div后它又恢复本来的样子. 于是我就想写一个JS,监听一个hover事件,当hover发生的时候,触发一个计时器,在计时器里写两个值,一个管角度,一个管宽度 ...
- 【洛谷 P2120】 [ZJOI2007]仓库建设(斜率优化)
题目链接 斜率优化+1,好吧不水分了. 玩具装箱那题以后再做,当作复习吧. \(f[i]=f[j]-(sum[i]-sum[j])*dis[i]+p[i]\) \(f[j]=-dis[i]*sum[j ...
- HDU 1166 敌兵布阵 (树状数组 单点修改+区间查询)
题目链接 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和T ...
- JFinal向客户端渲染图片的方法
JFinal提供了好几种方便的render但是不知道为啥就是没有提供直接渲染图片的render,如果我们直接在Controller的方法中往输入流中写的话是还是会有默认的render生效的,比如下面这 ...
- DataFrame衍生新特征操作
1.DataFrame中某一列的值衍生为新的特征 #将LBL1特征的值衍生为one-hot形式的新特征 piao=df_train_log.LBL1.value_counts().index #先构造 ...
- 数据库与sql注入的相关知识
数据库与sql注入的相关知识 sql语句明显是针对数据库的一种操作,既然想通过sql注入的方法来拿取数据那么就要先了解一下如何的去操作数据库,这方面并不需要对数据库有多么的精通但是如果了解掌握了其中的 ...
- python基础之内置异常对象
前言 什么叫异常?简单来说就是程序运行发生了预计结果之外的情况从而导致程序无法正常运行.而python解释器将一些常见的异常情况在它发生时打包成一个异常对象,我们可以通过捕捉这些异常对象从而进行处理, ...