Spring框架(三)--------- 整合jdbc $ 事务

一.spring整合jdbc

1.配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
	<!-- 指定spring读取db.properties配置 文件-->
<context:property-placeholder location="classpath:db.properties"  />


<!-- 1.将连接池放入spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
	<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
	<property name="driverClass" value="${jdbc.driverClass}" ></property>
	<property name="user" value="${jdbc.user}" ></property>
	<property name="password" value="${jdbc.password}" ></property>
</bean>




<!-- 2.将JDBCTemplate放入spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
	<property name="dataSource" ref="dataSource" ></property>
</bean>


<!-- 3.将UserDao放入spring容器 -->
<bean name="userDao" class="com.auicyh.a_jdbctemplate.UserDaoImpl" >
	<property name="jt" ref="jdbcTemplate" ></property>
	<!-- <property name="dataSource" ref="dataSource" ></property> -->
</bean>


</beans>

2.db.properties文件


3.UserDao.java

public interface UserDao {
	// 增
	void save(User u);

	// 删
	void delete(Integer id);

	// 改
	void update(User u);

	// 查
	User getById(Integer id);

	// 查
	int getTotalCount();

	// 查
	List<User> getAll();
}

4.UserDaoImpl.java

public class UserDaoImpl implements UserDao{
	
	private JdbcTemplate jt;

	/**
	 * 为什么这里设置set方法
	 * @param jt
	 */
	public void setJt(JdbcTemplate jt) {
		System.out.println("jt = "+jt);
		this.jt = jt;
	}
	@Override
	public void save(User u) {
		String sql = "insert into user values('1111131111111111',?,null,null,null,null,null,null,null,null) ";
		jt.update(sql, u.getUsername());
	}
	@Override
	public void delete(Integer id) {
		String sql = "delete from t_user where id = ? ";
		jt.update(sql,id);
	}
	@Override
	public void update(User u) {
		String sql = "update  t_user set name = ? where id=? ";
		jt.update(sql, u.getName(),u.getUid());
	}
	@Override
	public User getById(Integer id) {
		String sql = "select * from t_user where id = ? ";
		return jt.queryForObject(sql,new RowMapper<User>(){
			@Override
			public User mapRow(ResultSet rs, int arg1) throws SQLException {
				User u = new User();
				u.setUid(rs.getString("uid"));
				u.setName(rs.getString("username"));
				return u;
			}}, id);
		
	}
	@Override
	public int getTotalCount() {
		String sql = "select count(*) from user  ";
		Integer count = jt.queryForObject(sql, Integer.class);
		return count;
	}

	@Override
	public List<User> getAll() {
		String sql = "select * from user  ";
		List<User> list = jt.query(sql, new RowMapper<User>(){
			@Override
			public User mapRow(ResultSet rs, int arg1) throws SQLException {
				User u = new User();
				u.setUid(rs.getString("uid"));
				u.setUsername(rs.getString("username"));
				return u;
			}});
		return list;
	}
}

测试类

//演示JDBC模板
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
	@Resource(name="userDao")
	private UserDao ud;
	
	@Test
	public void fun1() throws Exception{
		
		//0 准备连接池
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql:///obd");
		dataSource.setUser("root");
		dataSource.setPassword("123456");
		//1 创建JDBC模板对象
		JdbcTemplate jt = new JdbcTemplate();
		jt.setDataSource(dataSource);
		//2 书写sql,并执行
		String sql = "insert into user values('1234567890-09','rose',null,null,null,null,null,null,null,null) ";
		jt.update(sql);
		
	}
	
	@Test
	public void fun2() throws Exception{
		User u = new User();
		u.setUsername("tom");
		ud.save(u);
	}
	@Test
	public void fun3() throws Exception{
		User u = new User();
		u.setUid("2");
		u.setName("jack");
		ud.update(u);
		
	}
	
	@Test
	public void fun4() throws Exception{
		ud.delete(2);
	}
	
	@Test
	public void fun5() throws Exception{
		System.out.println(ud.getTotalCount());
	}
	
	@Test
	public void fun6() throws Exception{
		System.out.println(ud.getById(1));
	}
	
	@Test
	public void fun7() throws Exception{
		System.out.println(ud.getAll());
	}
	
}

二.spring管理事务的方式

1.编码方式

    1.1 将核心事务管理器配置到spring容器


    1.2 配置TransactionTemplate模板


    1.3 将事务模板注入Service


    1.4 在Service中调用模板


2.xml配置方式


 2.3 配置通知

    

2.4 配置将通知织入目标


3.注解方式

3.1 导包


    3.2 导入新的约束

    

 3.3 开启注解管理事务


3.4 使用注解




阅读更多
文章标签: Spring jdbc aop
个人分类: spring
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭