自己写的项目开发框架(2)--从spring配置hibernate和jdbc开始

* 本框架也是一个很简单的SSH2框架,只是希望在DAO层中能够支持hibernate的对象操作和jdbc的sql查询而写的一个简单例子框架。所以本框架注重DAO层的代码。

一。通过spring配置hibernate和jdbc的支持:

spring配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--  配置数据源 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass">
   <value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="jdbcUrl">
   <value>jdbc:mysql://127.0.0.1:3306/lh446?useUnicode=true&characterEncoding=utf-8</value>
  </property>
  <property name="user">
   <value>root</value>
  </property>
  <property name="password">
   <value>lh446</value>
  </property>
  <property name="minPoolSize">
   <value>1</value>
  </property>
  <property name="maxPoolSize">
   <value>20</value>
  </property>
  <property name="maxIdleTime">
   <value>1800</value>
  </property>
  <property name="acquireIncrement">
   <value>2</value>
  </property>
  <property name="maxStatements">
   <value>0</value>
  </property>
  <property name="initialPoolSize">
   <value>2</value>
  </property>
  <property name="idleConnectionTestPeriod">
   <value>1800</value>
  </property>
  <property name="acquireRetryAttempts">
   <value>30</value>
  </property>
  <property name="breakAfterAcquireFailure">
   <value>false</value>
  </property>
 </bean>

 <!-- 配置Hibernate -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
  <property name="mappingResources">
   <list>
       <value>com/lh446/authority/entity/Functionn.hbm.xml</value>
       <value>com/lh446/authority/entity/Role.hbm.xml</value>
       <value>com/lh446/authority/entity/Users.hbm.xml</value>
       <value>com/lh446/authority/entity/UserRole.hbm.xml</value>
       <value>com/lh446/authority/entity/RoleFunction.hbm.xml</value>
        <value>com/lh446/authority/entity/Code.hbm.xml</value>
        <value>com/lh446/authority/entity/SysLog.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.generate_statistics">true</prop>
    <prop key="hibernate.connection.release_mode">auto</prop>
    <prop key="hibernate.autoReconnect">true</prop>
    <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
   </props>
  </property>
 </bean>

<!-- 配置hibernate -->
 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <!-- 配置jdbc -->
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource"></property>
 </bean>


这样我们的项目中就可以使用hibernate的同时又支持jdbc。hibernate只要做增,删,改操作和一些HQL查询。jdbc帮助我们完成一些复杂的查询语句。

二。使用hibernate和jdbc

新增一个Persistence类将hibernateTemplate和jdbcTemplate注入到该类中。

public class Persistence {
    private JdbcTemplate jdbcTemplate;
    private 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;
	}
....
}

这样我们的持久类就支持hibernate和jdbc了。

待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值