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:切入点,真正被拦截的点,指对哪 ...
随机推荐
- Linux下solr集群搭建
第一步:创建四个tomcat实例.每个tomcat运行在不同的端口.8180.8280.8380.8480 第二步:部署solr的war包.把单机版的solr工程复制到集群中的tomcat中. 第三步 ...
- linux sed 常见字符串处理
1.删除特殊字符 将 1.1.0_boke_1.0.1 转换为110_boke_101 command: new_var=`echo 1.1.0_boke_1.0.1 |sed s/\.//g` ( ...
- windows系统上利用putty通过SSH连接亚马逊AWS服务器
1. 找到在购买亚马逊的AWS服务器时保存的密钥文件(假设为abc.pem). 2.打开PuTTYgen,如下图,点击图中1处的“load”,找到abc.pem文件所在的位置,并选择abc.pem,确 ...
- 【转360】KB4041678 Windows 仅安全更新(2017.10) 补丁更新后执行SQL出错! http://bbs.360.cn/thread-15201531-1-1.html
把EXCEL20003表数据导入到MDB数据库中sql命令语句\"SELECT * INTO 表 FROM [Excel 8.0;DATABASE=C:\\1.xls].[Sheet1$]\ ...
- 破解sublime的sftp
http://www.dodobook.net/linux/2751,按照这个在Linux下操作(Windows下不行) 提示错误: File "/usr/lib/python2.7/sit ...
- tomcat优化和JVM修改内存
Tomcat中的线程池(APR和ThreadPool) 2. 在Connector中指定使用共享线程池: <Connector executor="tomcatThreadPool&q ...
- 10.C# 构造函数
1.构造函数 构造函数是用来初始化对象的,只能由new运算符调用.构造函数与类同名,没有返回值,不能用void修饰,可以有public和private两种修饰符,当用private修饰时外界不能访问到 ...
- 一步一步学Python(3) 基础补充
最近在系统学习Python,以MOOC上面的一套Python3的课程为基础.本文主要总结一下基础部分的关键点. 1.python基本数据类型 2.python运算符 3.构建简洁高效的IDE环境 4. ...
- jQuery-淡入淡出效果-fadeIn()淡进 fadeOut()淡出 fadeToggle();
动画高级函数:基于底层函数又进行了封装两大块:简化版的动画函数和万能动画函数简化版动画函数显示/隐藏$().show; $(...).hide(); 强调:无参数的show()/hide()使用的 ...
- supervison
http://blog.csdn.net/kongxx/article/details/50452357