您的位置:首页 > 编程语言 > Java开发

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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: