Spring学习(六)


1 spring 配置c3p0连接池

第一步 导入jar包


第二步 创建spring配置文件,配置连接池

代码设置

//		ComboPooledDataSource dataSource=new ComboPooledDataSource();
//		dataSource.setDriverClass("com.mysql.jdbc.Driver");
//		dataSource.setJdbcUrl("jdbc:mysql:///spring_day03");
//		dataSource.setUser("root");
//		dataSource.setPassword("123456");

把代码在配置文件中配置

<?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" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->

		<!-- 配置c3p0连接池 -->
		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource.class">
			<!-- 注入属性值 -->
			<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
			<property name="JdbcUrl" value="jdbc:mysql:///spring_day03"></property>
			<property name="user" value="root"></property>
			<property name="password" value="123456"></property>
		</bean>
</beans>


2 dao使用jdbcTemplate

(1)创建service和dao,配置service和dao对象,在service注入dao对象

<!-- 创建service和dao对象,在service注入dao对象 -->
		<bean id="userService" class="cn.itcast.c3p0.UserService">
			<!-- 注入dao对象 -->
			<property name="userDao" ref="userDao"></property>
		</bean>
		<bean id="userDao" class="cn.itcast.c3p0.UserDao">

(2)创建jdbcTemplate对象,把模板对象注入到dao里面

package cn.itcast.c3p0;

import org.springframework.jdbc.core.JdbcTemplate;

public class UserDao {

	
	//得到JdbcTemplate对象
	private JdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
        }

	//添加操作
	public void add() {
		//创建jdbcTemplate对象
		//JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
		/*得到JdbcTemplate对象*/
		 
	}

}
(3)在jdbcTemplate对象里面注入dataSource
		<!-- 创建jdbcTemplate -->
		<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
			<!-- 把jdbcTemplate -->
			<property name="dataSource" ref="dataSource"></property>
		</bean>

测试

package cn.itcast.c3p0;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class testService {

	@Test
	public void testDemo() {
		ApplicationContext context=
				new ClassPathXmlApplicationContext("bean1.xml");
		UserService service=(UserService) context.getBean("userService");
		service.add();
	}
	
}

Spring的事物管理

1 事物概念

2 事物特性(acid)

3 不考虑隔离性产生读问题

4 解决读问题

(1)设置隔离级别

1 spring事物管理两种方式

第一种 编程式事务管理(不用)

第二种 声明式事务管理(主用)

(1)基于xml配置文件实现

(2)基于注解方式实现

2 spring事务管理api介绍

接口 PlatformTransactionManager

事务管理器

(1)spring针对不同的dao层框架提供接口不同的实现类。


(2)首先配置事务管理器

搭建转账环境

1 创建数据库表,添加数据


2 创建service和dao类,完成注入关系

(1)service层又叫业务逻辑层

(2)dao层,单纯对数据库操作层,在dao层不添加业务

(3)需求:小王 转账20给小马

-小王少20,-小马多20

package cn.itcast.service;

import cn.itcast.dao.OrderDao;

public class OrderService {

	private OrderDao orderDao;

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}
	//调用dao的方法
	//业务逻辑层,写转账业务
	public void accountMoney() {
		//小王少20
		orderDao.lessMoney();
		//小马多20
		orderDao.moreMoney();
	}
}
package cn.itcast.dao;

import org.springframework.jdbc.core.JdbcTemplate;

public class OrderDao {

	
	//注入jdbcTemplate模板
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	
	/*对数据库操作的方法,不写业务*/
	//小王少钱的方法
	public void lessMoney() {
		String sql="update account set salary=salary-? where username=?";
		jdbcTemplate.update(sql,20,"小王");
		
		
	}
	
	//小马多钱的方法
	public void moreMoney() {
		String sql="update account set salary=salary+? where username=?";
		jdbcTemplate.update(sql,20,"小马");
	}
	
	
	
	
}

配置文件

<?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: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/context
http://www.springframework.org/schema/context/spring-context.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.xsd">

		<!-- 配置c3p0连接池 -->
		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
			<!-- 注入属性值 -->
			<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
			<property name="JdbcUrl" value="jdbc:mysql:///spring_day03?useUnicode=true&characterEncoding=utf8"></property>
			<property name="user" value="root"></property>
			<property name="password" value="123456"></property>
		</bean>

		<bean id="orderService" class="cn.itcast.service.OrderService">
			<property name="orderDao" ref="orderDao"></property>
		</bean>
		<bean id="orderDao" class="cn.itcast.dao.OrderDao">
			<property name="jdbcTemplate" ref="jdbcTemplate"></property>
		</bean>
		<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
			<property name="dataSource" ref="dataSource"></property>
		</bean>
		
		
		
		
</beans>

进行测试

package cn.itcast.service;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestService {

	
	@Test
	public void testDemo() {
		ApplicationContext context=
				new ClassPathXmlApplicationContext("bean1.xml");
		OrderService service=(OrderService)context.getBean("orderService");
		service.accountMoney();
	}
}

3 产生问题:

(1)日鬼片小王少了20 之后,出现异常,小马不会多20,钱丢失了。


4 解决

(1)添加事务解决,出现异常进行回滚操作。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值