Spring对数据库的操作,使用JdbcTemplate对象
需要引入相关的jar文件
如版本:(Spring核心jar包就不列了)
spring-jdbc-3.2.5.RELEASE.jar
spring-tx-3.2.5.RELEASE.jar
C3P0连接池:c3p0-0.9.1.2.jar
数据库驱动包:mysql-connector-java-5.1.22-bin.jar
例:(对象的获取及注入通过spring来实现)
核心jdbc操作
1 package test.jdbc; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.text.SimpleDateFormat; 6 import java.util.List; 7 import java.util.Map; 8 9 import org.springframework.jdbc.core.JdbcTemplate; 10 import org.springframework.jdbc.core.RowMapper; 11 12 public class StudentDao { 13 14 private JdbcTemplate jdbcTemplate; 15 16 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 17 this.jdbcTemplate = jdbcTemplate; 18 } 19 20 public Student getOneById(String id){ 21 List<Student> list = jdbcTemplate.query( 22 "select * from student where id = ?" 23 , new objectRowMapper() 24 , id); 25 return (list!=null && list.size() > 0) ?list.get(0):null; 26 } 27 public List<Student> getAll(){ 28 List<Student> list = jdbcTemplate.query( 29 "select * from student" 30 , new objectRowMapper()); 31 return list; 32 } 33 34 class objectRowMapper implements RowMapper<Student>{ 35 36 public Student mapRow(ResultSet rs, int index) throws SQLException { 37 Student std = new Student(); 38 std.setId(rs.getInt("id")); 39 std.setSname(rs.getString("sname")); 40 std.setSclass(rs.getInt("sclass")); 41 if(rs.getTimestamp("startdate")!=null){ 42 std.setStartdate(new java.util.Date(rs.getTimestamp("startdate").getTime()) ); 43 } 44 if(rs.getDate("stopdate")!=null){ 45 std.setStopdate(rs.getDate("stopdate")); 46 } 47 System.out.println(index); 48 return std; 49 } 50 51 } 52 }
Student实体类:
1 package test.jdbc; 2 3 import java.util.Date; 4 5 public class Student { 6 private int sid; 7 private String sname; 8 private int sclass; 9 private Date startdate; 10 private Date stopdate; 11 12 public int getId() { 13 return sid; 14 } 15 public void setId(int id) { 16 this.sid = id; 17 } 18 19 public Date getStartdate() { 20 return startdate; 21 } 22 public void setStartdate(Date startdate) { 23 this.startdate = startdate; 24 } 25 public Date getStopdate() { 26 return stopdate; 27 } 28 public void setStopdate(Date stopdate) { 29 this.stopdate = stopdate; 30 } 31 public String getSname() { 32 return sname; 33 } 34 public void setSname(String sname) { 35 this.sname = sname; 36 } 37 public int getSclass() { 38 return sclass; 39 } 40 public void setSclass(int sclass) { 41 this.sclass = sclass; 42 } 43 @Override 44 public String toString() { 45 return "Student [sid=" + sid + ", sname=" + sname + ", sclass=" 46 + sclass + ", startdate=" + startdate + ", stopdate=" 47 + stopdate + "]"; 48 } 49 50 }
bean.xml(Spring容器对象配置文件)
<?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://localhost:3306/test01"></property> <property name="user" value="root"></property> <property name="password" value="123456"></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"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- dao 实例 --> <bean id="studentDao" class="test.jdbc.StudentDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans>
测试类
1 public class App { 2 // 容器对象 3 ApplicationContext ac = new ClassPathXmlApplicationContext("test/jdbc/bean.xml"); 4 5 @Test 6 public void testApp() throws Exception { 7 StudentDao std = (StudentDao) ac.getBean("studentDao"); 8 std.getOneById("13"); 9 System.out.println(std.getAll()); ; 10 } 11 }
时间: 2024-12-28 16:01:56