Spring使用c3p0实现数据增删改查
一、导入Spring、c3p0、mysql的jar包
二、配置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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 实例化c3p0对象 --> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK"></property> <property name="user" value="root"></property> <property name="password" value=""></property> <!--依次为数据库连接池最小连接数、最大连接数、初始连接数--> <property name="minPoolSize" value="3"></property> <property name="maxPoolSize" value="15"></property> <property name="initialPoolSize" value="3"></property> </bean> <!-- 配置JDBCTemplate --> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
三、编写代码
Nation类
package maya.entities; public class Nation { private String code; private String name; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Test类
package maya.test; import java.sql.Connection; 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.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import com.mchange.v2.c3p0.ComboPooledDataSource; import maya.entities.Nation; public class TestC3P0 {
//查单行单列 public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); DataSource ds = (DataSource)context.getBean("dataSource"); JdbcTemplate j = (JdbcTemplate)context.getBean("jdbcTemplate"); //查询 String sql = "select count(*) from nation"; long count = j.queryForObject(sql,Long.class); System.out.println(count); }
//查全部 public static void main8(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); DataSource ds = (DataSource)context.getBean("dataSource"); JdbcTemplate j = (JdbcTemplate)context.getBean("jdbcTemplate"); //查询 String sql = "select * from nation"; RowMapper<Nation> rw = new BeanPropertyRowMapper<Nation>(Nation.class); List<Nation> list = j.query(sql, rw); for (Nation data : list) { System.out.println(data.getName()); } }
//查单个 public static void main7(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); DataSource ds = (DataSource)context.getBean("dataSource"); JdbcTemplate j = (JdbcTemplate)context.getBean("jdbcTemplate"); //查询 String sql = "select * from nation where code=?"; RowMapper<Nation> rm = new BeanPropertyRowMapper<Nation>(Nation.class); Nation data = j.queryForObject(sql, rm,"n005"); System.out.println(data.getName()); }
//批量增加 public static void main6(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); DataSource ds = (DataSource)context.getBean("dataSource"); JdbcTemplate j = (JdbcTemplate)context.getBean("jdbcTemplate"); //增加 String sql = "insert into nation values(?,?) "; //使用数组 List<Object[]> batchArgs = new ArrayList<Object[]>(); batchArgs.add(new Object[] {"n005","侗族"}); batchArgs.add(new Object[] {"n006","朝鲜族"}); j.batchUpdate(sql,batchArgs); }
//删除单个 public static void main5(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); DataSource ds = (DataSource)context.getBean("dataSource"); JdbcTemplate j = (JdbcTemplate)context.getBean("jdbcTemplate"); //删除 String sql = "delete from nation where code=?"; j.update(sql,"n005"); }
//增加单个 public static void main4(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); DataSource ds = (DataSource)context.getBean("dataSource"); JdbcTemplate j = (JdbcTemplate)context.getBean("jdbcTemplate"); //增加 String sql = "insert into nation value(?,?)"; j.update(sql, "n005", "侗族"); }
//修改单个 public static void main3(String[] args) { //JdbcTemplate。加载xml文件,创建连接池,建立连接 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); DataSource ds = (DataSource)context.getBean("dataSource"); JdbcTemplate j = (JdbcTemplate)context.getBean("jdbcTemplate"); //修改 String sql = "update info set name=? where code=?"; j.update(sql, "张琳", "p005");//update方法可以执行insert,update,delete } }
还可把sql语句写作用于传递外部参数为语句中变量赋值(:=)的方式
1.配置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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK"></property> <property name="user" value="root"></property> <property name="password" value=""></property> <property name="minPoolSize" value="3"></property> <property name="maxPoolSize" value="15"></property> <property name="initialPoolSize" value="3"></property> </bean> <!-- 命名的JDBCTemplate --> <bean class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" id="namedParameterJdbcTemplate"> <!--方法注入--> <constructor-arg ref="dataSource"></constructor-arg> </bean> </beans>
2.写代码
方法一:
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); DataSource ds = (DataSource)context.getBean("dataSource"); NamedParameterJdbcTemplate j = (NamedParameterJdbcTemplate)context.getBean("namedParameterJdbcTemplate"); String sql = "select * from nation where code=:code"; Map<String, String> paramMap = new HashMap<String,String>(); paramMap.put("code", "n003"); RowMapper<Nation> rw = new BeanPropertyRowMapper<Nation>(Nation.class); Nation data = j.queryForObject(sql, paramMap, rw); //输出查看结果 System.out.println(data.getName()); }
方法二:
public static void main(String[] args) throws Exception{ ComboPooledDataSource ds = new ComboPooledDataSource(); ds.setDriverClass("com.mysql.jdbc.Driver"); ds.setJdbcUrl("jdbc:mysql://localhost:3306/mydb?characterEncoding"); ds.setUser("root"); ds.setPassword(""); ds.setMinPoolSize(5); ds.setMaxPoolSize(15); NamedParameterJdbcTemplate j = new NamedParameterJdbcTemplate(ds); String sql = "select * from nation where code=:code"; Map<String, String> paramMap = new HashMap<String,String>(); paramMap.put("code", "n003"); RowMapper<Nation> rm = new BeanPropertyRowMapper<Nation>(Nation.class); Nation data = j.queryForObject(sql, paramMap, rm); System.out.println(data.getName()); }