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);

}
}
数据库表格:
  

 

 

 

 

 

 测试结果均可!

posted @ 2022-03-22 14:41  wiselee/  阅读(36)  评论(0编辑  收藏  举报