标签:spring
使用Spring JDBC的步骤
1、引入jar包
spring-jdbc-3.2.5.RELEASE.jar
spring-tx-3.2.5.RELEASE.jar
c3p0-0.9.1.2.jar
mysql-connector-java-5.1.38-bin.jar
2、配置
c3p0数据库连接池的配置
JdbcTemplate对象
Dao对象
3、测试
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 1. 数据源对象: C3P0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="10"></property> <property name="maxStatements" value="100"></property> <property name="acquireIncrement" value="2"></property> </bean> <!-- 2. 创建JdbcTemplate对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg index="0" type="javax.sql.DataSource" ref="dataSource"></constructor-arg> </bean> <!-- dao 实例 --> <bean id="deptDao" class="com.rk.hibernate.i_jdbc.DepartmentDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans>
Department.java
package com.rk.hibernate.i_jdbc; public class Department { private int deptId; private String deptName; private int version; public int getDeptId() { return deptId; } public void setDeptId(int deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } @Override public String toString() { return "Department [deptId=" + deptId + ", deptName=" + deptName + ", version=" + version + "]"; } }
DepartmentDao.java
package com.rk.hibernate.i_jdbc; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; public class DepartmentDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void save() { String sql = "INSERT INTO T_Department(name,dept_version) values(?,?)"; jdbcTemplate.update(sql, "Tom",0); } public Department findById(int id) { String sql = "SELECT * FROM T_Department where id=?"; List<Department> list = jdbcTemplate.query(sql,new MyResult(),id); return (list != null && list.size()>0) ? list.get(0) : null; } public List<Department> findAll() { String sql = "SELECT * FROM T_Department"; List<Department> list = jdbcTemplate.query(sql, new MyResult()); return list; } } class MyResult implements RowMapper<Department> { @Override public Department mapRow(ResultSet rs, int rowNum) throws SQLException { int id = rs.getInt("id"); String name = rs.getString("name"); int dept_version = rs.getInt("dept_version"); Department dept = new Department(); dept.setDeptId(id); dept.setDeptName(name); dept.setVersion(dept_version); return dept; } }
App.java
package com.rk.hibernate.i_jdbc; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { private ApplicationContext ac = new ClassPathXmlApplicationContext("/com/rk/hibernate/i_jdbc/applicationContext.xml"); @Test public void test() { DepartmentDao deptDao = (DepartmentDao) ac.getBean("deptDao"); //1. 保存 // deptDao.save(); //2.根据id进行查询 // Department dept = deptDao.findById(9); // System.out.println(dept); //3.查询所有 List<Department> list = deptDao.findAll(); System.out.println(list.size()); System.out.println(list); } }
标签:spring
原文地址:http://lsieun.blog.51cto.com/9210464/1829123