目录
JdbcTemplate(操作数据库-删除功能)
1.创建数据库
2.配置文件实现
3.创建实体类
4.创建dao层
5.创建service层
6.测试类
7.测试结果:
JdbcTemplate(操作数据库-删除功能)
1.创建数据库
user_db数据库的t_book表中创建两条记录 user_id 和 username
2.配置文件实现
注意修改数据库的端口号、表名、数据库账号密码,特别是一些jar包的版本问题,降低版本号即可
<?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"xmlns:aop="http://www.springframework.org/schema/aop"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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 开启组件扫描--><context:component-scan base-package="org.example"></context:component-scan>
<!--数据库连接池--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"><property name="url" value="jdbc:mysql://localhost:3306/user_db?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="sise"/><property name="driverClassName" value="com.mysql.jdbc.Driver"/></bean><!-- 创建jdbcTemplate对象--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--需要注入数据源信息--><property name="dataSource" ref="dataSource"></property></bean>
</beans>
3.创建实体类
Book实体类的属性对应 t_book表中的记录
package org.example.spring.entity;public class Book {private String userId;private String username;public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}}
4.创建dao层
接口层:
package org.example.spring.dao;import org.example.spring.entity.Book;public interface BookDao {//添加的方法void add(Book book);//修改的方法void update(Book book);//删除的方法void delete(int id);
}
实现层:
实现层中实现对数据的增删改
package org.example.spring.dao;import org.example.spring.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;@Repository
public class BookDaoImpl implements BookDao{//注入jdbcTemplate对象@Autowiredprivate JdbcTemplate jdbcTemplate;//添加的方法,从实体类Book中传递对象进来@Overridepublic void add(Book book) {//1.创建sql语句String sql="insert into t_book values(?,?)";//2.调用方法实现//3.参数是可变参数,是一个数组形式,因此存在一种变式
// int update = jdbcTemplate.update(sql, book.getUserId(), book.getUsername());//变式为Object[] args= {book.getUserId(), book.getUsername()};int update = jdbcTemplate.update(sql, args);//输出影响的值System.out.println(update);}@Overridepublic void update(Book book) {String sql="update t_book set username=? where user_id=?";Object[] args={book.getUsername(),book.getUserId()};int update=jdbcTemplate.update(sql,args);System.out.println(update);}@Overridepublic void delete(int id) {String sql="delete from t_book where user_id=?";int update=jdbcTemplate.update(sql,id);System.out.println(update);}
}
5.创建service层
调用dao层对象进行数据传递,这层写逻辑代码
package org.example.spring.service;import org.example.spring.dao.BookDao;
import org.example.spring.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class BookService {//注入dao@Autowiredprivate BookDao bookDao;//添加的方法public void addBook(Book book){bookDao.add(book);}//修改的方法public void updateBook(Book book){bookDao.update(book);}//删除的方法(注意,删除是根据id去删除)public void deleteBook(int id){bookDao.delete(id);}
}
6.测试类
package org.example.spring.test;import org.example.spring.entity.Book;
import org.example.spring.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBook
{@Testpublic void test01(){ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");BookService bookService = context.getBean("bookService", BookService.class);//因为没有写到页面数据,所以直接通过new对象实现//添加
// Book book=new Book();
// book.setUserId("2");
// book.setUsername("abc");
//
// bookService.addBook(book);//修改
// Book book=new Book();
// book.setUserId("1");
// book.setUsername("123");
//
// bookService.updateBook(book);//删除bookService.deleteBook(1);}}
7.测试结果:
第一条记录已经删除