java三大框架spring的JdbcTemplate连接数据库(C3P0)

通过配置文件配置先导入资源文件(properties文件,里面保存了数据库连接相关信息),然后配置C3P0数据源,最后配置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: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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置 C3P0 数据源 -->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///db_spring?useUnicode=true&characterEncoding=UTF-8"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>

		<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
	</bean>
	
	<!-- 配置 Spirng 的 JdbcTemplate -->
	<bean id="jdbcTemplate" 
		class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
</beans>

上面在jdbcUrl一行,我那样设置的原因是防止乱码。如果用其他属性那样设置为

${jdbc.jdbcUrl}就不能起到防止乱码的作用,具体原因也不晓得。

在java类中调用的代码为:

	public void testUpdate()
	{
		String sql="update t_student set name = ? where id =?";
		jdbcTemplate.update(sql, "张三",1);
	}
批量测试如下:

public void testBatchUpdate()
	{
		String sql="insert into t_student(name,age) values(?,?)";
		List<Object[]> batchArgs=new ArrayList<>();
		batchArgs.add(new Object[]{"张三2",55});
		batchArgs.add(new Object[]{"张李四2",44});
		batchArgs.add(new Object[]{"王五2",33});
		jdbcTemplate.batchUpdate(sql, batchArgs);
	}

需要的jar包有除了spring必须的外还需要jdbc包,c3p0包。

完整调用类为:

package com.levi.spring.jdbc;

import static org.junit.Assert.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanReferenceFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class JDBCTest
{
	private static ApplicationContext ac=null;
	private static JdbcTemplate jdbcTemplate;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
		ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		jdbcTemplate=(JdbcTemplate) ac.getBean("jdbcTemplate");
	}

	@Test
	public void testDataSourse() throws SQLException
	{
		DataSource dataSourse=ac.getBean(DataSource.class);
		System.out.println(dataSourse.getConnection());
	}
	/**
	 * 执行 INSERT, UPDATE, DELETE
	 */
	@Test
	public void testUpdate()
	{
		String sql="update t_student set name = ? where id =?";
		jdbcTemplate.update(sql, "张三",1);
	}
	@Test
	public void testBatchUpdate()
	{
		String sql="insert into t_student(name,age) values(?,?)";
		List<Object[]> batchArgs=new ArrayList<>();
		batchArgs.add(new Object[]{"张三2",55});
		batchArgs.add(new Object[]{"张李四2",44});
		batchArgs.add(new Object[]{"王五2",33});
		jdbcTemplate.batchUpdate(sql, batchArgs);
	}
	/**
	 * 从数据库中获取一条记录, 实际得到对应的一个对象
	 * 注意不是调用 queryForObject(String sql, Class<Employee> requiredType, Object... args) 方法!
	 * 而需要调用 queryForObject(String sql, RowMapper<Employee> rowMapper, Object... args)
	 * 1. 其中的 RowMapper 指定如何去映射结果集的行, 常用的实现类为 BeanPropertyRowMapper
	 * 2. 使用 SQL 中列的别名完成列名和类的属性名的映射. 例如 last_name lastName
	 * 3. 不支持级联属性. JdbcTemplate 到底是一个 JDBC 的小工具, 而不是 ORM 框架
	 */
	@Test
	public void testQueryForObject()
	{
		String sql="select *from t_student where id=?";
		RowMapper<Student> rowMapper=new BeanPropertyRowMapper<>(Student.class);
		Student student=jdbcTemplate.queryForObject(sql,rowMapper,2);
		System.out.println(student);
	}
	/**
	 * 获取单个列的值, 或做统计查询
	 * 使用 queryForObject(String sql, Class<Long> requiredType) 其中这个requiredType不能是用户自定义的类。
	 */
	@Test
	public void testForObject2()
	{
		String sql="select count(id) from t_student";
		Integer count=jdbcTemplate.queryForObject(sql, Integer.class);
		System.out.println(count);
	}
	/**
	 * 查到实体类的集合
	 * 注意调用的不是 queryForList 方法
	 */
	@Test
	public void testQueryForList()
	{
		String sql="select * from t_student";
		RowMapper< Student>rowMapper=new BeanPropertyRowMapper<>(Student.class);
		List<Student> list=jdbcTemplate.query(sql, rowMapper);
		System.out.println(list);
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值