首页 > 数据库 >Spring-JdbcTemplate实现数据库增、删、改、查

Spring-JdbcTemplate实现数据库增、删、改、查

时间:2022-10-03 15:34:30浏览次数:54  
标签:String bookService Spring 数据库 public Book void JdbcTemplate book

一、先在mysql创建好一个数据库

二、使用JdbcTemplate需要引入的依赖(spring的maven项目)

<!-- jdbcTemplate实现jar -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
</dependency>
 
<!-- 数据库驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.43</version>
</dependency>
 
<!-- 数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.12</version>
</dependency>
<!-- 事务管理 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>

<!--上下文-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.22</version>
</dependency>

<!--beans依赖-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.3.23</version>
</dependency>

<!--core核心-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.3.22</version>
</dependency>

三、环境搭配----bean.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 http://www.springframework.org/schema/context/spring-context.xsd">

    <!--开启组件扫描-->
    <context:component-scan base-package="com.lxh.spring5"></context:component-scan>
    <!--创建数据源(带druid连接池)-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="jdbc:mysql://localhost/book"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="username" value="root"></property>
        <property name="password" value="laixinghai"></property>

    </bean>
    <!--创建jdbcTemplate对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入dataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

四、创建实体类Book

public class Book {
    private String userId; // 用户id
    private String username; // 用户名
    private String ustatus; // 状态

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

    public String getUstatus() {
        return ustatus;
    }

    public void setUstatus(String ustatus) {
        this.ustatus = ustatus;
    }

    @Override
    public String toString() {
        return "Book{" +
                "userId='" + userId + '\'' +
                ", username='" + username + '\'' +
                ", ustatus='" + ustatus + '\'' +
                '}';
    }
}

 

五、dao层代码

BookDao接口

import com.lxh.spring5.entity.Book;

import java.util.List;

public interface BookDao {

    // 添加方法
    public void add(Book book);

    // 修改方法
    public void updateBook(Book book);

    // 删除方法
    public void delete(String id);

    // 查询表记录的个数
    public int selectCount();

    // 类查询指定id查找
    public Book findBookInfo(String id);

    // 查找全部
    public List<Book> findAllBook();
}

BookDaoImpl实现类

import com.lxh.spring5.entity.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.List;

@Repository
public class BookDaoImpl implements BookDao{

    // 注入JDBCTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 添加方法
    @Override
    public void add(Book book) {
        // 1.创建sql语句
        String  sql = "insert into t_book value(?,?,?)";
        // 2.1调用方法实现
        // int update = jdbcTemplate.update(sql, book.getUserId(), book.getUsername(), book.getUstatus());
        // 2.2调用方法实现
        Object[] obj = new Object[] {book.getUserId(), book.getUsername(), book.getUstatus()};
        int update = jdbcTemplate.update(sql, obj);
        System.out.println(update);
    }

    // 修改方法
    @Override
    public void updateBook(Book book) {
        String sql = "update t_book set username=?, ustatus=? where user_id=?";
        Object[] obj = new Object[] {book.getUsername(), book.getUstatus(), book.getUserId()};
        int update = jdbcTemplate.update(sql, obj);
        System.out.println(update);
    }

    // 删除
    @Override
    public void delete(String id) {
        String sql = "delete from t_book where user_id=?";

        int update = jdbcTemplate.update(sql, id);
        System.out.println(update);
    }

    // 查询表记录的个数
    @Override
    public int selectCount() {
        String sql = "select count(*) from t_book";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count;
    }

    // 查询返回对象
    @Override
    public Book findBookInfo(String id) {
        String sql = "select * from t_book where user_id=?";
        // RowMapper接口,针对返回不同类型数据,使用这个接口实现类完成数据封装
        Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
        return book;
    }

    // 查询返回集合
    @Override
    public List<Book> findAllBook() {
        String sql = "select * from t_book";
        // RowMapper接口,针对返回不同类型数据,使用这个接口实现类完成数据封装
        List<Book> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
        return query;
    }
}

六、service层代码

import com.lxh.spring5.dao.BookDao;
import com.lxh.spring5.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "bookService")
public class BookService {

    // 注入dao
    @Autowired
    private BookDao bookDao;

    // 调用添加方法
    public void addBook(Book book) {
        bookDao.add(book);
    }

    // 调用修改方法
    public void updateBook(Book book) {
        bookDao.updateBook(book);
    }

    // 调用删除方法
    public void delete(String id) {
        bookDao.delete(id);
    }

    // 调用查询表记录的个数
    public int find() {
        return bookDao.selectCount();
    }

    // 调用查询返回对象
    public Book findOne(String id) {
        return bookDao.findBookInfo(id);
    }

    // 调用查询返回集合
    public List<Book> findAll() {
        return bookDao.findAllBook();
    }
}

七、测试代码

import com.lxh.spring5.entity.Book;
import com.lxh.spring5.service.BookService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBook {

    // 添加
    @Test
    public void testAdd() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");

        BookService bookService = context.getBean("bookService", BookService.class);

        Book book = new Book();
        book.setUserId("1");
        book.setUsername("java");
        book.setUstatus("A");
        bookService.addBook(book);
    }

    // 修改
    @Test
    public void testUpdate() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");

        BookService bookService = context.getBean("bookService", BookService.class);

        Book book = new Book();
        book.setUserId("1");
        book.setUsername("python");
        book.setUstatus("b");
        bookService.updateBook(book);
    }

    // 删除
    @Test
    public void testDelete() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");

        BookService bookService = context.getBean("bookService", BookService.class);

        bookService.delete("1");
    }

    // 查询记录个数
    @Test
    public void testSelectCount() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");

        BookService bookService = context.getBean("bookService", BookService.class);

        System.out.println(bookService.find());
    }

    // 类查询指定id查找
    @Test
    public void testFindBookInfo() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");

        BookService bookService = context.getBean("bookService", BookService.class);

        System.out.println(bookService.findOne("1"));
    }

    // 查找返回集合
    @Test
    public void testFindAllBook() {
        // 1.创建Spring配置文件对象
        ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
        // 2.调用对象
        BookService bookService = context.getBean("bookService", BookService.class);

        System.out.println(bookService.findAll());
    }
}

 

标签:String,bookService,Spring,数据库,public,Book,void,JdbcTemplate,book
From: https://www.cnblogs.com/lxh-daniel/p/16750586.html

相关文章