大家可以关注作者的账号,关注从零开始学Spring笔记文集。也可以根据目录前往作者的博客园博客进行学习。本片文件将基于黑马程序员就业班视频进行学习以及资料的分享,并记录笔记和自己的看法。欢迎大家一起学习和讨论。
【从零开始学Spring笔记】Spring学习路线
Spring是EE开发的一站式的框架,有EE开发的每层的解决方案。Spring 对持久层也提供了解决方案: ORM 模块和JDBC的模板。
Spring提供了很多持久层技术的模板类简化编程:
JDBC模板使用的入门
第一步:创建web项目,引入jar包
除了引入Spring的六个基本jar包和text包和aop包外,还需要引入mysql的启动包和spring的jdbc和tx包
可以在以前下载解压后的libs文件夹中寻找,同时也上传到百度云了,下载即可。下载链接在本系列文集的第一篇【从零开始学Spring笔记】Spring4学习路线中寻找。
第二步:创建数据库和表
create database spring4_day03;
use spring4_day03;
create table account (
id int primary key auto_increment,
name varchar (20),
money double
);
第三步:使用JDBC模板保存数据
示例
package com.tyust.jdbc.demo1;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class JdbcDemo1 {
@Test
//jdbc模板的使用类似于Dbutils.
public void demo1() {
//创建连接池
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql:///spring4_day03");
ds.setUsername("root");
ds.setPassword("root");
//创建jdbc模板
JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
jdbcTemplate.update("insert into account values (null,?,?)","张飞",10000d);
}
}
将连接池和模板都交给Spring管理
示例
配置文件
<bean id = "dmds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="DriverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring4_day03"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dmds"></property>
</bean>
测试类
package com.tyust.jdbc.demo1;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcDemo2 {
@Resource(name = "jt")
private JdbcTemplate jt;
@Test
public void demo1() {
jt.update("insert into account values (null,?,?)","刘备",210000d);
}
}
输出结果
使用开源的数据库连接池
1.DBCP的使用
第一步:引入jar包
可以在以前下载解压后的libs文件夹中寻找,同时也上传到百度云了,下载即可。下载链接在本系列文集的第一篇【从零开始学Spring笔记】Spring4学习路线中寻找。
第二步:配置DBCP连接池
<!-- 配置dbcp连接池 -->
<bean id = "ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="DriverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring4_day03"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
2.C3P0的使用
第一步:引入jar包
可以在以前下载解压后的libs文件夹中寻找,同时也上传到百度云了,下载即可。下载链接在本系列文集的第一篇【从零开始学Spring笔记】Spring4学习路线中寻找。
第二步:配置C3P0连接池
<!-- 配置C3p0连接池 -->
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring4_day03"/>
<property name="user" value="root"/>
<property name="password" value="root"></property>
</bean>
引入外部属性文件
第一步:new->file
定义一个属性文件
第二步:在Spring的配置文件中,引入属性文件
<!-- 引入属性文件 -->
<!-- 第一种方式通过一个bean标签引入(很少使用) -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> -->
<!-- 第二种方式通过context标签引入 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3p0连接池 -->
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"></property>
</bean>
模板的CRUD的操作
@Test
// 修改操作
public void demo2() {
jt.update("update account set name = ? ,money= ? where id = ?", "何巨涛", 2000d, 7);
}
@Test
// 删除操作
public void demo3() {
jt.update("delete from account where id = ?", 6);
}
@Test
// 查询操作:
public void demo4() {
String name = jt.queryForObject("select name from account where id = ?", String.class, 5);
System.out.println(name);
}
@Test
// 统计查询
public void demo5() {
Long count = jt.queryForObject("select count(*) from account", Long.class);
System.out.println(count);
}
@Test
// 封装到一个对象中
public void demo6() {
Account account = jt.queryForObject("select * from account where id = ?", new MyRowMapper(), 5);
System.out.println(account);
}
@Test
// 查询多条记录
public void demo7() {
List<Account> list = jt.query("select * from account", new MyRowMapper());
for (Account account : list) {
System.out.println(account);
}
}
class MyRowMapper implements RowMapper<Account> {
@Override
public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
Account account = new Account();
account.setId(rs.getInt("id"));
account.setMoney(rs.getDouble("money"));
account.setName(rs.getString("name"));
return account;
}
}