spring hibernate mysql配置事务(SessionFactory) + AOP

====================spring.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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
		   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">


	<bean id="placeHolder"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
		p:locations="classpath:jdbc.properties" />

	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName"
			value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="annotatedClasses">
			<list>
				<value>
					com.spring.ch11.dao.hibernate.nativeapi.aop.Demo
				</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQL5InnoDBDialect
				</prop>
				<prop key="show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.use_sql_comments">true</prop>
			</props>
		</property>
	</bean>

	<bean id="com.spring.ch11.dao.hibernate.nativeapi.aop.NativeApiImpl"
		class="com.spring.ch11.dao.hibernate.nativeapi.aop.NativeApiImpl"
		p:sessionFactory-ref="sessionFactory">
	</bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<aop:config>
		<aop:pointcut
			expression="execution(* com.spring.ch11.dao.hibernate.nativeapi.aop.NativeApiImpl.*(..))"
			id="myPointCut" />
		<aop:advisor pointcut-ref="myPointCut" advice-ref="txAdvice" />
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" read-only="false" />
		</tx:attributes>
	</tx:advice>
</beans>

 

 

====================INativeApi.java====================

package com.spring.ch11.dao.hibernate.nativeapi.aop;

import java.util.List;

import com.spring.ch11.dao.hibernate.daosupport.DomainObject;

public interface INativeApi {

	public List<Demo> getAll();
	public void save(DomainObject domainObject);

}

 

 

====================NativeApiImpl .java====================

package com.spring.ch11.dao.hibernate.nativeapi.aop;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.spring.ch11.dao.hibernate.daosupport.DomainObject;

public class NativeApiImpl implements INativeApi {

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<Demo> getAll() {
		Session session = sessionFactory.getCurrentSession();
		return session.createQuery("from Demo").list();
	}

	@Override
	public void save(DomainObject domainObject) {
		sessionFactory.getCurrentSession().save(domainObject);
	}

}

 

 

====================Demo.java====================

package com.spring.ch11.dao.hibernate.nativeapi.aop;

import java.io.Serializable;

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

@Entity
public class Demo implements Serializable{
	private int id;
	private float unitPrice;
	private String name;

	@Id
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public float getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(float unitPrice) {
		this.unitPrice = unitPrice;
	}

	public String getName() {
		return name;
	}

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

}

 

 

==========================================

package com.spring.ch11.dao.hibernate.nativeapi.aop;

import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"spring.xml", Client.class);
		INativeApi nativeApi = (INativeApi) context.getBean(NativeApiImpl.class
				.getName());
		List<Demo> demoList = nativeApi.getAll();
		for (Demo demo : demoList) {
			System.out.println(ToStringBuilder.reflectionToString(demo));
		}
	}

}

 

你可能感兴趣的:(spring,AOP,mysql,Hibernate,bean)