Spring与JDBC模板(jdbcTemplate)

  为了避免直接使用JDBC而带来的复杂冗长的代码

Spring提供的一个强有力的模板类 -- jdbcTemplate
简化JDBC操作

并且数据源DataSource对象与模板JdbcTemplate对象

  都可以通过Bean的形式定义在配置文件中 充分发挥了依赖注入的威力

1.jar包
Spring jdbc jar包
spring tx (事务jar包)

2.导入命名空间
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"

3.搭建分层

//beans
public class Book{
private int bid;
private String bname;
private int bprice;
} //dao
public interface IBookDao{
int addBook(Book book);
}
//impl
public class BookDaoImpl implements IBookDao{
public int addBook(Book book){ return 0;
}
} //service
public interface IBookBiz{
int addBook(Book book);
}
//impl
public class BookBizImpl implements IBookBiz{
private IBookDao dao;
public int addBook(Book book){ return dao.addBook();
}
}
//xml配置
//dao
<bean id="bookdao" class="xx.xx.BookDaoImpl"></bean> //service
<bean id="bookbiz" class="xx.xx.BookBizImpl">
<property name="dao" ref="bookdao"></property>
</bean>

4.注册数据源

注册源分为三类 如下
Spring   内置的   DriverManagerDataSource
DBCP         BasicDataSource
C3P0          ComboPooledDataSource

//DriverManagerDataSource
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///book"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean> //BasicDataSource
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///book"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean> //ComboPooledDataSource
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClasss" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///book"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> 4.jdbc的属性文件
//jdbc.properties
jdbc.driver=xxx
jdbc.url=xxx
jdbc.username=xxx
jdbc.password=xxx //xml
1.PropertyPlaceholderConfigurer 2.<context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClasss" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> 1.PropertyPlaceholderConfigurer
<bean class="PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>

以上是XML中配置三种数据源 的方法

以及使用properties文件配置的方法

5.继承JdbcTemplate 实现新增一本图书

public class BookDaoImpl extends JdbcDaoSupport implements IBookDao{
public int addBook(Book book){
String sql="xxx";
return this.getJdbcTeplate().update(sql,book.getBname,book.getBprice); }
}

6.书写测试类

@Test
public void test1() {
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
IBookBiz biz = (IBookBiz) ctx.getBean("bookBiz");
Book book = new Book();
book.setBname("第二本书");
book.setBprice(10); biz.add(book);
}

运行之后 成功在数据表中 插入了一条记录

使用注解的方式实现使用JdbcTemplate连接数据库

1.DAO IMPL

@Repository
public class BookDaoImpl implements IBookDao {
@Resource
private JdbcTemplate template; public int add(Book book) {
String sql = "insert into book values(null,?,?)";
int update = getTemplate().update(sql, book.getBname(), book.getBprice());
return update;
} public JdbcTemplate getTemplate() {
return template;
} public void setTemplate(JdbcTemplate template) {
this.template = template;
}
}

2.BIZ IMPL

@Service("bookBiz")
public class BookBizImpl implements IBookBiz {
@Autowired
private IBookDao dao; public int add(Book book) {
return dao.add(book);
} public IBookDao getDao() {
return dao;
} public void setDao(IBookDao dao) {
this.dao = dao;
}
}

3.XML配置

<context:component-scan base-package="sword"></context:component-scan>

    <!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///book"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean> <!--jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

【学习笔记】Spring JdbcTemplate (3-3-3)的更多相关文章

  1. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

  2. Spring学习笔记--Spring IOC

    沿着我们上一篇的学习笔记,我们继续通过代码学习IOC这一设计思想. 6.Hello类 第一步:首先创建一个类Hello package cn.sxt.bean; public class Hello ...

  3. Spring学习笔记:jdbcTemplate和数据源配置

    一.使用Spring框架jdbcTemplate实现数据库的增删改查 1.数据库 /* SQLyog Ultimate v8.32 MySQL - 5.7.19-log : Database - in ...

  4. Spring学习笔记--Spring配置文件和依赖注入

    Spring配置文件 1.alias:设置别名,为bean设置别名,并且可以设置多个别名; <!-- 设置别名 --> <alias name="user" al ...

  5. Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...

  6. Spring4.0学习笔记(12) —— JDBCTemplate 操作数据库

    整体配置 1.配置xml文件 <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi ...

  7. [spring入门学习笔记][spring的IoC原理]

    什么叫IoC 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency ...

  8. Spring Boot学习笔记---Spring Boot 基础及使用idea搭建项目

    最近一段时间一直在学习Spring Boot,刚进的一家公司也正好有用到这个技术.虽然一直在学习,但是还没有好好的总结,今天周末先简单总结一下基础知识,等有时间再慢慢学习总结吧. Spring Boo ...

  9. Spring学习笔记—Spring之旅

    1.Spring简介     Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...

  10. Spring学习笔记——Spring中的BeanFactory与FactoryBean

    BeanFactory BeanFactory是Spring的org.springframework.beans.factory下的一个接口,是Spring IOC所遵守的基本编程规范.他的实现类有D ...

随机推荐

  1. 关于RegExp对象实例的lastIndex属性的一些整理

    今天在做正则循环匹配,碰到一个怪问题,第一次可以匹配上,但循环第一次之后,就无法匹配上了.猛然想起,RegExp中lastIndex属性,于是上网搜索了一下,将一些资料整理归纳,以备今后自己查阅(记性 ...

  2. linux_定时任务

    什么是定时任务? linux系统自身定期执行的任务和工作: 轮训系统日志.备份系统数据.清理缓存等 var/log/messages # 系统日志文件, ll /etc/|grep cron # 查询 ...

  3. Django中url使用命名空间的错误

    出的错误: 1. Reverse for 'llist' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) ...

  4. 初始化本地项目到远程仓库【git】

    大部分情况,都是从远程仓库clone项目,步骤很简单. 今天要把本地项目初始化到远程仓库的步骤记录下来,其实也很简单,几步就好: #初始化本地仓库 git init #将本地内容添加至git索引中 g ...

  5. python初识-day2

    1.模块初识 python中的库包括标准库和第三方库,需要使用时可直接导入,导入格式为"import  库模块",例如: import sys #sys为python的标准库 pr ...

  6. 怎么查看mysql的安装目录

    如果忘记了MySQL的安装目录,怎么快速找到呢?方法或许很多,作者觉得这种最方便了 环境:windows+mysql+navicat 方法:进入mysql命令行输入:show variables li ...

  7. junit4X系列源码--Junit4 Runner以及test case执行顺序和源代码理解

    原文出处:http://www.cnblogs.com/caoyuanzhanlang/p/3534846.html.感谢作者的无私分享. 前一篇文章我们总体介绍了Junit4的用法以及一些简单的测试 ...

  8. 用Markdown格式写一份前端简历

    1. 基本信息 姓名:xxx 手机号码:1380000xxxx 学校:南昌大学 学历:大学本科/硕士/博士 工作经验:3年以上Web前端 电子邮件:xxx@outlook.com 2. 求职意向 工作 ...

  9. 01-Go命令与基础

    什么是Go? Go是一门并发支持.垃圾回收的编译型系统编程语言,旨在创造一门具有在静态编译语言的高性能和动态的高效开之间拥有良好平衡点的一门编程语言. Go的主要特点有哪些? 类型安全和内存安全 以非 ...

  10. 解决C#编译中"csc不是内部或外部命令"的问题

    安装完 VisualStudio 编译环境后,是不能用命令行直接编译写好的csc文件的,如果不配置环境变量,在命令提示符(cmd)中编译扩展名为cs的文件,会出现错误提示"csc不是内部或外 ...