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

spring4集成hibernate3:xml方式

    博客分类:
  • ssh
阅读更多

 

model:
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;
	}
	
}


 model.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>
 service:
package com.lzj.www.service.impl;

import org.springframework.orm.hibernate3.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);
	}	
	
}

 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>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/Spring</property>
		<property name="connection.username">root</property>
		<property name="connection.password">java_5937</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<property name="show_sql">true</property>

		<mapping resource="com/lzj/www/model/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
 beans.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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx    
		http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
	</bean>
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:pointcut expression="execution(* com.lzj.www.service.impl.*.*(..))" id="pointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
	</aop:config>
	
         <!-- nullException -->
	<bean id="userService" class="com.lzj.www.service.impl.UserServiceImpl">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
</beans>
 可能出现的错误:

 

 1、nullpointException
this.getHibernateTemplate()为null,原因是使用Template的对象是直接new出来的,spring并没有
对其进行注入,因此该对象是不具有sessionFactory的,也没有绑定相关的事务
所以要把这个对象配置到ioc容器中

2、java.lang.IllegalArgumentException
 Pointcut is not well-formed: expecting '(' at character position 0
在配置aop的advisor时,配置的是pointcut-ref而不是pointcut,用快捷键的时候小心点就是了                                              

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics