Spring-JDBC

本文介绍了如何在Spring中配置c3p0连接池,并提供了关联的Jar包信息,包括mchange-commons-java.jar、c3p0-0.9.5.2.jar和mysql-connector-java-5.1.22-bin.jar等。接着展示了配置c3p0.properties和jdbcconfig.xml的步骤,并通过JDBCTest.java测试了jdbc连接。文章还提及了NamedParameterJdbcTemplate,它是JdbcTemplate的增强版,具有更好的代码可读性和错误检查能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关联Jar包

若通过c3p0访问jdbc,则需要添加以下jar包

1)mchange-commons-java.jar

http://mvnrepository.com/artifact/com.mchange/mchange-commons-java/

2)c3p0-0.9.5.2.jar

http://mvnrepository.com/artifact/com.mchange/c3p0/

3)mysql-connector-java-5.1.22-bin.jar

4)spring-jdbc-4.2.6.RELEASE.jar

配置c3p0.properties

user=root
password=1234
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/high

initialPoolSize=5
maxPoolSize=10

配置jdbcconfig.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"
	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-4.2.xsd">
	<context:property-placeholder location="c3p0.properties"/>
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property>
		<property name="driverClass" value="${driverClass}"></property>
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		<property name="initialPoolSize"  value="${initialPoolSize}"></property>
		<property name="maxPoolSize" value="${maxPoolSize}"></property>
	</bean>
</beans>

测试jdbc连接 JDBCTest.java
public class JDBCTest {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("jdbcconfig.xml");
        DataSource dataSource = ctx.getBean("dataSource", ComboPooledDataSource.class);  
        try {  
            System.out.println(dataSource.getConnection());  
        } catch (SQLException e) {  
            e.printStackTrace();  
        } 
		
		//JdbcTemplate为线程安全类
		JdbcTemplate jdbcTemplate = ctx.getBean("jdbcTemplate", JdbcTemplate.class);
		
		//插入,更新,删除通用。
		String sql = "update student set name='high2' where id=?";
		jdbcTemplate.update(sql, 27);
		
		//批量插入举例,通过将参数放入list中,进行批量操作
		String insertSql = "insert into student (name, age) values(?, ?)";
		List<Object[]> batchArgs = new ArrayList<Object[]>();
		batchArgs.add(new Object[]{"lily", 20});
		batchArgs.add(new Object[]{"lucy", 21});
		
		jdbcTemplate.batchUpdate(insertSql, batchArgs);
		
		//查询一个对象,当查询的字段和类的属性不同时,可以使用别名,相同时则不需要
		String querySql = "select id as id, name, age from student where id=?";
		RowMapper<Student> rowMapper = new BeanPropertyRowMapper<Student>(Student.class);
		Student student = jdbcTemplate.queryForObject(querySql, rowMapper, 30);
		System.out.println(student);
		
		//查询一个Scalar数据
		String querySqlForScalar = "select name from student where id=?";
		String name = jdbcTemplate.queryForObject(querySqlForScalar, String.class, 30);
		System.out.println(name);
		
		//查询一个列表
		String querySqlForList = "select id as id, name, age from student where id<?";
		RowMapper<Student> rowMapper2 = new BeanPropertyRowMapper<Student>(Student.class);
		List<Student> students = jdbcTemplate.query(querySqlForList, rowMapper2, 30);
		System.out.println(students);
		
		//做统计查询
		String querySqlForCount = "select count(name) from student where name=?";
		long count = jdbcTemplate.queryForObject(querySqlForCount, Long.class, "high");
		System.out.println(count);
	}
}
NamedParameterJdbcTemplate

NamedParameterJdbcTemplate翻译为中文是具名参数,即具有名字的参数,他是对JdbcTemplate的封装,所以,JdbcTemplate具有的功能他都有。

下面直接引用Spring官网的例子,可见带有名字的参数虽然比上面的问号要麻烦一点。

但是在使用的时候可以直接使用名字,这样在代码可读性方面,检查正确性方面相对比较好。

// some JDBC-backed DAO class...
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public void setDataSource(DataSource dataSource) {
    this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}

public int countOfActors(Actor exampleActor) {

    // notice how the named parameters match the properties of the above 'Actor' class
    String sql = "select count(*) from T_ACTOR where first_name = :firstName and last_name = :lastName";

    SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(exampleActor);

    return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
}

<完>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值