2018.12.25 Spring中JDBCTemplate模版API学习
1 Spring整合JDBC模版
1.1 spring中土拱了一个可以操作数据库的对象。对象封装了jdbc技术
JDBCTemplateJDBC模板对象
1.2 与DBUtils中的QueryRunner非常相似
1.3 准备工作
1.导包 4+2 基础包+日志包。 junit5+spring-test、spring-aop、c3p0连接池、JDBC驱动、spring-jdbc、spring-tx事务


JDBC演示


1.4 JDBC模版实现增删改查操作
准备接口UserDao

编写实现类 UserDaoImpl

增

删

改

查




主配置文件applicationContext.xml

测试

1.5 代码优化




1.6 spring整合JDBC --读取properties配置
准备properties文件



主要代码提供参考(下面是优化之前的代码)
UserDao接口
package com.legend.a_jdbctemplate;
import java.util.List;
import com.legend.bean.User;
public interface UserDao {
//增
void save(User u);
//删
void delete(Integer id);
//改
void update(User u);
//查
User getById(Integer id);//通过ID
int getTotalCount();//查找总记录数
List<User> getAll();//获取所有的记录
}
UserDaoImpl实现类
package com.legend.a_jdbctemplate;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.legend.bean.User;
/**
* 使用JDBC模版实现增删改查
* @author qichunlin
*
*/
public class UserDaoImpl implements UserDao {
//声明模版
private JdbcTemplate jt;
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
//增
@Override
public void save(User u) {
String sql = "insert into t_user values(null,?)";
jt.update(sql,u.getName());
System.out.println("执行完毕");
}
//删
@Override
public void delete(Integer id) {
// TODO Auto-generated method stub
String sql = "delete from t_user where id=?";
jt.update(sql,id);
System.out.println("执行完毕");
}
//改
@Override
public void update(User u) {
// TODO Auto-generated method stub
String sql = "update t_user set name=? where id=?";
jt.update(sql,u.getName(),u.getId());
System.out.println("执行完毕");
}
//通过id查找用户
@Override
public User getById(Integer id) {
// TODO Auto-generated method stub
String sql = "select * from t_user where id=?";
return jt.queryForObject(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
// 不需要判断是否有值
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
System.out.println("执行完毕");
return user;
}},id);
}
//获取所有的行数
@Override
public int getTotalCount() {
// TODO Auto-generated method stub
String sql = "select count(*) from t_user ";
//当返回的是值类型, 后面的Class对象就是
Integer count = jt.queryForObject(sql, Integer.class);
System.out.println("执行完毕");
return count;
}
//获取所有的对象记录
@Override
public List<User> getAll() {
String sql = "select * from t_user ";
List<User> list = jt.query(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
// 不需要判断是否有值
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}});
return list;
}
}
applicationContext.xml主配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!-- 1.将连接池放入spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///crm"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 2.将JDBCTemplate放入spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3.将UserDao放入spring容器 -->
<bean name="userDao" class="com.legend.a_jdbctemplate.UserDaoImpl">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
</beans>
DemoTest测试类
package com.legend.a_jdbctemplate;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.legend.bean.User;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* SpringJDBC演示
* @author qichunlin
*
*/
//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class DemoTest2 {
//操作userDao对象
@Resource(name="userDao")
private UserDao ud;
@Test
public void fun2() throws Exception {
User u = new User();
u.setName("ATencent");
ud.save(u);
}
@Test
public void fun3() throws Exception {
User u = new User();
u.setId(10);
u.setName("hhhh");
ud.update(u);
}
@Test
public void fun4() throws Exception {
List<User> all = ud.getAll();
System.out.println(all.size());
}
@Test
public void fun5() throws Exception {
ud.delete(1);
}
@Test
public void fun6() throws Exception {
System.out.println(ud.getTotalCount());
}
//最原始的方法
@Test
public void fun1() throws Exception {
//准备连接池
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///crm");
dataSource.setUser("root");
dataSource.setPassword("123456");
//1.创建JDBC模版对象
JdbcTemplate jt = new JdbcTemplate();
jt.setDataSource(dataSource);
//2.书写sql,并执行
String sql = "insert into t_user values(3,'legend')";
jt.update(sql);
System.out.println("执行完毕");
}
}
2018.12.25 Spring中JDBCTemplate模版API学习的更多相关文章
- 2018.12.24 Spring中的aop演示(也就是运用aop技术实现代理模式)
Aop的最大意义是:在不改变原来代码的前提下,也不对源代码做任何协议接口要求.而实现了类似插件的方式,来修改源代码,给源代码插入新的执行代码. 1.spring中的aop演示 aop:面向方面编程.不 ...
- Spring中JdbcTemplate的基础用法
Spring中JdbcTemplate的基础用法 1.在DAO中使用JdbcTemplate 一般都是在DAO类中使用JdbcTimplate,在XML配置文件中配置好后,可以在DAO中注入即可. 在 ...
- Spring 中jdbcTemplate 实现执行多条sql语句
说一下Spring框架中使用jdbcTemplate实现多条sql语句的执行: 很多情况下我们需要处理一件事情的时候需要对多个表执行多个sql语句,比如淘宝下单时,我们确认付款时要对自己银行账户的表里 ...
- 【sping揭秘】19、关于spring中jdbctemplate中的DataSource怎么来呢
我们这是可以正好借助之前学的factorybean类,自己吧jdbctemplate加载到spring容器中,我们可以封装多个这种对象,那么可以实现针对不同的数据库的jdbctemplate 首先我们 ...
- 2018.12.22 Spring学习02
Spring学习02 1.使用注解配置Spring 1.1 为主配置文件引入新的命名空间(约束) 添加约束文件xxx-xxx-context.xml 添加到主配置文件中 选择刚才的context.xm ...
- 2018.12.20 Spring环境如何搭建
Spring学习 1.导入spring约束 为后续创建xml文件做铺垫 2.开始搭建Spring环境 1.创建Web项目,引入spring的开发包(根据下面的图来引入) 2.引入jar包 coreCo ...
- 【Spring Framework】12种spring中定义bean的方法
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
- 2018.11.29 Struts2中拦截器的学习&项目的实际运用
struts2官方架构 拦截器的创建 第一种方式 第二种方式 看源码AbstractInterceptor 底层已经帮我们写过这些方法了 第三种方式(推荐) 拦截器API学习 放行 前后处理 不放行, ...
- Spring中的JDBC API
1 JdbcTemplate的诞生 JDBC作为Java平台访问关系数据库的标准API,其成功是有目共睹的.为了避免在JDBC API在使用中的种种尴尬局面(几乎程式一样的代码,繁琐的异常处理),Sp ...
随机推荐
- vue生命周期及使用 && 单文件组件下的生命周期
生命周期钩子 这篇文章主要记录与生命周期相关的问题. 之前,我们讲到过生命周期,如下所示: 根据图示我们很容易理解vue的生命周期: js执行到new Vue() 后,即进入vue的beforeCre ...
- BFC --- Block Formatting Context --- 块级格式化上下文
虽然知道块级格式化上下文是什么东西,但要我把这个东西给说清楚,还真的不是一件容易的事儿,所以这篇文章我就要说说清楚到底什么使传说中的BFC,即块级格式化上下文. 一.BFC的通俗理解 通俗的理解 -- ...
- IDEA 14.0 (默认模式) 快捷键
IDEA 14.0 (默认模式) 快捷键 1.Alt+Shift+R:重命名变量名.类名.方法名(使用已经使用过的) 2.Ctrl+O :重写方法 3.Alt+Shift+P :实现接口 4.Alt+ ...
- ubuntu安装卸载软件
sudo apt-get remove nagios3 #卸载软件 sudo apt-get autoremove #卸载依附软件包 rpm格式 安装:rpm -ivh *** 查看:rpm -q * ...
- PHP SECURITY CALENDAR 2017 学习总结-更新中
这篇文章主要以审计代码为主来分析每道题目中所存在的漏洞点,记录一下自己的学习: 1.Day 1 - Wish List class Challenge { const UPLOAD_DIRECTORY ...
- MySQL之基本语句
SQL是Structure Query language(结构化查询语言)的缩写,它是使用关系模型的数据库应用语言.在众多开源数据库中,MySQL正是其中最杰出的代表,MySQL是由三个瑞典人于20世 ...
- 【学习笔记】使用SQLyog连接MySQL数据库
一.使用SQLyog创建数据库用来管理学生信息 #创建数据库student DROP DATABASE IF EXISTS Myschool; CREATE DATABASE Myschool; #在 ...
- hibernate的各种保存方式的区别 (save,persist,update,saveOrUpdte,merge,flush,lock)
hibernate的保存hibernate对于对象的保存提供了太多的方法,他们之间有很多不同,这里细说一下,以便区别:一.预备知识:在所有之前,说明一下,对于hibernate,它的对象有三种状态,t ...
- Dozer 实现对象间属性复制
使用场景:两个领域之间对象转换. 比如:在系统分层解耦过程中, 对外facade接口,一般使用VO对象,而内core业务逻辑层或者数据层通常使用Entity实体. VO对象 package com.m ...
- Java NIO(三) Buffer
Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到通道中的. 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被包装成NIO ...