搞了一天的这东东。发现spring这东西真是个好东东。废话少说直奔主题
首先要给spring弄个数据源
我把数据源配置文件放到了jdbc.properties文件了。
如下:
## MySQL
hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql:///shtest
hibernate.connection.username root
hibernate.connection.password 123
## Oracle
#hibernate.dialect org.hibernate.dialect.OracleDialect
#hibernate.dialect org.hibernate.dialect.Oracle9Dialect
#hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
#hibernate.connection.username ora
#hibernate.connection.password ora
#hibernate.connection.url jdbc:oracle:thin:@localhost:1521:orcl
#hibernate.connection.url jdbc:oracle:thin:@localhost:1522:XE
## DB2
#hibernate.dialect org.hibernate.dialect.DB2Dialect
#hibernate.connection.driver_class com.ibm.db2.jcc.DB2Driver
#hibernate.connection.driver_class COM.ibm.db2.jdbc.app.DB2Driver
#hibernate.connection.url jdbc:db2://localhost:50000/somename
#hibernate.connection.url jdbc:db2:somename
#hibernate.connection.username db2
#hibernate.connection.password db2
其次建立一个spring 的applicationContext.xml文件 里面有propertyConfigurerbean、dataSource bean 、
sessionFactory bean、一会一一对这几个bean进行介绍。
<?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:seam="http://jboss.com/products/seam/spring-seam"
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"
default-autowire="byName">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:resoureces/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="url" value="${hibernate.connection.url}"/>
<property name="username" value="${hibernate.connection.username}"/>
<property name="password" value="${hibernate.connection.password}"/>
<property name="driverClassName" value="${hibernate.connection.driver_class}"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocations">
<list>
<value>classpath:resoureces/hibernate.cfg.xml</value>
<value>classpath:resoureces/business.cfg.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"></bean>
<!-- 事务关注点 -->
<bean id="transactionAdvisor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="validate*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="validator*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userDao"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"
value="com.test.dao.UserDao" />
<property name="target">
<bean
class="com.test.dao.UserDaoImpl" />
</property>
<property name="interceptorNames">
<list>
<value>transactionAdvisor</value>
</list>
</property>
</bean>
</beans>
首先来看propertyConfigurer bean。org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
其是用来读取*.properties文件用的spring类 extends PropertyResourceConfigurer implements BeanNameAware, BeanFactoryAware
这是个使用用户和spring容器打交道的一个类。为什么要有这么bean呢?
答:它是为dataSource bean服务的。因为dataSource bean里面的属性值(<property name="url" value="${hibernate.connection.url}"/>)是用的spring的容器里面的propertyConfigurer bean里面取的值
紧接着咱们来看:sessionFactory bean 用org.springframework.orm.hibernate3.LocalSessionFactoryBean可以创建hibernate的sessionFactory 了。
再看它有这么一个<property name="configLocations"> 属性 它有这么个
<list>
<value>classpath:resoureces/hibernate.cfg.xml</value>
<value>classpath:resoureces/business.cfg.xml</value>
</list>
list值 。这个属性就可以把咱hibernate的东西整合到这里来了。
来看它整合的hibernate的那些东西hibernate.cfg.xml.这个文件内容如下:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- SQL logging configuration -->
<!-- NOTE: to show sql, use DEBUG log level for org.hibernate.SQL category -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">false</property>
<property name="generate_statistics">false</property>
<!-- Query configuration -->
<property name="hibernate.query.substitutions">true 1, false 0</property>
<property name="hibernate.max_fetch_depth">2</property>
<!-- Batch configuration -->
<property name="hibernate.default_batch_fetch_size">8</property>
<property name="hibernate.jdbc.batch_size">20</property>
<!-- Cache configuration -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_query_cache">true</property>
</session-factory>
</hibernate-configuration>
business.cfg.xml 这个文件内容如下:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping resource="com/test/domain/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我们可以把hibernate的配置文件分开写,引用一句小沈阳的话“为什么呢?”o(∩_∩)o...因为让它的颗度更细。当再添加其他文件配置文件的时候不至于引发其他的问题。
这个transactionAdvisor bean是spring的事务aop 。
它有一个属性 是transactionManager 所以我们还得弄个transactionManager bean。这个bean需要 sessionFactory。。不用担心spring的default-autowire属性给我们解决了手动诸如的问题。
生成库表
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test.domain">
<class name="User" table="USER" >
<id name="id" type="java.lang.String">
<generator class="uuid"/>
</id>
<property name="name"
not-null="true"
length="15"
column="NAME_"/>
<property name="age" column="AGE_" length="15"/>
</class>
</hibernate-mapping>
package com.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.util.ConfigHelper;
public class hbmtoddl {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration configuration = new Configuration();
configuration.configure("/resoureces/hibernate.cfg.xml");
configuration.configure("/resoureces/business.cfg.xml");
Properties properties = new Properties();
InputStream stream = ConfigHelper.getResourceAsStream("/resoureces/jdbc.properties");
try {
properties.load(stream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
configuration.setProperties(properties);
new SchemaExport(configuration).create(true, true);
}
}
基本上整合完了是不是该dao一下下了。
<bean id="userDao"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"
value="com.test.dao.UserDao" />
<property name="target">
<bean
class="com.test.dao.UserDaoImpl" />
</property>
<property name="interceptorNames">
<list>
<value>transactionAdvisor</value>
</list>
</property>
</bean>
package com.test.dao;
import com.test.domain.User;
public interface UserDao {
public void addUser(User user);
}
package com.test.dao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.test.domain.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
public void addUser(User user) {
// TODO Auto-generated method stub
getHibernateTemplate().save(user);
}
}
测试类UserTest
package com.junit;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;
import com.test.dao.UserDao;
import com.test.domain.User;
public class UserTest extends TestCase{
@Test
public void test(){
System.out.print("bean is ............:::");
ApplicationContext context= new ClassPathXmlApplicationContext(
new String[]{"/resoureces/testApplicationContext.xml"});
System.out.print("bean is ............:::"+context.getBean("userDao"));
UserDao userDao = (UserDao)context.getBean("userDao");
User user = new User();
user.setAge("44444444");
user.setName("额份额为发送");
userDao.addUser(user);
}
}
啰啰嗦嗦写弄完了。我的妈呀。。把我给累死了。那有啥问题希望大家共同讨论。有啥不合适的地方请大伙指正。that's it for taday.