之前在上家公司呆的时候,公司整合了jdbcTemplate与hibernateTemplate同时使用,现在在一家新公司,没框架,自己在整,遇到点难题,就是怎么jdbcTemplate与hibernateTemplate同时使用,我先说下我的做法,小弟没怎么整过框架,对框架不怎么熟练,请谅解。
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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 日志记录某个类中方法花费时间aop -->
<aop:config>
<!-- Spring 2.0 可以用 AspectJ 的语法定义 Pointcut,这里拦截 service 包中的所有方法 -->
<aop:advisor id="methodTimeLog" advice-ref="methodTimeAdvice" pointcut="execution(* *..Service..*(..))"/>
</aop:config>
<bean id="methodTimeAdvice" class="com.MethodTimeAdvice"/>
<import resource="classpath:database.xml"/>
<!-- Action -->
<!-- <bean id="loginAction" scope="prototype" class="action.LoginAction"></bean>
<bean id="registrationAction" scope="prototype" class="action.RegistrationAction"></bean> -->
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ams?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/model/User.hbm.xml</value>
</list>
</property>
</bean> -->
<!-- struts beans -->
<bean id="LoginAction" class="com.action.LoginAction" scope="prototype">
<property name="userService" ref="userService" />
</bean>
<bean id="userService" class="com.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="com.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>database.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:dataSource.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!---->
<property name="driverClassName" value="${dataSource.driverClassName}"/>
<!---->
<property name="url" value="${dataSource.url}" />
<!---->
<property name="username" value="${dataSource.username}"/>
<!---->
<property name="password" value="${dataSource.password}"/>
<!--最少保持的空闲连接数 (默认2个)-->
<!--<property name="prototypeCount" value="5"/>-->
<!--最大活动时间(超过此时间线程将被kill,默认为5分钟)-->
<!--<property name="maximumActiveTime" value="600000"/>-->
<!--连接最长时间(默认为4个小时)-->
<!--<property name="maximumConnectionLifetime" value="600000"/>-->
<!--最大连接数 (默认5个)-->
<!--<property name="maximumConnectionCount" value="100"/>-->
<!--最小连接数 (默认2个)-->
<!--<property name="minimumConnectionCount" value="10"/>-->
<!--同时最大连接数-->
<!--<property name="simultaneousBuildThrottle" value="50"/>-->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<!---->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!---->
<prop key="hibernate.connection.autocommit">false</prop>
<!---->
<prop key="hibernate.autoReconnect">true</prop>
<!---->
<prop key="hibernate.format_sql">true</prop>
<!---->
<prop key="hibernate.show_sql">true</prop>
<!---->
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/model/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Hibernate模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Hibernate事务 -->
<bean id="hibernateTransaction" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- AOP事务控制 -->
<tx:advice id="txAdvice1" transaction-manager="hibernateTransaction">
<tx:attributes>
<tx:method name="list*" propagation="NOT_SUPPORTED" />
<tx:method name="query*" propagation="NOT_SUPPORTED" />
<tx:method name="get*" propagation="NOT_SUPPORTED" />
<tx:method name="find*" propagation="NOT_SUPPORTED" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* *..Service..*(..))" />
<aop:advisor advice-ref="txAdvice1" pointcut-ref="allManagerMethod" />
</aop:config>
</beans>
BaseDao.java生成模板的set和get方法
public class BaseDao{
protected JdbcTemplate jdbcTemplate;
protected HibernateTemplate hibernateTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}
UserDaoImpl.java继承BaseDao
public class UserDaoImpl extends BaseDao implements UserDao{
public User find(String name, String password) {
String sql = "FROM User AS u WHERE u.name = "+name+" AND u.password = "+password+"";
List<User> list = getHibernateTemplate().find(sql);
if (list.size()==0){
return null;
}else{
return list.get(0);
}
}
}
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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 日志记录某个类中方法花费时间aop -->
<aop:config>
<!-- Spring 2.0 可以用 AspectJ 的语法定义 Pointcut,这里拦截 service 包中的所有方法 -->
<aop:advisor id="methodTimeLog" advice-ref="methodTimeAdvice" pointcut="execution(* *..Service..*(..))"/>
</aop:config>
<bean id="methodTimeAdvice" class="com.MethodTimeAdvice"/>
<import resource="classpath:database.xml"/>
<!-- Action -->
<!-- <bean id="loginAction" scope="prototype" class="action.LoginAction"></bean>
<bean id="registrationAction" scope="prototype" class="action.RegistrationAction"></bean> -->
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ams?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/model/User.hbm.xml</value>
</list>
</property>
</bean> -->
<!-- struts beans -->
<bean id="LoginAction" class="com.action.LoginAction" scope="prototype">
<property name="userService" ref="userService" />
</bean>
<bean id="userService" class="com.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="com.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>database.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:dataSource.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!---->
<property name="driverClassName" value="${dataSource.driverClassName}"/>
<!---->
<property name="url" value="${dataSource.url}" />
<!---->
<property name="username" value="${dataSource.username}"/>
<!---->
<property name="password" value="${dataSource.password}"/>
<!--最少保持的空闲连接数 (默认2个)-->
<!--<property name="prototypeCount" value="5"/>-->
<!--最大活动时间(超过此时间线程将被kill,默认为5分钟)-->
<!--<property name="maximumActiveTime" value="600000"/>-->
<!--连接最长时间(默认为4个小时)-->
<!--<property name="maximumConnectionLifetime" value="600000"/>-->
<!--最大连接数 (默认5个)-->
<!--<property name="maximumConnectionCount" value="100"/>-->
<!--最小连接数 (默认2个)-->
<!--<property name="minimumConnectionCount" value="10"/>-->
<!--同时最大连接数-->
<!--<property name="simultaneousBuildThrottle" value="50"/>-->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<!---->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!---->
<prop key="hibernate.connection.autocommit">false</prop>
<!---->
<prop key="hibernate.autoReconnect">true</prop>
<!---->
<prop key="hibernate.format_sql">true</prop>
<!---->
<prop key="hibernate.show_sql">true</prop>
<!---->
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/model/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Hibernate模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Hibernate事务 -->
<bean id="hibernateTransaction" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- AOP事务控制 -->
<tx:advice id="txAdvice1" transaction-manager="hibernateTransaction">
<tx:attributes>
<tx:method name="list*" propagation="NOT_SUPPORTED" />
<tx:method name="query*" propagation="NOT_SUPPORTED" />
<tx:method name="get*" propagation="NOT_SUPPORTED" />
<tx:method name="find*" propagation="NOT_SUPPORTED" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* *..Service..*(..))" />
<aop:advisor advice-ref="txAdvice1" pointcut-ref="allManagerMethod" />
</aop:config>
</beans>
BaseDao.java生成模板的set和get方法
public class BaseDao{
protected JdbcTemplate jdbcTemplate;
protected HibernateTemplate hibernateTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}
UserDaoImpl.java继承BaseDao
public class UserDaoImpl extends BaseDao implements UserDao{
public User find(String name, String password) {
String sql = "FROM User AS u WHERE u.name = "+name+" AND u.password = "+password+"";
List<User> list = getHibernateTemplate().find(sql);
if (list.size()==0){
return null;
}else{
return list.get(0);
}
}
}
解决方案 »
- 关于jboss部署项目的绝对路径获取的问题
- 乱码问题
- 请教一个tomcat的问题
- 谁有描述xsd文件的xsd文件
- 急等 tomcat通过windows服务启动无法访问网络路径的问题
- 我安装JBUILDER2006,我启动TOMCAT后,http://localhost:8080/admin/为何打不开?请指教
- 如何捕捉IE的保存事件
- 谁用struts做过项目呀?网上有用struts做项目的源码么,谁能提供一份,我想学习。谢谢。
- eclipse运行jsp 只差一步到罗马!帮忙马上送分!!
- 大虾解释一下weblogic的domain的目录结构!!!
- spring注入报错,请各位大侠帮忙看看 ,能不能帮小弟解决,小弟非常感谢!
- java的苦恼
要想用的话,注入就好,然后拿到就可以使用啦