Spring5自学笔记_Real004(AOP(面向切面编程))

Spring5自学笔记_Real004(JDBCTemplate)

一、概念与相关准备工作

1、什么是JdbcTemplate
(1)Spring矿建对JDBC进行了封装,使用JdbcTemplate方便实现对数据库的操作

2、准备工作
(1)引入相关jar包
Spring5自学笔记_Real004(AOP(面向切面编程))_第1张图片
(2)在Spring配置文件中配置数据库的连接池


<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">

    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver">property>
        <property name="url" value="jdbc:mysql://localhost:3306/userDB">property>
        <property name="username" value="root">property>
        <property name="password" value="000000">property>
    bean>
beans>

3、配置JdbcTemplate对象,并且注入DataSource信息

    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        
        <property name="dataSource" ref="dataSource">property>
    bean>

4、同时配置文件打开组件扫描,利用注解方式注入属性。创建service类,创建dao类,在dao中注入jdbcTemplate对象,在Service中注入dao对象。
(1)配置文件

    
    <context:component-scan base-package="com.SSMStudy.Spring5.JDBCTemplateStudy">context:component-scan>

(2)创建dao接口以及dao类的实现类

public interface BookDao {
}

@Repository
public class BookDaoImpl implements BookDao{

    //注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

}

(3)创建Service类

@Service
public class BookService {

    //注入Dao
    @Autowired
    private BookDao bookDao;
}

二、JdbcTemplate操作数据库(添加)

1、对应数据库中的表格,添加实体类。(在数据库中已经建好表格)

Spring5自学笔记_Real004(AOP(面向切面编程))_第2张图片

public class Book {
    private String userid;
    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;
    }
}

2、编写service和dao
(1)在dao数据库添加操作
Spring5自学笔记_Real004(AOP(面向切面编程))_第3张图片

//在Service类中添加AddBook()方法,里面调用了Dao中的add方法
@Service
public class BookService {

    //注入Dao
    @Autowired
    private BookDao bookDao;

    //添加Book数据到数据库里面的方法
    public void addBook(Book book){
        bookDao.add(book);
    }
}
//Dao类中的asdd()方法调用了JdbcTemplate对象中的Update方法,Update()是用来添加、更新、删除、修改数据用的
@Repository
public class BookDaoImpl implements BookDao{

    //注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(Book book) {
        
        String sql = "insert into t_book values(?, ?, ?)";
        //int update = jdbcTemplate.update(sql, book.getUserid(), book.getUsername(), book.getUstatus());
        Object[] args = {book.getUserid(), book.getUsername(), book.getUstatus()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println(update);
    }
}

3、测试:

public class TestBook {
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        BookService bookService = context.getBean("bookService", BookService.class);
        Book book = new Book();
        book.setUserid("2");
        book.setUsername("三国演义");
        book.setUstatus("售罄");

        bookService.addBook(book);
    }
}

输出结果:
1
Spring5自学笔记_Real004(AOP(面向切面编程))_第4张图片

三、JdbcTemplate操作数据库(修改和删除)

用到的JdbcTemplate类中的方法是一样的,只是sql语句不一样而已!
(1)dao接口

public interface BookDao {
    //添加书籍
    void add(Book book);

    //修改书籍
    void updateBook(Book book);

    //删除书籍
    void delete(Book book);
}

(2)Dao的实现类

@Repository
public class BookDaoImpl implements BookDao{

    //注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(Book book) {
        
        String sql = "insert into t_book values(?, ?, ?)";
        //int update = jdbcTemplate.update(sql, book.getUserid(), book.getUsername(), book.getUstatus());
        Object[] args = {book.getUserid(), book.getUsername(), book.getUstatus()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println(update);
    }

    @Override
    public void updateBook(Book book) {
        String sql = "update t_book set username=?, ustatus=? where user_id=?";
        Object[] args = {book.getUsername(), book.getUstatus(), book.getUserid()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println(update);
    }

    @Override
    public void delete(Book book) {
        String sql = "delete from t_book where user_id=?";
        int update = jdbcTemplate.update(sql, book.getUserid());
        System.out.println(update);
    }
}

(3)Service类

@Service
public class BookService {

    //注入Dao
    @Autowired
    private BookDao bookDao;

    //添加Book数据到数据库里面的方法
    public void addBook(Book book){
        bookDao.add(book);
    }

    //修改数据
    public void updateBook(Book book){
        bookDao.updateBook(book);
    }

    //删除数据
    public void deleteBook(Book book){
        bookDao.delete(book);
    }
}

(4)测试:

public class TestBook {
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        BookService bookService = context.getBean("bookService", BookService.class);

        //修改操作
        Book book1 = new Book();
        book1.setUserid("2");
        book1.setUsername("三国演义");
        book1.setUstatus("有货了");
        bookService.updateBook(book1);
    }
}

输出结果:
在这里插入图片描述

public class TestBook {
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        BookService bookService = context.getBean("bookService", BookService.class);

        //删除操作
        Book book3 = new Book();
        book3.setUserid("2");
        //book3.setUstatus("有货了");
        //book3.setUsername("三国演义");
        bookService.deleteBook(book3);
    }
}

输出结果:
在这里插入图片描述

四、JdbcTemplate操作数据库(查询操作)

(一)查询返回某个值

由于类的所有结构与修改、添加、删除等操作的结构是一致的,所以这里只写Dao接口的实现类的方法即可。

    /**
     * 铲鲟返回表格中有多少数据
     * @return
     */
    @Override
    public int selectCount() {
        String sql = "select count(*) from t_book";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count;
    }

在这里插入图片描述
两个参数:
第一个参数:SQL语句
第二个参数:返回类型的.class对象

(二)查询返回对象Spring5自学笔记_Real004(AOP(面向切面编程))_第5张图片

    @Override
    public Book findBookInfo(String id) {
        String sql = "select * from t_book where id=?";
        Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id)
        return book;
    }

(三)查询返回集合

场景:查询图书列表并分页显示
Spring5自学笔记_Real004(AOP(面向切面编程))_第6张图片

    @Override
    public List<Book> findAllBook() {
        String sql = "select * from t_book";
        List<Book> books = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
        return books;
    }

测试:

public class TestBook {
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        BookService bookService = context.getBean("bookService", BookService.class);

        List<Book> books = bookService.findAll();
        for (Book book1 : books) {
            System.out.println(book1);
        }

    }
}
结果:
Book{userid='1', username='vbnm', ustatus='dfbn'}
Book{userid='2', username='ertt', ustatus='rfn'}
Book{userid='3', username='erfg', ustatus='erthg'}

五、JDBCTemplate操作数据库(批量操作)

1、批量操作:指的是一次性操作表里的多条记录

2、批量添加

Spring5自学笔记_Real004(AOP(面向切面编程))_第7张图片

    @Test
    public void test02(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        BookService bookService = context.getBean("bookService", BookService.class);

        List<Object[]> batchArgs = new ArrayList<Object[]>();
        Object[] o1 = {"4", "java", "a"};
        Object[] o2 = {"5", "c++", "b"};
        Object[] o3 = {"6", "mysql", "c"};
        batchArgs.add(o1);
        batchArgs.add(o2);
        batchArgs.add(o3);

        bookService.batchAdd(batchArgs);
    }
输出结果:
[1, 1, 1]

Spring5自学笔记_Real004(AOP(面向切面编程))_第8张图片

3、批量修改

只要将sql的语句和参数进行修改即可

4、批量删除

只要将sql的语句和参数进行修改即可

你可能感兴趣的:(Spring,spring,java)