Spring框架第六篇之Spring与DAO

时间:2021-11-11 09:57:14

一、Spring与JDBC模板

1、搭建环境

首先导入需要的jar包:

Spring框架第六篇之Spring与DAO

以上jar中多导入了DBCP和C3P0的jar包,因为这里需要演示怎么配置多种数据源,所以导入了这两个包,在实际开发中无需导入这两个包。

2、数据源的配置

数据源的配置分为3中情况:

1、Spring内置的连接池DriverManagerDataSource;

2、DBCP数据源 BasicDataSource;

3、C3P0数据源 ComboPooledDataSource;

具体在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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 注册数据源:1、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:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>--> <!-- 注册数据源:2、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:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>--> <!-- 注册数据源:3、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:3306/test"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean> <!-- 注册JdbcTemplate -->
<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="myDataSource"/>
</bean> <!-- 注册Dao -->
<bean id="studentDao" class="com.ietree.spring.dao.basic.dao.StudentDaoImpl">
<property name="jdbcTemplate" ref="myJdbcTemplate"/>
</bean> <!-- 注册Service -->
<bean id="studentService" class="com.ietree.spring.dao.basic.service.StudentServiceImpl">
<property name="dao" ref="studentDao"/>
</bean> </beans>

3、从属性文件读取数据库连接信息

创建db.properties数据库配置文件:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.user=root
jdbc.password=root

配置Spring配置文件:

<!-- 注册数据源:3、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:db.properties"/>
</bean> <!--注册属性文件:方式二-->
<context:property-placeholder location="classpath:db.properties"/>

4、配置JDBC模板

<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="myDataSource"/>
</bean>
<!-- 注册Dao -->
<bean id="studentDao" class="com.ietree.spring.dao.basic.dao.StudentDaoImpl">
<property name="jdbcTemplate" ref="myJdbcTemplate"/>
</bean>

5、DAO实现类继承JdbcDaoSupport类,对数据库的增删改查操作

package com.ietree.spring.dao.basic.dao;

import com.ietree.spring.dao.basic.bean.Student;
import com.ietree.spring.dao.basic.bean.StudentRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport; import java.util.List; /**
* Created by Root on 2017/7/9.
*/
public class StudentDaoImpl extends JdbcDaoSupport implements IStudentDao { @Override
public void insertStudent(Student student) {
String sql = "INSERT INTO tbl_student(name,age) VALUES(?,?)";
this.getJdbcTemplate().update(sql,student.getName(),student.getAge());
} @Override
public void deleteStudent(int id) {
String sql = "DELETE FROM tbl_student WHERE id = ?";
this.getJdbcTemplate().update(sql,id);
} @Override
public void updateStudent(Student student) {
String sql = "UPDATE tbl_student SET name=?,age=? WHERE id = ?;";
this.getJdbcTemplate().update(sql,student.getName(),student.getAge(),student.getId());
} @Override
public List<String> selectAllStudentNames() {
String sql = "SELECT name FROM tbl_student";
return this.getJdbcTemplate().queryForList(sql,String.class);
} @Override
public String selectStudentNameById(int id) {
String sql = "SELECT name FROM tbl_student WHERE id = ?";
return this.getJdbcTemplate().queryForObject(sql,String.class,id);
} @Override
public List<Student> selectAllStudent() {
String sql = "SELECT id,name,age FROM tbl_student";
return this.getJdbcTemplate().query(sql, new StudentRowMapper());
} @Override
public Student selectStudentById(int id) {
String sql = "SELECT id,name,age FROM tbl_student WHERE id = ?";
return this.getJdbcTemplate().queryForObject(sql, new StudentRowMapper(),id);
}
}

StudentRowMapper类:

package com.ietree.spring.dao.basic.bean;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException; /**
* Created by Root on 2017/7/11.
*/
public class StudentRowMapper implements RowMapper<Student> { /**
* 这里的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;
}
}

以上案例完整的配置文件如下:

<?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"> <!-- 注册数据源:1、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:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>--> <!-- 注册数据源:2、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:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>--> <!-- 注册数据源:3、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:3306/test"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>--> <!-- 注册数据源:3、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:db.properties"/>
</bean>--> <!--注册属性文件:方式二-->
<context:property-placeholder location="classpath:db.properties"/> <!-- 根据JdbcDaoSupport类的源码可以省略JdbcTemplate的注册,将DataSource作为Dao的属性 -->
<!-- 注册JdbcTemplate -->
<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="myDataSource"/>
</bean>
<!-- 注册Dao -->
<bean id="studentDao" class="com.ietree.spring.dao.basic.dao.StudentDaoImpl">
<property name="jdbcTemplate" ref="myJdbcTemplate"/>
</bean> <!-- 注册Dao -->
<bean id="studentDao" class="com.ietree.spring.dao.basic.dao.StudentDaoImpl">
<property name="dataSource" ref="myDataSource"/>
</bean> <!-- 注册Service -->
<bean id="studentService" class="com.ietree.spring.dao.basic.service.StudentServiceImpl">
<property name="dao" ref="studentDao"/>
</bean> </beans>

二、Spring的事务管理

1、Spring事务管理API

2、使用Spring的事务代理工厂管理事务

3、使用Spring的事务注解管理事务

4、使用AspectJ的AOP配置管理事务