七、Spring JDBC - (一)Spring对数据持久层的支持

一、数据持久层分析



	数据连接层:
			 1.JDBC1.0数据连接层
			 		(使用JDBC1.0的API连接数据库 )
			 2.JDBC2.0
 			 		(使用JDBC2.0的API封装的数据源连接数据库 )
 			 					C3P0连接池	
 			 					DBCP连接池
 			
 	数据操作层:
			 1.JDBC1.0数据操作层
			 		(使用JDBC1.0的API操作数据库 )
			 
			 2.JDBC2.0数据操作层
			 		(使用QueryRunner操作数据库 )
			 		(使用JdbcTemplate操作数据库 )



	 1.利用JDBC1.0连接数据库并操作CURD
	 2.利用DbUtils组件的QryRunner操作数据库(无事务控制的支持)
	 3.利用连接池管理连接资源
	 4.利用hibernate连接数据库并操作CURD
	 5.利用spring连接数据库

二、SpringJDBC

	SpringJDBC 提供了数据连接层和数据操作层的支持。
			数据连接层 -- 利用spring创建数据源对象。
			数据操作层 -- 利用JdbcTemplate操作数据


	Spring对JDBC提供了很好的支持。
	体现在:
		  1)Spring对c3p0连接池的支持。
		    
		    可以支持很多连接池的参数配置
		  				(c3p0连接池有很多属性,
		  				  hibernate对c3p0连接池的支持属性只有几个。
		  				  而spring可以支持很多)
		  			
		  				  
		  2)spring对jdbc提供了JcbcTemplate来简化jdbc操作。
		     JdbcTemplate模板工具类,类似于DbUtils工具类(QueryRunner)。
	
		     

二、SpringJDBC提供了数据连接层的支持

		Spring对jdbc的支持实质上是
				1.利用Spring的IOC,控制反转创建数据源对象
				2.利用Spring的DI,实现属性注入、赋值等操作。
				

		Spring对Jdbc的支持主要是利用Spring容器可以创建对象的功能。
		Spring对C3P0连接池有很好的支持
		
2.1 直接注入Spring创建的数据源对象
>>>>>> applicationContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    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">
	
	<!-- 1.数据源对象:c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="6"></property>
		<property name="maxIdleTime" value="1000"></property>
	</bean>
	
	
	<bean id="userDao02" class="org.jsoft.e_jdbc.UserDao02">
		<property name="dataSource"  ref="dataSource"></property>
	</bean>


	
</beans>

>>>>>> UserDao02.java


/**
 * 	使用spring创建数据源
 * 
 * @author BGS
 *
 */
public class UserDao02 {

	private DataSource dataSource;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	

	/**
	 * 	保存方法
	 * 	需要优化的地方:
	 * 			  1.jdbc操作代码重复
	 */
	public void save()  {
		
		Statement st=null;
		Connection conn=null;
				
		try {
			String sql="insert into t_dept(t_id,t_name) values('331','xxxx')";
			
			//DataSource和Connetion的转化
			conn = dataSource.getConnection();
			
			st=conn.createStatement();
			st.execute(sql);
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}finally {
			try {
				st.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
		UserDao02	u=(UserDao02) ac.getBean("userDao02");
		u.save();
	}
	
}	

2.2 直接注入含有Spring创建的数据源对象JdbcTemplate对象
>>>>>> applicationContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    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">
	
	<!-- 1.数据源对象:c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="6"></property>
		<property name="maxIdleTime" value="1000"></property>
	</bean>
	
	
	<!--方式一:利用构造方法注入数据源
	<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
	</bean>
	-->	
	<!--方式二:利用set方法注入数据源-->
	<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<bean id="userDao04" class="org.jsoft.e_jdbc.UserDao04">
		<property name="jt"  ref="jdbcTemplate"></property>
	</bean>
	
	
	
</beans>


>>>>>> UserDao04.java


/**
 * 	使用JdbcTemplate简化重复代码,注入含有数据源的JdbcTemplate对象
 * 
 * @author BGS
 *
 */
public class UserDao04 {

	private JdbcTemplate jt;
	

	public void setJt(JdbcTemplate jdbcTemplate) {
		this.jt = jdbcTemplate;
	}

	/**
	 * 	新增
	 */
	public void save()  {
		String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
		jt.update(sql);
	}
	
	/**
	 * 	修改
	 */
	public void  update() {
		String sql="update t_dept set t_name=? where  t_id=?";
		jt.update(sql, "xxxwww",1331);
	}

三、SpringJDBC提供了数据操作层的支持

	
	
3.1 JdbcTemplate的API
		
		+++ 创建JdbcTemplate对象
			
			方式1:
				JdbcTemplate jt=new JdbcTemplate (dataSource)

			方式2:
				JdbcTemplate jt=new JdbcTemplate ()
				jt.setDataSource(dataSource)
	
	
		+++ 方法
			
				新增:
					String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
					jt.update(sql);
				
				修改:
					String sql="update t_dept set t_name=? where  t_id=?";
					jt.update(sql, "xxxwww",1331)		

				删除:
					String sql="delete from t_dept  where   t_id=?";
					jt.update(sql, 1331);


				
				查询单行数据:
					String sql="select * from t_dept where t_id=?";
					Map<String, Object> result = jt.queryForMap(sql, 1331);
				
				查询多行数据:			
					String sql="select * from t_dept";
					List<Map<String, Object>> result = jt.queryForList(sql);
				
				查询数据,并封装对象
					
					String sql="select * from t_dept where t_id=?";	
					//封装对象
					List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {
			
						@Override
						public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
							
							//方式一
							DeptEntity  dept=new DeptEntity();
							dept.setId(rs.getString("t_id"));
							dept.setName(rs.getString("t_name"));
							dept.setCode(rs.getString("t_code"));
							
							//方式二
							DeptEntity  dept2=new DeptEntity();
							dept2.setId(rs.getString(1));
							dept.setName(rs.getString(2));
							dept.setCode(rs.getString(3));
							
							return dept2;
						}
					},1331);

3.2 利用JdbcTemplate的构造方法创建含有数据源的对象
>>>>>> applicationContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    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">
	
	<!-- 1.数据源对象:c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="6"></property>
		<property name="maxIdleTime" value="1000"></property>
	</bean>
	
	
	<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
	</bean>
	
	<bean id="userDao04" class="org.jsoft.e_jdbc.UserDao04">
		<property name="jt"  ref="jdbcTemplate"></property>
	</bean>
	
	
	
</beans>


>>>>>> UserDao04.java


/**
 * 	使用JdbcTemplate简化重复代码,注入含有数据源的JdbcTemplate对象
 * 
 * @author BGS
 *
 */
public class UserDao04 {

	private JdbcTemplate jt;
	

	public void setJt(JdbcTemplate jdbcTemplate) {
		this.jt = jdbcTemplate;
	}

	/**
	 * 	新增
	 */
	public void save()  {
		String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
		jt.update(sql);
	}
	
	
	
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
		UserDao04	u=(UserDao04) ac.getBean("userDao04");
		u.query();
	}
	
}	

3.3 利用JdbcTemplate的setDataSource()方法创建含有数据源的对象
>>>>>> applicationContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    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">
	
	<!-- 1.数据源对象:c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="6"></property>
		<property name="maxIdleTime" value="1000"></property>
	</bean>
	
	
	<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource"  ref="dataSource"></property>
	</bean>
	
	<bean id="userDao04" class="org.jsoft.e_jdbc.UserDao04">
		<property name="jt"  ref="jdbcTemplate"></property>
	</bean>
	
	
	
</beans>


>>>>>> UserDao04.java


/**
 * 	使用JdbcTemplate简化重复代码,注入含有数据源的JdbcTemplate对象
 * 
 * @author BGS
 *
 */
public class UserDao04 {

	private JdbcTemplate jt;
	

	public void setJt(JdbcTemplate jdbcTemplate) {
		this.jt = jdbcTemplate;
	}

	/**
	 * 	新增
	 */
	public void save()  {
		String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
		jt.update(sql);
	}
	
	
	
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
		UserDao04	u=(UserDao04) ac.getBean("userDao04");
		u.query();
	}
	
}	

3.4 示例代码
>>>>>> applicationContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    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">
	
	<!-- 1.数据源对象:c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="6"></property>
		<property name="maxIdleTime" value="1000"></property>
	</bean>
	
	
	<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
	</bean>
	
	<bean id="userDao04" class="org.jsoft.e_jdbc.UserDao04">
		<property name="jt"  ref="jdbcTemplate"></property>
	</bean>
	
	
	
</beans>


>>>>>> UserDao04.java


/**
 * 	使用JdbcTemplate简化重复代码,注入含有数据源的JdbcTemplate对象
 * 
 * @author BGS
 *
 */
public class UserDao04 {

	private JdbcTemplate jt;
	

	public void setJt(JdbcTemplate jdbcTemplate) {
		this.jt = jdbcTemplate;
	}

	/**
	 * 	新增
	 */
	public void save()  {
		String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
		jt.update(sql);
	}
	
	/**
	 * 	修改
	 */
	public void  update() {
		String sql="update t_dept set t_name=? where  t_id=?";
		jt.update(sql, "xxxwww",1331);
	}
	
	
	/**
	 *      删除
	 */
	public void delete() {
		String sql="delete from t_dept  where   t_id=?";
		jt.update(sql, 1331);
	}
	
	/**
	 * 	查询
	 * 		queryForMap	查询一行数据
	 */
	public void  findById() {
		String sql="select * from t_dept where t_id=?";
		Map<String, Object> result = jt.queryForMap(sql, 1331);
		System.out.println(result);
	}
	
	
	/**
	 * 	查询
	 * 		queryForList 查询多行数据
	 */
	public void  querys() {
		String sql="select * from t_dept";
		List<Map<String, Object>> result = jt.queryForList(sql);
		System.out.println(result);
	}
	
	

	
	/**
	 * 	查询1
	 * 		query 【封装对象】
	 */
	public void  query() {
		String sql="select * from t_dept where t_id=?";
		
		//封装对象
		List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {

			@Override
			public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
				
				//方式一
				DeptEntity  dept=new DeptEntity();
				dept.setId(rs.getString("t_id"));
				dept.setName(rs.getString("t_name"));
				dept.setCode(rs.getString("t_code"));
				
				//方式二
				DeptEntity  dept2=new DeptEntity();
				dept2.setId(rs.getString(1));
				dept.setName(rs.getString(2));
				dept.setCode(rs.getString(3));
				
				return dept2;
			}
		},1331);
		
		
		System.out.println(result);
	}
	
	
	/**
	 * 	查询2
	 * 		query【封装对象】
	 */
	public void  query2() {
		String sql="select * from t_dept";
		
		//封装对象
		List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {

			@Override
			public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
				
				//方式一
				DeptEntity  dept=new DeptEntity();
				dept.setId(rs.getString("t_id"));
				dept.setName(rs.getString("t_name"));
				dept.setCode(rs.getString("t_code"));
				
				//方式二
				DeptEntity  dept2=new DeptEntity();
				dept2.setId(rs.getString(1));
				dept.setName(rs.getString(2));
				dept.setCode(rs.getString(3));
				
				return dept2;
			}
		});
		
		
		System.out.println(result);
	}
	

	/**
	 * 	查询3
	 * 		query 【封装对象】
	 */
	public void  query3() {
		String sql="select * from t_dept";
		
		//封装对象
		List<DeptEntity> result = jt.query(sql, new DeptRow());
	
		System.out.println(result);
	}
	
	class  DeptRow  implements  RowMapper<DeptEntity>{

		@Override
		public DeptEntity mapRow(ResultSet rs, int arg1) throws SQLException {
			//方式一
			DeptEntity  dept=new DeptEntity();
			dept.setId(rs.getString("t_id"));
			dept.setName(rs.getString("t_name"));
			dept.setCode(rs.getString("t_code"));
			
			return dept;
		}
		
	}
	
	
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
		UserDao04	u=(UserDao04) ac.getBean("userDao04");
		u.query();
	}
	
}	

四、SpringJDBC的演变过程

2.1 手动创建数据源
>>>>>> applicationContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    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">
	
	<bean id="userDao01" class="org.jsoft.e_jdbc.UserDao01"></bean>
</beans>

>>>>>> UserDao01.java

/**
 * 	手动创建数据源。最原始的JDBC连接
 * 
 * @author BGS
 *
 */
public class UserDao01 {



	/**
	 * 	保存方法
	 * 	需要优化的地方:
	 * 			  1.连接的管理
	 * 			  2.jdbc操作代码重复
	 */
	public void save()  {
		
		Statement st=null;
		Connection conn=null;
				
		try {
			String sql="insert into t_dept(t_id,t_name) values('33','xxxx')";
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
			st=conn.createStatement();
			st.execute(sql);
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}finally {
			try {
				st.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
		UserDao01	u=(UserDao01) ac.getBean("userDao01");
		u.save();
	}
	
}	

2.2 直接注入Spring创建的数据源
>>>>>> applicationContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    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">
	
	<!-- 1.数据源对象:c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="6"></property>
		<property name="maxIdleTime" value="1000"></property>
	</bean>
	
	

	<bean id="userDao02" class="org.jsoft.e_jdbc.UserDao02">
		<property name="dataSource"  ref="dataSource"></property>
	</bean>

	
</beans>

>>>>>> UserDao02.java


/**
 * 	使用spring创建数据源
 * 
 * @author BGS
 *
 */
public class UserDao02 {

	private DataSource dataSource;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	

	/**
	 * 	保存方法
	 * 	需要优化的地方:
	 * 			  1.jdbc操作代码重复
	 */
	public void save()  {
		
		Statement st=null;
		Connection conn=null;
				
		try {
			String sql="insert into t_dept(t_id,t_name) values('331','xxxx')";
			
			//DataSource和Connetion的转化
			conn = dataSource.getConnection();
			
			st=conn.createStatement();
			st.execute(sql);
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}finally {
			try {
				st.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
		UserDao02	u=(UserDao02) ac.getBean("userDao02");
		u.save();
	}
	
}	

2.3 直接注入Spring创建的数据源,并使用JdbcTemplate简化操作
>>>>>> applicationContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    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">
	
	<!-- 1.数据源对象:c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="6"></property>
		<property name="maxIdleTime" value="1000"></property>
	</bean>
	
	

	<bean id="userDao03" class="org.jsoft.e_jdbc.UserDao03">
		<property name="dataSource"  ref="dataSource"></property>
	</bean>
	

</beans>


>>>>>> UserDao03.java




/**
 * 	使用JdbcTemplate简化重复代码,注入数据源对象
 * 
 * @author BGS
 *
 */
public class UserDao03 {

	private DataSource dataSource;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	
	/**
	 * 	新增
	 */
	public void save()  {
		String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
		JdbcTemplate  jt=new JdbcTemplate(dataSource);
		jt.update(sql);
	}
	
	/**
	 * 	修改
	 */
	public void  update() {
		String sql="update t_dept set t_name=? where  t_id=?";
		JdbcTemplate  jt=new JdbcTemplate(dataSource);
		jt.update(sql, "xxxwww",1331);
	}
	
	
	/**
	 *      删除
	 */
	public void delete() {
		String sql="delete from t_dept  where   t_id=?";
		JdbcTemplate  jt=new JdbcTemplate(dataSource);
		jt.update(sql, 1331);
	}
	
	/**
	 * 	查询
	 * 		queryForMap	查询一行数据
	 */
	public void  findById() {
		String sql="select * from t_dept where t_id=?";
		JdbcTemplate  jt=new JdbcTemplate(dataSource);
		Map<String, Object> result = jt.queryForMap(sql, 1331);
		System.out.println(result);
	}
	
	
	/**
	 * 	查询
	 * 		queryForList 查询多行数据
	 */
	public void  querys() {
		String sql="select * from t_dept";
		JdbcTemplate  jt=new JdbcTemplate(dataSource);
		List<Map<String, Object>> result = jt.queryForList(sql);
		System.out.println(result);
	}
	
	

	
	/**
	 * 	查询1
	 * 		query 【封装对象】
	 */
	public void  query() {
		String sql="select * from t_dept where t_id=?";
		JdbcTemplate  jt=new JdbcTemplate(dataSource);
		
		//封装对象
		List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {

			@Override
			public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
				
				//方式一
				DeptEntity  dept=new DeptEntity();
				dept.setId(rs.getString("t_id"));
				dept.setName(rs.getString("t_name"));
				dept.setCode(rs.getString("t_code"));
				
				//方式二
				DeptEntity  dept2=new DeptEntity();
				dept2.setId(rs.getString(1));
				dept.setName(rs.getString(2));
				dept.setCode(rs.getString(3));
				
				return dept2;
			}
		},1331);
		
		
		System.out.println(result);
	}
	
	
	/**
	 * 	查询2
	 * 		query【封装对象】
	 */
	public void  query2() {
		String sql="select * from t_dept";
		JdbcTemplate  jt=new JdbcTemplate(dataSource);
		
		//封装对象
		List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {

			@Override
			public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
				
				//方式一
				DeptEntity  dept=new DeptEntity();
				dept.setId(rs.getString("t_id"));
				dept.setName(rs.getString("t_name"));
				dept.setCode(rs.getString("t_code"));
				
				//方式二
				DeptEntity  dept2=new DeptEntity();
				dept2.setId(rs.getString(1));
				dept.setName(rs.getString(2));
				dept.setCode(rs.getString(3));
				
				return dept2;
			}
		});
		
		
		System.out.println(result);
	}
	

	/**
	 * 	查询3
	 * 		query 【封装对象】
	 */
	public void  query3() {
		String sql="select * from t_dept";
		JdbcTemplate  jt=new JdbcTemplate(dataSource);
		
		//封装对象
		List<DeptEntity> result = jt.query(sql, new DeptRow());
	
		System.out.println(result);
	}
	
	class  DeptRow  implements  RowMapper<DeptEntity>{

		@Override
		public DeptEntity mapRow(ResultSet rs, int arg1) throws SQLException {
			//方式一
			DeptEntity  dept=new DeptEntity();
			dept.setId(rs.getString("t_id"));
			dept.setName(rs.getString("t_name"));
			dept.setCode(rs.getString("t_code"));
			
			return dept;
		}
		
	}
	
	
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
		UserDao03	u=(UserDao03) ac.getBean("userDao03");
		u.delete();
	}
	
}	

2.4 直接注入含有spring创建的数据源的JdbcTemplate
>>>>>> applicationContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    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">
	
	<!-- 1.数据源对象:c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="6"></property>
		<property name="maxIdleTime" value="1000"></property>
	</bean>
	
	
	<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
	</bean>
	
	<bean id="userDao04" class="org.jsoft.e_jdbc.UserDao04">
		<property name="jt"  ref="jdbcTemplate"></property>
	</bean>
	
	
	
</beans>


>>>>>> UserDao04.java


/**
 * 	使用JdbcTemplate简化重复代码,注入含有数据源的JdbcTemplate对象
 * 
 * @author BGS
 *
 */
public class UserDao04 {

	private JdbcTemplate jt;
	

	public void setJt(JdbcTemplate jdbcTemplate) {
		this.jt = jdbcTemplate;
	}

	/**
	 * 	新增
	 */
	public void save()  {
		String sql="insert into t_dept(t_id,t_name) values('1331','xxxx')";
		jt.update(sql);
	}
	
	/**
	 * 	修改
	 */
	public void  update() {
		String sql="update t_dept set t_name=? where  t_id=?";
		jt.update(sql, "xxxwww",1331);
	}
	
	
	/**
	 *      删除
	 */
	public void delete() {
		String sql="delete from t_dept  where   t_id=?";
		jt.update(sql, 1331);
	}
	
	/**
	 * 	查询
	 * 		queryForMap	查询一行数据
	 */
	public void  findById() {
		String sql="select * from t_dept where t_id=?";
		Map<String, Object> result = jt.queryForMap(sql, 1331);
		System.out.println(result);
	}
	
	
	/**
	 * 	查询
	 * 		queryForList 查询多行数据
	 */
	public void  querys() {
		String sql="select * from t_dept";
		List<Map<String, Object>> result = jt.queryForList(sql);
		System.out.println(result);
	}
	
	

	
	/**
	 * 	查询1
	 * 		query 【封装对象】
	 */
	public void  query() {
		String sql="select * from t_dept where t_id=?";
		
		//封装对象
		List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {

			@Override
			public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
				
				//方式一
				DeptEntity  dept=new DeptEntity();
				dept.setId(rs.getString("t_id"));
				dept.setName(rs.getString("t_name"));
				dept.setCode(rs.getString("t_code"));
				
				//方式二
				DeptEntity  dept2=new DeptEntity();
				dept2.setId(rs.getString(1));
				dept.setName(rs.getString(2));
				dept.setCode(rs.getString(3));
				
				return dept2;
			}
		},1331);
		
		
		System.out.println(result);
	}
	
	
	/**
	 * 	查询2
	 * 		query【封装对象】
	 */
	public void  query2() {
		String sql="select * from t_dept";
		
		//封装对象
		List<DeptEntity> result = jt.query(sql, new RowMapper<DeptEntity>() {

			@Override
			public DeptEntity mapRow(ResultSet rs, int index) throws SQLException {
				
				//方式一
				DeptEntity  dept=new DeptEntity();
				dept.setId(rs.getString("t_id"));
				dept.setName(rs.getString("t_name"));
				dept.setCode(rs.getString("t_code"));
				
				//方式二
				DeptEntity  dept2=new DeptEntity();
				dept2.setId(rs.getString(1));
				dept.setName(rs.getString(2));
				dept.setCode(rs.getString(3));
				
				return dept2;
			}
		});
		
		
		System.out.println(result);
	}
	

	/**
	 * 	查询3
	 * 		query 【封装对象】
	 */
	public void  query3() {
		String sql="select * from t_dept";
		
		//封装对象
		List<DeptEntity> result = jt.query(sql, new DeptRow());
	
		System.out.println(result);
	}
	
	class  DeptRow  implements  RowMapper<DeptEntity>{

		@Override
		public DeptEntity mapRow(ResultSet rs, int arg1) throws SQLException {
			//方式一
			DeptEntity  dept=new DeptEntity();
			dept.setId(rs.getString("t_id"));
			dept.setName(rs.getString("t_name"));
			dept.setCode(rs.getString("t_code"));
			
			return dept;
		}
		
	}
	
	
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/e_jdbc/applicationContext.xml");
		UserDao04	u=(UserDao04) ac.getBean("userDao04");
		u.query();
	}
	
}	

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值