1. 程式人生 > 其它 >spring5基礎學習之jdbctemplate的操作

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用的多