`
xiaoxuesheng
  • 浏览: 6749 次
社区版块
存档分类
最新评论

spring4集成hibernate4:xml方式

    博客分类:
  • ssh
阅读更多
User.java
package com.lzj.www.model;

public class User {

	private Integer id;
	private String name;
	
	public Integer getId() {
		return id;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}


UserServiceImpl.java
package com.lzj.www.service.impl;


import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.lzj.www.model.User;
import com.lzj.www.service.UserService;


public class UserServiceImpl extends HibernateDaoSupport implements UserService{

	@Override
	public void addUser(User user) {
		this.getHibernateTemplate().save(user);
	}	
	
}


User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.lzj.www.model">
	
	<class name="User" table="user">
		<id name="id">
			<generator class="increment"/>
		</id>
		<property name="name"/>
	</class>
	
</hibernate-mapping>

hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!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/lzj/www/model/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

applicationContext.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
    <!-- 引入properties文件 -->
    
    <context:property-placeholder location="classpath:appConfig.properties" />
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${driver}" />
        <property name="jdbcUrl" value="${url}" />
        <property name="user" value="${user}" />
        <property name="password" value="${password}" />
        <property name="initialPoolSize" value="5" />
    </bean>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />

        <!-- hibernate的相关属性配置 -->
        <property name="hibernateProperties">
            <value>
                <!-- 设置数据库方言 -->
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                <!-- 设置自动创建|更新|验证数据库表结构 -->
                hibernate.hbm2ddl.auto=update
                <!-- 是否在控制台显示sql -->
                hibernate.show_sql=true
                <!-- 是否格式化sql,优化显示 -->
                hibernate.format_sql=true
                <!-- 是否开启二级缓存 -->
                hibernate.cache.use_second_level_cache=false
                <!-- 是否开启查询缓存 -->
                hibernate.cache.use_query_cache=false
                <!-- 数据库批量查询最大数 -->
                hibernate.jdbc.fetch_size=50
                <!-- 数据库批量更新、添加、删除操作最大数 -->
                hibernate.jdbc.batch_size=50
                <!-- 是否自动提交事务 -->
                hibernate.connection.autocommit=true
                <!-- 指定hibernate在何时释放JDBC连接 -->
                hibernate.connection.release_mode=auto
                <!-- 创建session方式 hibernate4.x 的方式 -->
                hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
                <!-- javax.persistence.validation.mode默认情况下是auto的,就是说如果不设置的话它是会自动去你的classpath下面找一个bean-validation**包 
                    所以把它设置为none即可 -->
                javax.persistence.validation.mode=none
            </value>
        </property>
        
        <!-- 引入hibernate的配置文件 -->
         <property name="configLocation" value="classpath:hibernate.cfg.xml"/>  

    </bean>
    
    <!-- 定义事务管理 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
        	<!-- 针对不同的方法的具体事务操作 -->
            <tx:method name="add*" propagation="REQUIRED" />
            <!-- 
                read-only="true"  表示只读
             -->
            <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 定义切面,执行有关的hibernate session的事务操作 -->
    <aop:config>
    	<!-- 定义在哪里使用事务 -->
        <aop:pointcut id="serviceOperation" expression="execution(* com.lzj.www.service.impl.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
    </aop:config>
    
    <!-- 为该对象注入sessionFactory,否则在使用this.getHibernateTemplate()时会报nullException -->
    <bean id="userService" class="com.lzj.www.service.impl.UserServiceImpl">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
    
</beans>

test.java
package com.lzj.www.test;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lzj.www.model.User;
import com.lzj.www.service.UserService;

public class test {

	@Test
	public void test01(){
		
//		BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
//		UserService userService = (UserService)factory.getBean("userService");
//		userService.service("xiaoming");
		
//		UserService userService = new UserServiceImpl();
		BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService)factory.getBean("userService");
		User user = new User();
		user.setName("name");
		userService.addUser(user);
	}
	
	public static void main(String[] args) {
		
		BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService)factory.getBean("userService");
		User user = new User();
		user.setName("xiaoming");
		userService.addUser(user);
		
	}
	
}
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics