spring 之 访问数据库

spring datasource(jdbc)

bean.xml配置

	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>
	<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		
		<property name="initialSize" value="1"/>  <!-- 连技池启动时的初始值 -->
		<property name="maxActive" value="500"/>  <!-- 连接池的最大值 -->
		<property name="maxIdle" value="2"/>      <!-- 最大空闲值,访问少时,保留的连接数-->
		<property name="minIdle" value="1"/>	  <!-- 最小空闲值,空闲的连接数少于阀值时,连接池去预申请一些连接 -->
	</bean>
	
	<tx:annotation-driven transaction-manager="txManager"/>   <!-- enable the configuration of transactional behavior based on annotations -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   		 <property name="dataSource" ref="dataSource"/> 
	</bean>
注入数据源:

	private DataSource dataSource;//将数据源注入
	public void add(){
		try {
			Connection conn=dataSource.getConnection();
			conn.createStatement().executeUpdate("insert into user values(null,'ljf')");
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("save!!!");
	}
	@Resource
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
jdbc.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=

spring 整合hibernate

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

	<aop:aspectj-autoproxy />      <!--Enables the use of the @AspectJ style of Spring AOP -->
	<context:annotation-config />  <!-- Activates various annotations to be detected in bean classes -->
	<context:component-scan base-package="com.test" />
	
	<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/test" />
		<property name="username" value="root" />
		<property name="password" value="" />
		
		<property name="initialSize" value="1"/>  <!-- 连技池启动时的初始值 -->
		<property name="maxActive" value="500"/>  <!-- 连接池的最大值 -->
		<property name="maxIdle" value="2"/>      <!-- 最大空闲值,访问少时,保留的连接数-->
		<property name="minIdle" value="1"/>	  <!-- 最小空闲值,空闲的连接数少于阀值时,连接池去预申请一些连接 -->
	</bean>
	
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><!-- LocalSessionFactoryBean -->
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan"><!--自动扫描实体类  -->
			<list>
				<value>com.test.domain</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
				hibernate.show_sql=true
			</value>
		</property>
	</bean>
	
	 
	<!-- java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session 搜索后,说spring3.1不再建议使用!! -->
	<!--<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>-->
	
	<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
   		 <property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="txManager"/>   <!-- enable the configuration of transactional behavior based on annotations -->
	
</beans> 

对于HibernateTemplate ,spring 3.0已经建议不再使用了……http://forum.springsource.org/showthread.php?117227-Missing-Hibernate-Classes-Interfaces-in-spring-orm-3.1.0.RC1

service层:

@Service("userService")
@Transactional
public class UserServiceBean {
	@Resource
	private SessionFactory sessionFactory;//将数据源注入
	public void save(User user){
		Session session=sessionFactory.getCurrentSession();
		session.save(user);
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
事务应放在service层里
注解方式:
<!-- 使用基于注解方式配置事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

@Transactional         
@Transactional(readOnly=true)
@Transactional(propagation=Propagation.NOT_SUPPORTED)

xml方式

<!-- 以下 为事务配置 -->
	<bean id="txManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<aop:config>
		<aop:pointcut id="bussinessService"
			expression="execution(public * com.spring.service..*.*(..))" />
		<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
	</aop:config>
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
</beans>




























   

你可能感兴趣的:(spring 之 访问数据库)