目录
一、入门使用
1.Dao层的解决的方法
Spring是EE开发的一站式的框架,有EE开发的每层的解决方案。
Spring对持久层也提供了解决方案:ORM模块和JDBC的模板。
2.JDBC模板的简单使用
(1)创建项目引入jar包
(jdbc依赖tx(事务管理的包))
(2)创建数据库和表
create database spring4_day03;
use spring4_day03;
create table account(
id int primary key auto_increment,
name varchar(20),
money double
);
(3)使用JDBC的模板保存数据
注:jdbc不可以打印sql语句
缺点:
每次使用都要创建连接池 和 JDBC的模板
二、将连接池和模板交给spring管理
由于上面的方法有很多的局限性,操作不是很方便
1.引入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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
2.配置内置连接池和JDBC模板
3.使用JDBC的模板
4.开源连接池的引入
(1)问题描述
之前引用的都是spring默认的连接池
那么如何使用开源的连接池如何引用???????
(2)DBCP的使用
1)引入jar包
2)配置DBCP的连接池 交给spring管理
3)使用
(3)c3p0的使用
1)引入c3p0的jar包
2)配置c3p0的连接池
3)使用
5.抽取配置到属性文件
在开发过程中,配置文件会很多,DAO service aop的等等
会有很多的配置文件,那么怎么将配置文件配置到属性文件
(1)创建一个属性文件
都是k v形式的属性
要将连接池的配置改成属性文件,引入到配置文件中
(2)在spring中的配置文件中引入属性文件
方法1:
方法2:
(3)通过${}引用属性配置中的值
6.问题总结
(1)为什么在测试类中直接可以通过@Resource方法进行jdbcTemplate注入
因为引入了test包了,就可以(只能)在test中进行@Resource的方法进行注入
其他类要在配置中开启 application-context
类中开启
三、JDBCTemplate实现CRUD
一般实现增删改的操作的方法是一样的
1.修改
2.删除
3.查
1.根据某个属性查询
2.查询插入的数据条数
3.查询某个对象(结果封装到一个对象中)
(1)创建实体类
(2)执行查询的方法
(这两个方法都可以)