spring5基础学习之jdbctemplate的操作

今天开始写关于spring5的随笔,之前关于底层的ioc和aop之后再补,先把今天学的jdbctemplate的操作放上,spring5知识是从尚硅谷看的,有兴趣可以看看

首先导入依赖就不粘贴了,配置文件格式,jdbc链接配置和数据库表就不粘了

<?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="Dao,Service"></context:component-scan>
    <!--    引入外部配置文件-->
    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="${prop.url}" />
        <property name="username" value="${prop.username}" />
        <property name="password" value="${prop.password}" />
        <property name="driverClassName" value="${prop.driverClassName}" />
    </bean>
<!--    jdbcTemplate对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--        注入datasource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

先建立一个实体类层

package entiy;

public class Book {
    private int u_id;
    private String u_name;
    private String u_type;

    public int getU_id() {
        return u_id;
    }

    public void setU_id(int u_id) {
        this.u_id = u_id;
    }

    public String getU_name() {
        return u_name;
    }

    public void setU_name(String u_name) {
        this.u_name = u_name;
    }

    public String getU_type() {
        return u_type;
    }

    public void setU_type(String u_type) {
        this.u_type = u_type;
    }

    @Override
    public String toString() {
        return "Book{" +
                "u_id=" + u_id +
                ", u_name='" + u_name + '\'' +
                ", u_type='" + u_type + '\'' +
                '}';
    }
}

Dao层接口

package Dao;

import entiy.Book;

import java.util.List;

public interface BookDao {
    void add(Book book);
    void updatebook(Book book);
    void dalete_1(String id);
    int findAll();
    Book findobject(String id);
    List<Book> findlist();
    void batchadd(List<Object[]> batchargs);
}

实现接口:

package Dao;

import entiy.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 BookImpl implements BookDao{
    //注入jdbctemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //添加
    @Override
    public void add(Book book) {
        String sql="insert into t_user values(?,?,?)";
        Object args[]={book.getU_id(),book.getU_name(),book.getU_type()};
        int updata=jdbcTemplate.update(sql,args);
        System.out.println(updata);
    }

    //更新
    @Override
    public void updatebook(Book book) {
        String sql="update t_user set u_name=?,u_type=? where u_id=?";
        Object args[]={book.getU_name(),book.getU_type(),book.getU_id()};
        int updata=jdbcTemplate.update(sql,args);
        System.out.println(updata);
    }

    //删除
    @Override
    public void dalete_1(String id) {
        String sql="delete from t_user where u_id=?";
        int dalete=jdbcTemplate.update(sql,id);
        System.out.println(dalete);
    }

    //查询
    @Override
    public int findAll() {
        String sql="select count(*) from t_user";
        Integer cout=jdbcTemplate.queryForObject(sql,Integer.class);
        return cout;
    }

    //查询返回对象
    @Override
    public Book findobject(String id) {
        String sql="select * from t_user where u_id=?";
        //使用BeanPropertyRowMapper封装Book,要不然会报错,查询一个会返回多个
        Book book=jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Book>(Book.class),id);
        return book;
    }

    //查询返回list
    @Override
    public List<Book> findlist() {
        String sql="select * from t_user";
        List<Book> books=jdbcTemplate.query(sql,new BeanPropertyRowMapper<Book>(Book.class));
        return books;

    }

    //批量操作
    @Override
    public void batchadd(List<Object[]> batchargs) {
        String sql="insert into t_user values(?,?,?)";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchargs);
        System.out.println(Arrays.toString(ints));
    }


}

service层

package Service;

import Dao.BookDao;
import entiy.Book;
import org.aspectj.lang.annotation.Around;
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 updatebook(Book book){
        bookDao.updatebook(book);
    }
    public void deletebook(String id){
        bookDao.dalete_1(id);
    }
    public int select(){
        return bookDao.findAll();
    }
    public Book selectbook(String id){
        return bookDao.findobject(id);
    }
    public List<Book> selectList(){
        return bookDao.findlist();
    }
    public void batch(List<Object[]> batchargs){
        bookDao.batchadd(batchargs);
    }

}

测试:

package test;

import Service.BookService;
import entiy.Book;
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 {
    @Test
    public void testjdbc(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        BookService bookService=context.getBean("bookService",BookService.class);
        Book book=new Book();
        book.setU_id(3);
        book.setU_name("西游记");
        book.setU_type("1");
        bookService.addbook(book);
    }
    @Test
    public void testjdbc1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        BookService bookService=context.getBean("bookService",BookService.class);
        Book book=new Book();
        book.setU_id(2);
        book.setU_name("水浒传");
        book.setU_type("2");
        bookService.updatebook(book);
    }
    @Test
    public void testjdbc2(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        BookService bookService=context.getBean("bookService",BookService.class);
        Book book=new Book();
        bookService.deletebook("2");
    }
    @Test
    public void testjdbc3(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        BookService bookService=context.getBean("bookService",BookService.class);
        Book book=new Book();
        int a=bookService.select();
        System.out.println(a);
    }

    @Test
    public void testjdbc4(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        BookService bookService=context.getBean("bookService",BookService.class);
        Book book=bookService.selectbook("1");
        System.out.println(book);
    }
    @Test
    public void testjdbc5(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        BookService bookService=context.getBean("bookService",BookService.class);
        System.out.println(bookService.selectList());
    }
    @Test
    public void testjdbc6(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        BookService bookService=context.getBean("bookService",BookService.class);
        List<Object[]> batchargs=new ArrayList<>();
        Object[] o1= {"4","cqwe","4"};
        Object[] o2= {"4","cqwe","4"};
        Object[] o3= {"4","cqwe","4"};
        batchargs.add(o1);
        batchargs.add(o2);
        batchargs.add(o3);
        bookService.batch(batchargs);
    }

}

效果

 

 

 

 总的来说只是个简单数据库操作,了解基础就行,以后会用MyBits用的多

posted @ 2022-04-06 21:25  chenghaixinag  阅读(26)  评论(0编辑  收藏  举报