Spring 与JDBC模板 是对 IoC的应用
Spring 的事务管理与Mybatis是对AOP的应用
4.1 Spring与JDBC模板
<?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" 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">
<!-- 注册数据源:Spring内置连接池 -->
<!--
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3366/test"/>
<property name="username" value="root"/>
<property name="password" value="111"/>
</bean>
-->
<!-- 注册数据源:DBCP -->
<!--
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3366/test"/>
<property name="username" value="root"/>
<property name="password" value="111"/>
</bean>
-->
<!-- 注册数据源:C3P0 -->
<!--
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3366/test"/>
<property name="user" value="root"/>
<property name="password" value="111"/>
</bean>
-->
<!-- 注册数据源:C3P0 -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 注册属性文件:方式一 -->
<!--
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
-->
<!-- 注册属性文件:方式一 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 注册JdbcTemplate -->
<!--
<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="myDataSource"/>
</bean>
<bean id="studentDao" class="com.bjpowernode.dao.StudentDaoImpl">
<property name="jdbcTemplate" ref="myJdbcTemplate"/>
</bean>
-->
<!-- 注册Dao -->
<bean id="studentDao" class="com.bjpowernode.dao.StudentDaoImpl">
<property name="dataSource" ref="myDataSource"/>
</bean>
<!-- 注册Service -->
<bean id="studentService" class="com.bjpowernode.service.StudentServiceImpl">
<property name="dao" ref="studentDao"/>
</bean>
</beans>
bean:
dao层:对数据库的增删改查
IStudentDao.java
package com.bjpowernode.dao;
import java.util.List;
import com.bjpowernode.beans.Student;
public interface IStudentDao {
void insertStudent(Student student);
void deleteById(int id);
void updateStudent(Student student);
List<String> selectAllStudentsNames();
String selectStudentNameById(int id);
List<Student> selectAllStudents();
Student selectStudentbyId(int id);
}
StudentDaoImpl.java
package com.bjpowernode.dao;
import java.util.List;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.SqlCall;
import com.bjpowernode.beans.Student;
public class StudentDaoImpl extends JdbcDaoSupport implements IStudentDao {
@Override
public void insertStudent(Student student) {
String sql = "insert into Student(name,age) values(?,?)";
this.getJdbcTemplate().update(sql, student.getName(),student.getAge());
}
@Override
public void deleteById(int id) {
String sql = "delete from student where id = ?";
this.getJdbcTemplate().update(sql, id);
}
@Override
public void updateStudent(Student student) {
String sql = "update student set name=?,age=? where id=?";
this.getJdbcTemplate().update(sql, student.getName(),student.getAge(),student.getId());
}
@Override
public List<String> selectAllStudentsNames() {
String sql = "select name from student";
return this.getJdbcTemplate().queryForList(sql,String.class);
}
@Override
public String selectStudentNameById(int id) {
String sql = "select name from student where id=?";
return this.getJdbcTemplate().queryForObject(sql, String.class,id);
}
@Override
public List<Student> selectAllStudents() {
String sql = "select id,name,age from student";
return this.getJdbcTemplate().query(sql,new StudentRowMapper() );
}
@Override
public Student selectStudentbyId(int id) {
String sql = "select id,name,age from student where id=?";
return this.getJdbcTemplate().queryForObject(sql,new StudentRowMapper(),id);
}
}
不能写成一下形式:
package com.bjpowernode.dao;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.SqlCall;
import com.bjpowernode.beans.Student;
//代码不能写为下面的形式
public class StudentDaoImpl2 extends JdbcDaoSupport implements IStudentDao {
private JdbcTemplate jt;
public StudentDaoImpl2() {
jt = this.getJdbcTemplate();
}
@Override
public void insertStudent(Student student) {
String sql = "insert into Student(name,age) values(?,?)";
this.jt.update(sql, student.getName(),student.getAge());
}
@Override
public void deleteById(int id) {
String sql = "delete from student where id = ?";
this.jt.update(sql, id);
}
@Override
public void updateStudent(Student student) {
String sql = "update student set name=?,age=? where id=?";
this.jt.update(sql, student.getName(),student.getAge(),student.getId());
}
@Override
public List<String> selectAllStudentsNames() {
String sql = "select name from student";
return this.jt.queryForList(sql,String.class);
}
@Override
public String selectStudentNameById(int id) {
String sql = "select name from student where id=?";
return this.jt.queryForObject(sql, String.class,id);
}
@Override
public List<Student> selectAllStudents() {
String sql = "select id,name,age from student";
return this.jt.query(sql,new StudentRowMapper() );
}
@Override
public Student selectStudentbyId(int id) {
String sql = "select id,name,age from student where id=?";
return this.jt.queryForObject(sql,new StudentRowMapper(),id);
}
}
StudentRowMapper.java
package com.bjpowernode.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import com.bjpowernode.beans.Student;
public class StudentRowMapper implements RowMapper<Student> {
//rs:当查询出总的结果集后,框架会自动遍历这个结果集,每一次遍历的一行数据都会被存放到
//这个方法的rs参数中,也就是说这里的rs代表的是一行数据,并非所有查询结果。换个角度来说
//只要能执行到这个方法,就说明rs不会为空。
@Override
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
return student;
}
}
Service层:
IStudentService.java
package com.bjpowernode.service;
import java.util.List;
import com.bjpowernode.beans.Student;;
public interface IStudentService {
void addStudent(Student student);
void removeById(int id);
void modifyStudent(Student student);
List<String> findAllStudentsNames();
String findStudentNameById(int id);
List<Student> findAllStudents();
Student findStudentbyId(int id);
}
StudentServiceImpl.java
package com.bjpowernode.service;
import java.util.List;
import com.bjpowernode.beans.Student;
import com.bjpowernode.dao.IStudentDao;
public class StudentServiceImpl implements IStudentService{
private IStudentDao dao;
public void setDao(IStudentDao dao) {
this.dao = dao;
}
@Override
public void addStudent(Student student) {
dao.insertStudent(student);
}
@Override
public void removeById(int id) {
dao.deleteById(id);
}
@Override
public void modifyStudent(Student student) {
dao.updateStudent(student);
}
@Override
public List<String> findAllStudentsNames() {
List<String>names = dao.selectAllStudentsNames();
return names;
}
@Override
public String findStudentNameById(int id) {
String name = dao.selectStudentNameById(id);
return name;
}
@Override
public List<Student> findAllStudents() {
List<Student> students = dao.selectAllStudents();
return students;
}
@Override
public Student findStudentbyId(int id) {
Student student = dao.selectStudentbyId(id);
return student;
}
}
MyTest.java
package com.bjpowernode.test;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjpowernode.beans.Student;
import com.bjpowernode.service.IStudentService;
public class MyTest {
private IStudentService service;
@Before
public void Before(){
String resource = "applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(resource);
service = (IStudentService) applicationContext.getBean("StudentService");
}
@Test
public void test01(){
Student student = new Student("张三",23);
service.addStudent(student);
}
@Test
public void test02(){
service.removeById(2);
}
@Test
public void test03(){
Student student = new Student("张三",23);
student.setId(3);
service.modifyStudent(student);
}
@Test
public void test04(){
List<String> names = service.findAllStudentsNames();
System.out.println(names);
}
@Test
public void test05(){
String name = service.findStudentNameById(1);
System.out.println(name);
}
@Test
public void test06(){
List<Student>students = service.findAllStudents();
for(Student student:students)
System.out.println(student);
}
@Test
public void test07(){
Student student = service.findStudentbyId(1);
System.out.println(student);
}
}