spring4集成hibernate4:annotation方式

model:

package com.lzj.www.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.GenericGenerator;

@Entity
public class User {

	private Integer id;
	private String name;
	
	@Id
	@GenericGenerator(name="generator", strategy="increment")
	@GeneratedValue(generator="generator")
	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;
	}
	
}

 service:

 

 

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);
	}	
	
}

 xml文件:spring配置

 

 

<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 destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <!-- 设置JDBC驱动名称 -->
        <property name="driverClass" value="${driver}" />
        <!-- 设置JDBC连接URL -->
        <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>
        <!-- 自动扫描实体对象 com.lzj.www.model包结构中存放实体类 -->
        <property name="packagesToScan" value="com.lzj.www.model" />
    </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="create*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="import*" 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>
    
     <!-- 扫描有注解的文件  base-package 包路径 -->
    <context:component-scan base-package="com.lzj.www.model.User"/>
    
    <bean id="userService" class="com.lzj.www.service.impl.UserServiceImpl">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
    
</beans>

 test:

 

 

 

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("applicationContext.xml");
		UserService userService = (UserService)factory.getBean("userService");
		User user = new User();
		user.setName("name");
		userService.addUser(user);
	}
	
	//用main的方法测试方便查错
	public static void main(String[] args) {
		
		BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService)factory.getBean("userService");
		User user = new User();
		user.setName("name");
		userService.addUser(user);
		
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值