<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
">
<context:annotation-config/>
<context:component-scan base-package="org.zero01"/>
<bean
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql:///school"
p:user="root"
p:password="Zero-One1."
p:loginTimeout="2000"
p:maxPoolSize="10"
p:minPoolSize="1"
/>
</beans>
创建数据库表格字段封装类:
package org.zero01.pojo;
import org.springframework.stereotype.Component;
@(JavaWeb)Component("stu")
public class Student {
private int sid;
private String name;
private int age;
private String sex;
private String address;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
编写dao类:
package org.zero01.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.zero01.pojo.Student;
import javax.sql.DataSource;
@Component("stuDAO")
public class StudentDAO {
@Autowired
private DataSource dataSource;
public int springInsert(Student student) {
// 实例化jdbc模板对象,并传入数据源
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "INSERT INTO student(sname,age,sex,address) VALUES (?,?,?,?)";
// 调用update方法执行insert
int row = jdbcTemplate.update(sql, student.getName(), student.getAge(), student.getSex(), student.getAddress());
return row;
}
}
可以看到,使用了JdbcTemplate之后,只需要写sql语句再调用相应的执行方法即可,不需要去关心数据库连接对象的获得、关闭以及减少了大量设置值的代码。
而且以上只是其中一种写法,我们还可以直接继承JdbcTemplate,这样就可以直接调用父类的方法了:
package org.zero01.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.zero01.pojo.Student;
import javax.sql.DataSource;
@Component("stuDAO")
public class StudentDAO extends JdbcTemplate {
@Autowired
// 重写父类的setDataSource来设置数据源对象
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
public int springInsert(Student student) {
String sql = "INSERT INTO student(sname,age,sex,address) VALUES (?,?,?,?)";
// 直接调用父类的方法即可
int row = update(sql, student.getName(), student.getAge(), student.getSex(), student.getAddress());
return row;
}
}
以下通过JdbcTemplate来编写一个简单的增删查改小例题:
接口:
package org.zero01.dao;
import org.zero01.pojo.Student;
import java.util.List;
public interface DAO {
public int insert(Student student);
public int delete(int sid);
public List<Student> selectAll();
public List<Student> selectByLimit(int start, int end);
public Student selectById(int sid);
public long countAll();
public int update(Student student);
}
因为JdbcTemplate不提供表格字段自动映射到对象的属性上的功能,所以我们需要自己实现它的一个接口来进行手动配置映射:
package org.zero01.dao;
import org.springframework.jdbc.core.RowMapper;
import org.zero01.pojo.Student;
import java.sql.ResultSet;
import java.sql.SQLException;
// Student对象的属性映射类
public class StudentMapper implements RowMapper<Student> {