spring框架-jdbcTemplate
首先 dao层:
dao
-bookdao(interface)
-bookdaoimpl
service层:
bookService
实体类对象
entiry-book
测试类 Test-TestBook
结构如图所示:
xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com"></context:component-scan>
<!-- 数据库连接池 -->
<!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"-->
<!-- destroy-method="close">-->
<!-- <property name="url" value="jdbc:mysql:///user_db" />-->
<!-- <property name="username" value="root" />-->
<!-- <property name="password" value="110120" />-->
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />-->
<!-- </bean>-->
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClassName}"/>
<property name="url" value="${prop.url}"/>
<property name="username" value="${prop.username}"/>
<property name="password" value="${prop.password}"/>
</bean>
<!--jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 注入datasource-->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
jdbc的配置文件:
#prop.url=jdbc:mysql://localhost:3306/user_db?rewriteBatchedStatements=true
prop.url=jdbc:mysql://localhost:3306/user_db?useUnicode=true&characterEncoding=utf-8
prop.username=root
prop.password=110120
prop.driverClassName=com.mysql.jdbc.Driver
initialSize=10
maxActive=20
maxWait=1000
filters=wall
所需要jar包:
下面为具体代码:
dao层代码:
package com.dao;
import com.entiry.Book;
import java.util.List;
public interface bookDAO {
void add(Book book) ;
void update(Book book);
void delById(String Id);
int selectCount();
Book selectBook(String id);
List<Book> findList();
void addBatchList(List<Object[]> list);
void batchUpdateBook(List<Object[]> lists);
void batchDel(List<Object[]> list);
}
package com.dao;
import com.entiry.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.Arrays;
import java.util.List;
@Repository
public class bookDAOImpl implements bookDAO{
//注入jdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
//添加的方法
@Override
public void add(Book book) {
String sql = "insert into book values(?,?,?)";
int update = jdbcTemplate.update(sql, book.getUserId(), book.getUsername(), book.getUstatus());
if (update == 1){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
}
@Override
public void update(Book book) {
String sql = "update book set username = ?,usatus = ? where user_id = ?";
int update = jdbcTemplate.update(sql, book.getUsername(), book.getUstatus(), book.getUserId());
if (update == -1){
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
}
@Override
public void delById(String Id) {
String sql = "delete from book where user_id = ?";
int update = jdbcTemplate.update(sql, Id);
if (update == 1){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
@Override
public int selectCount() {
String sql = "select count(*) from book";
Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
return integer;
}
@Override
public Book selectBook(String id) {
String sql = "select * from book where user_id = ?";
Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
return book;
}
@Override
public List<Book> findList() {
String sql = " select * from book";
List<Book> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
return bookList;
}
@Override
public void addBatchList(List<Object[]> list) {
String sql = "insert into book values(?,?,?)";
int[] ints = jdbcTemplate.batchUpdate(sql,list);
System.out.println(Arrays.toString(ints));
}
@Override
public void batchUpdateBook(List<Object[]> lists) {
String sql = "update book set username =?,usatus = ? where user_id = ?";
int[] ints = jdbcTemplate.batchUpdate(sql, lists);
System.out.println(Arrays.toString(ints));
}
@Override
public void batchDel(List<Object[]> list) {
String sql ="delete from book where user_id = ?";
int[] ints = jdbcTemplate.batchUpdate(sql, list);
System.out.println(Arrays.toString(ints));
}
}
service层:
package com.service;
import com.dao.bookDAO;
import com.entiry.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookService {
//注入dao
@Autowired
private bookDAO bookdao;
//添加
public void addBook(Book book){
bookdao.add(book);
}
//修改
public void update(Book book){
bookdao.update(book);
}
//删除
public void delById(String Id){
bookdao.delById(Id);
}
//查询返回某个值
public int FindCount(){
return bookdao.selectCount();
}
//查询返回某个对象
public Book findBook(String id){
return bookdao.selectBook(id);
}
//返回多个对象
public List<Book> findList(){
return bookdao.findList();
}
//批量添加
public void BatchAdd(List<Object[]> list){
bookdao.addBatchList(list);
}
//批量修改
public void batchUpdateBook(List<Object []> lists){
bookdao.batchUpdateBook(lists);
}
//批量删除
public void batchDel(List<Object []> list){
bookdao.batchDel(list);
}
}
entiry层:
package com.entiry;
public class Book {
private String userId;
private String username;
private String ustatus;
public Book() {
}
public Book(String userId, String username, String ustatus) {
this.userId = userId;
this.username = username;
this.ustatus = ustatus;
}
public void setUserId(String userId) {
this.userId = userId;
}
public void setUsername(String username) {
this.username = username;
}
public void setUstatus(String ustatus) {
this.ustatus = ustatus;
}
public String getUserId() {
return userId;
}
public String getUsername() {
return username;
}
public String getUstatus() {
return ustatus;
}
@Override
public String toString() {
return "Book{" +
"userId='" + userId + '\'' +
", username='" + username + '\'' +
", ustatus='" + ustatus + '\'' +
'}';
}
}
测试类:
package com.Test;
import com.entiry.Book;
import com.service.BookService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;
import java.util.List;
public class TestBook {
/**
* 测试jdbc 的add 方法
*/
@Test
public void testADD (){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
bookService.addBook(new Book("3","Tom","a"));
}
/**
* 测试修改功能
*/
@Test
public void testUpdate(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
bookService.update(new Book("1","Rose1","atuguig"));
}
/**
* 测试删除功能
*/
@Test
public void testDel(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
bookService.delById("1");
}
/**
* 测试查询个数的功能
*/
@Test
public void testSelectCount(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
int count = bookService.FindCount();
System.out.println(count);
}
/**
* 测试查询单个对象的功能
*/
@Test
public void testSelectOneBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
Book book = bookService.findBook("1");
System.out.println(book.toString());
}
/**
* 测试查询多个对象的功能
*/
@Test
public void testSelectAllBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
List<Book> list = bookService.findList();
System.out.println(list);
}
/**
* 测试批量添加多个对象的功能
*/
@Test
public void testAddSomeBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
List<Object[]> list = new ArrayList<>();
Object [] o1 = {"4","javac","a"};
Object [] o2 = {"5","c","b"};
Object [] o3 = {"6","c++","a"};
list.add(o1);
list.add(o2);
list.add(o3);
bookService.BatchAdd(list);
}
/**
* 测试批量修改多个对象的功能
*/
@Test
public void testModifySomeBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
List<Object[]> list = new ArrayList<>();
Object [] o1 = {"javac1","a","4"};
Object [] o2 = {"c1","b","5"};
Object [] o3 = {"c++1","a","6"};
list.add(o1);
list.add(o2);
list.add(o3);
bookService.batchUpdateBook(list);
}
/**
* 测试批量删除多个对象的功能
*/
@Test
public void testDelSomeBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
List<Object[]> list = new ArrayList<>();
Object [] o1 = {"4"};
Object [] o2 = {"5"};
Object [] o3 = {"6"};
list.add(o1);
list.add(o2);
list.add(o3);
bookService.batchDel(list);
}
}
数据库表格:
测试结果均可!
spring框架-jdbcTemplate的更多相关文章
- Spring框架 JdbcTemplate类 @Junit单元测试,可以让方法独立执行 如:@Test
package cn.zmh.PingCe; import org.junit.Test; import org.springframework.jdbc.core.BeanPropertyRowMa ...
- 设计模式(十五)——命令模式(Spring框架的JdbcTemplate源码分析)
1 智能生活项目需求 看一个具体的需求 1) 我们买了一套智能家电,有照明灯.风扇.冰箱.洗衣机,我们只要在手机上安装 app 就可以控制对这些家电工作. 2) 这些智能家电来自不同的厂家,我们不想针 ...
- Spring框架之使用JdbcTemplate开发Dao层程序
简介: JdbcTemplate开发dao层程序 由Spring框架给我们提供,Spring提供的很多操作数据源(关系型数据库,二维表格模型,有明确的行和列(mysql/orcal等) 非关系 ...
- 使用spring框架的JdbcTemplate实现对Oracle数据库的简单操作实例
最近实现了一个小功能,针对Oracle数据库两张关联表进行查询和修改,因为比较简单,所以选择了spring框架里的JdbcTemplate.JdbcTemplate算是老古董了,是当年spring为了 ...
- Spring框架针对dao层的jdbcTemplate操作crud之delete删除数据库操作 Spring相关Jar包下载
首先,找齐Spring框架中IoC功能.aop功能.JdbcTemplate功能所需的jar包,当前13个Jar包 1.Spring压缩包中的四个核心JAR包,实现IoC控制反转的根据xml配置文件或 ...
- Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作
查询目标是完成3个功能: (1)查询表,返回某一个值.例如查询表中记录的条数,返回一个int类型数据 (2)查询表,返回结果为某一个对象. (3)查询表,返回结果为某一个泛型的list集合. 一.查询 ...
- Spring框架针对dao层的jdbcTemplate操作crud之update修改数据库操作
使用jdbcTemplate 原理是把加载驱动Class.forName("com.mysql.jdbc.Driver"); 和连接数据库Connection conn=Drive ...
- Spring框架针对dao层的jdbcTemplate操作crud之add添加数据库操作
使用jdbcTemplate 原理是把加载驱动Class.forName("com.mysql.jdbc.Driver"); 和连接数据库Connection conn=Drive ...
- Spring框架之JdbcTemplate
Spring框架之JdbcTemplate 一.JdbcTemplate简介 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到Jd ...
随机推荐
- APT 安装 MySQL 提示错误:dpkg: error: dpkg frontend lock is locked by another process
在安装 MySQL 的时候提示错误: ubuntu@VM-0-6-ubuntu:/opt$ sudo dpkg -i mysql-apt-config_0.8.22-1_all.deb dpkg: e ...
- 线性代数 | Jordan 标准型的笔记
内容概述: 把方阵 A 的特征多项式 \(c(λ)=|λE-A|\) 展开成 \(c(λ)=\sum_ia_i\lambda^i\) 的形式,然后使用神乎其技的证明,得到 \(c(A)=O\),特征多 ...
- Ubuntu20.04配置 ES7.17.0集群
Ubuntu20.04配置 ES7.17.0集群 ES能做什么? elasticsearch简写es,es是一个高扩展.开源的全文检索和分析引擎,它可以准实时地快速存储.搜索.分析海量的数据. Ubu ...
- CF1528C Trees of Tranquillity(图论,数据结构)
题面 有两棵 n n n 个点的有根树 T 1 T_1 T1, T 2 T_2 T2,根是 1 1 1 ,共用编号 1 1 1~ n n n.求最大的点集 S S S 满足每个点在 T 1 T_1 ...
- Python入门系列(七)开发常说的“累”与“对象”
类与对象 Python是一种面向对象的编程语言. 要创建类,请使用关键字class class MyClass: x = 5 创建一个名为p1的对象,并打印x的值 p1 = MyClass() pri ...
- CodeForces - 1690F
Problem - F - Codeforces 题意: 给出一个字符串,给出一个序列,每次对应位置的字符变成序列指定位置的字符,即序列中对应位置为2,那么字符串的这个位置的字符就要变成字符串第二个位 ...
- 记一次用arthas排查jvm中CPU占用过高问题
记一次使用arthas排查jvm中CPU占用过高问题.这工具屌爆了 碾压我目前使用的全部JVM工具. 安装 小试 curl -O https://arthas.aliyun.com/arthas-bo ...
- 安装配置华为Fusion access(Windows AD)桌面云并对接Fusion Compute与Windows_server_2012
安装Fusion access虚拟机 根据自己情况自定义 点击编辑虚拟机设置 添加镜像 点击确定并开启此虚拟机 选择第二个 Install 添加Network:修改hostname:修改密码 回车添加 ...
- OpenCV读写视频操作
一.读取视频流 在使用OpenCV读取摄像头,或者处理一些磁盘中保存的视频文件时,通常使用VideoCapture进行读取. std::string video_path("/path/to ...
- RedHat Linux升级内核
操作系统:Red Hat 6.4 内核文件:linux-3.10.1.tar.gz https://www.cnblogs.com/cherish-sweet/p/newyum.html uname ...