Spring框架之使用JdbcTemplate开发Dao层程序
简介: JdbcTemplate开发dao层程序
由Spring框架给我们提供,Spring提供的很多操作数据源(关系型数据库,二维表格模型,有明确的行和列(mysql/orcal等) 非关系型数据库(redis.mongodb)NoSQL)消息列(activeMq,jms)的小工具
JdbcTemplate操作关系数据库
RedisTemplate操作redis
JmsTtemplate操作消息队列
JdbcTemplate类
使用方法和QueryRunner基本一致.
构造方法传递数据源DataSource对象
API方法:
update(String sql,Object...obj) 用来执行增删改操作
queryForObject(String sql,RowMapper mapper,Object...obj ) 查询返回单个对象.
queryForObject(String sql, Class cla,Object...obj)查询返回单个对象基本类型及其包装类和字符串
query(String sql,RowMapper mapper,Object...obj)查询返回集合对象
其中RowMapper是一个接口实现类
BeanPropertyRowMapper,查询的结果集封装,使用单个对象或者集合.
JdbcTemplate类实现account表的curd
使用的是Maven中的quickstart骨架创建的项目
引入的依赖为:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itheima</groupId>
<artifactId>spring_04_qm04</artifactId>
<version>1.0-SNAPSHOT</version> <name>spring_04_qm04</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<!--使用Junit4测试jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--使用dbcp2连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.4.0</version>
</dependency>
<!--引入jdbcTemplate-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!--引入mysql数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!--引入spring框架-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--junit使用12版本,不然会报错.-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
文件结构为:
pojo层为:
public class Account {
private int id;
private String name;
private double money;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
从这里就可以看出来,这只是一个简单的举例,并没有使用业务层,直接从dao'调用的方法,数据库设计的也是十分简陋
dao层的代码为
import java.util.List;
public interface AccountDao {
// 增加
void saveAccount(Account acount);
}
dao层的实现类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; import java.util.List; //使用注解的方法进行创建对象
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao { // 需要使用JdbcTemplate进行操作数据库
// 使用注解的方法,进行注入
@Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Override
public void saveAccount(Account account) {
String sql = "insert into account values(?,?,?)";
jdbcTemplate.update(sql,account.getId(),account.getName(),account.getMoney());
}
}
在这里需要解释一下各个标签的用处
1.注解@Controller为我们的控制器action类的类注解相当于applicationContext.xml文件中的bean节点,而括号中的值相当于bean节点中的id属性的属性值。
同理:@Service为我们业务层的类注解,@Repository为数据层dao的类注解。
2.@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法,简化程序代码。
当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。
3.@Qualifier("jdbcTemplate")确保唯一性
applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns 代表 xml namespace, 就是 XML 标签的命名空间。 -->
<!--xmlns:context 代表使用 context 作为前缀的命名空间-->
<!--xmlns:xsi 代表是指 XML 文件遵守的 XML 规范。 -->
<!--xsi:schemaLocation 代表 XML 标签遵守的 XML 规范。-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
">
<!--开启注解扫描-->
<context:component-scan base-package="包名"></context:component-scan>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--通过set方法的方式进行注入-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置操作数据库的Jdbcemplaate操作数据库-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--通过构造参数的方式进行注入-->
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
</beans>
<context:component-scan base-package="com.itheima"></context:component-scan> 关于这个标签:
通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,
配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,
如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,
则把这些类注册为bean
注:在注解后加上例如@Component(value=”abc”)时,注册的这个类的bean的id就是adc.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
//整合Junit4测试时,用来引入多个配置文件时候,使用
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class MainTest { @Autowired
@Qualifier("accountDao")
private AccountDao accountDao; @Test
public void saveTest(){
Account account = new Account();
account.setName("孙");
account.setId(3);
account.setMoney(1000);
accountDao.saveAccount(account);
}
}
@RunWith就是一个运行器 @RunWith(JUnit4.class)就是指用JUnit4来运行 @RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境 @RunWith(Suite.class)的话就是一套测试集合 @ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件 单个文件
@ContextConfiguration(Locations="classpath:applicationContext.xml")
@ContextConfiguration(classes = SimpleConfiguration.class) 多个文件时,可用
@ContextConfiguration(locations = { "classpath:spring1.xml", "classpath:spring2.xml" })
Spring框架之使用JdbcTemplate开发Dao层程序的更多相关文章
- MyBatis开发Dao层的两种方式(原始Dao层开发)
本文将介绍使用框架mybatis开发原始Dao层来对一个对数据库进行增删改查的案例. Mapper动态代理开发Dao层请阅读我的下一篇博客:MyBatis开发Dao层的两种方式(Mapper动态代理方 ...
- MyBatis开发Dao层的两种方式(Mapper动态代理方式)
MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Ma ...
- mybatis 学习笔记(三):mapper 代理开发 dao 层
mybatis 学习笔记(三):mapper 代理开发 dao 层 优势 通过使用mapper 代理,我们可以不需要去编写具体的实现类(使用 getMapper() 方法自动生成),只需编写接口即可, ...
- 第五章 征服数据库(Spring对DB的使用)——开发持久层
本章内容: 定义Spring对数据库访问的支持 配置数据库资源 使用Spring的JDBC模板 在几乎所有的企业级应用中,都需要构建数据持久层.现在意义上的数据持久层是指把对象或者数据保存到数据库中, ...
- 四、spring集成ibatis进行项目中dao层基类封装
Apache iBatis(现已迁至Google Code下发展,更名为MyBatis)是当前IT项目中使用很广泛的一个半自动ORM框架,区别于Hibernate之类的全自动框架,iBatis对数据库 ...
- SSH框架整合中Hibernate实现Dao层常用结构
一.疑惑 一直以来,我在使用SSH框架的时候经常会发现后者有疑虑到底使用hibernate的那种方法或者如何配置hibernate来操作数据库,经过 一段时间的学习下面我来总结一下,常用的dao层配置 ...
- MyBatis使用Mapper动态代理开发Dao层
开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同原始Dao接口实现类方法. Mappe ...
- mybatis mapper接口开发dao层
本文将探讨使用 mapper接口,以及 pojo 包装类进行 dao 层基本开发 mybatis dao 层开发只写 mapper 接口 其中需要 开发的接口实现一些开发规范 1. UserMappe ...
- Spring框架学习——AOP的开发
一.AOP开发中的相关术语. ——JoinPoint(连接点):指那些可以被拦截到的点.比如增删改查方法都可以增强,这些方法就可以被称为是连接点. ——PointCut:切入点,真正被拦截的点,指对哪 ...
随机推荐
- resful规范
1.简介 什么是resful resful是一个规范,说白了就是面向资源编程,把网络中所有的东西,想象成资源 2.规范 10条规范 1)API与用户的通信协议,总是用HTTPS协议:HTTPS比htt ...
- npm install webpack -g
npm install webpack -g 全局安装webpack
- squid 快速配置
安装 yum install squid -y yum install httpd-tools -y 基本认证配置文件 #网络 http_port dns_nameservers 100.100.2. ...
- 如何写一个优秀的GitHub项目README文档?
今天给大家介绍一个Github上的README文档写作教程模版,该模版目前获得6634颗星星,2296Fork,相对而言,还是比较得到大家认可的.不花哨,不别出心裁,一个比较实用的,普适性的架子:所谓 ...
- Eclipse配置Github -分享你的代码
搭建了虚拟机供练手用,想要保存练习代码,于是想在VM Eclipse上配置Github,从此随练随保存. 步骤:1. eclipse ->help->install new softwar ...
- Linux下修改MySQL数据库字符编码为UTF-8解决中文乱码
由于MySQL编码原因会导致数据库出现乱码. 解决办法: 修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 &g ...
- pd.concat/merge/join
pandas的拼接分为两种: 级联:pd.concat, pd.append 合并:pd.merge, pd.join 一.回顾numpy.concatenate 生成1个6*3的矩阵,一个2*3的矩 ...
- Entity Framework学习初级篇3--LINQ TO Entities
LINQ 技术(即LINQ to Entities)使开发人员能够通过使用LINQ 表达式和LINQ 标准查询运算符,直接从开发环境中针对实体框架对象上下文创建灵活的强类型查询.LINQ to Ent ...
- 一个vue的循环列表,里面的按钮的移入事件
需求:移入的时候,互相关注变成取消关注 移入移出事件传参$event,把这个参数打印出来看就可以搞定,而不是移入的时候,文本都改变,只改变当前行 的文本 <p @mouseover=" ...
- cocos2d-x JS 纯代码渲染Lable描边
/** * Enables shadow style and sets color, offset and blur radius styles. * @param {cc.Color} shadow ...