Spring中使用JdbcTemplate操作数据库
2017-03-05 20:27
591 查看
1:配置C3p0数据源,配置Spring中JdbcTemplate
<?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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
<property name="minPoolSize" value="${jdbc.minPoolSize}"/>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
</bean>
<!-- 配置Spring中JdbcTemplate(Jdbc模板类) -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
2:db.properties
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mydatabase
jdbc.initialPoolSize=5
jdbc.minPoolSize=5
jdbc.maxPoolSize=10
3:Test
package com.spring.jdbc;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class Main {
public static void main(String[] args) throws SQLException {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-jdbc.xml");
DataSource dataSource=(DataSource)ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
JdbcTemplate jdbcTemplate=(JdbcTemplate)ctx.getBean("jdbcTemplate");
String sql="UPDATE emp SET ename=? WHERE empno=?";
jdbcTemplate.update(sql,"Jack",5);
String sql1="INSERT INTO emp(empno,ename,job,hiredate,sal) VALUES(?,?,?,?,?)";
List<Object[]> batchArgs=new ArrayList<>();
batchArgs.add(new Object[]{701,"struts2","控制层","2017-03-05",1000});
batchArgs.add(new Object[]{702,"Spring","业务层","2017-03-05",2000});
batchArgs.add(new Object[]{703,"Hiberante","持久层","2017-03-05",3000});
jdbcTemplate.batchUpdate(sql1,batchArgs);
}
}
<?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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
<property name="minPoolSize" value="${jdbc.minPoolSize}"/>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
</bean>
<!-- 配置Spring中JdbcTemplate(Jdbc模板类) -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
2:db.properties
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mydatabase
jdbc.initialPoolSize=5
jdbc.minPoolSize=5
jdbc.maxPoolSize=10
3:Test
package com.spring.jdbc;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class Main {
public static void main(String[] args) throws SQLException {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-jdbc.xml");
DataSource dataSource=(DataSource)ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
JdbcTemplate jdbcTemplate=(JdbcTemplate)ctx.getBean("jdbcTemplate");
String sql="UPDATE emp SET ename=? WHERE empno=?";
jdbcTemplate.update(sql,"Jack",5);
String sql1="INSERT INTO emp(empno,ename,job,hiredate,sal) VALUES(?,?,?,?,?)";
List<Object[]> batchArgs=new ArrayList<>();
batchArgs.add(new Object[]{701,"struts2","控制层","2017-03-05",1000});
batchArgs.add(new Object[]{702,"Spring","业务层","2017-03-05",2000});
batchArgs.add(new Object[]{703,"Hiberante","持久层","2017-03-05",3000});
jdbcTemplate.batchUpdate(sql1,batchArgs);
}
}
相关文章推荐
- Spring中的数据库操作--使用JdbcTemplate
- 使用Spring 内置的JdbcTemplate操作数据库
- 【Spring学习笔记】五、使用JDBCTemplate以对象方式操作数据库
- Spring使用JdbcTemplate实现对数据库操作
- spring-使用配置文件完成JdbcTemplate操作数据库-dbcp
- Spring使用JdbcTemplate实现对数据库操作 (内部类定义RowMapper)
- JDBC之使用Spring提供的JdbcTemplate进行数据库操作
- Spring Boot教程(二十九)使用JdbcTemplate操作数据库
- Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法
- Spring使用JdbcTemplate操作数据库---使用RowMapperResultSetExtractor读数据篇
- Spring (六) 使用Spring 内置的JdbcTemplate操作数据库
- Spring使用JdbcTemplate实现对数据库操作
- Spring使用JdbcTemplate实现对数据库操作
- Spring使用JdbcTemplate操作数据库---使用RowCallbackHander读数据篇
- Spring使用JdbcTemplate操作数据库
- spring 使用dataSource、JdbcTemplate简化数据库操作
- 使用Spring提供的三个JDBC模板类(JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate)操作数据库
- SpringBoot使用JdbcTemplate操作数据库
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十六)使用JdbcTemplate操作数据库
- Spring使用JdbcTemplate实现对数据库操作