创建一个Service类和一个Dao接口,一个实现Dao接口的实现类DaoImpl类
BookService类 package com.robin.Spring5.JdbcTmplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; @Component public class BookService { @Autowired private BookDao bookdao; public void addBook( Book book){ bookdao.addBook(book); } // 修改 public void updateBook(Book book) { bookdao.updateBook(book); } // 删除 public void deletecol(String id){ bookdao.deletecol(id); } //查询返回某一个值 public int queryCount() { return bookdao.queryCount(); } //查询ID,返回一条记录 public Book queryRow(String id){ return (Book) bookdao.queryRow(id); } // 将整个表返回 public List<Book> queryAll(){ return bookdao.queryAll(); } }BookDao接口
package com.robin.Spring5.JdbcTmplate; import java.util.List; public interface BookDao { public void addBook(Book book); void updateBook(Book book); void deletecol(String id); int queryCount(); Book queryRow(String id); public List<Book> queryAll(); }BookDaoImpl类
package com.robin.Spring5.JdbcTmplate; 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 { @Autowired private JdbcTemplate jdbcTemplate; public void addBook( Book book){ String sql="insert into t_book values(?,?,?)"; Object[] args={book.getbID(),book.getbName(),book.getbDesc()}; int update=jdbcTemplate.update(sql,args); System.out.println("操作影响的数据行数是;"+update); } @Override public void updateBook(Book book) { String sql="update t_book set bName=?,bDesc=? where bID=? "; Object[] args={book.getbName(),book.getbDesc(),book.getbID()}; int update=jdbcTemplate.update(sql,args); System.out.println("操作影响的数据行数是;"+update); } @Override public void deletecol(String id) { String sql="delete from t_book where bID=?"; Object[] args={id}; int update=jdbcTemplate.update(sql,args); System.out.println("操作影响的数据行数是;"+update); } @Override public int queryCount() { String sql="select count(*) from t_book"; Integer integer = jdbcTemplate.queryForObject(sql, Integer.class); return integer; } @Override public Book queryRow(String id) { String sql="select * from t_book where bID=?"; Book book= jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id); return book; } @Override public List<Book> queryAll(){ String sql="select * from t_book "; List<Book> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class)); return query; } }Book类
package com.robin.Spring5.JdbcTmplate; public class Book { private String bID; private String bName; private String bDesc; public void setbID(String bID) { this.bID = bID; } public void setbName(String bName) { this.bName = bName; } public void setbDesc(String bDesc) { this.bDesc = bDesc; } public String getbID() { return bID; } public String getbName() { return bName; } public String getbDesc() { return bDesc; } @Override public String toString() { return "Book{" + "bID='" + bID + '\'' + ", bName='" + bName + '\'' + ", bDescription='" + bDesc + '\'' + '}'; } }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" 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="com.robin.Spring5.JdbcTmplate"></context:component-scan> <!--创建数据源--> <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/robin"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!--注入数据源--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>TestJDBCTemplate
package com.robin.Spring5.JdbcTmplate; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class TestJdbcTemplate { @Test public void TestJT() { ApplicationContext context=new ClassPathXmlApplicationContext("bean3.xml"); BookService bookService=context.getBean("bookService" ,BookService.class); Book book = new Book(); book.setbID("001"); book.setbName("一个虚拟的世界!"); book.setbDesc("这个世界本省不存在,而是有人们虚拟的构建出来的!"); book.setbID("005"); book.setbName("伟大的上帝"); book.setbDesc("上帝的思想"); book.setbID("006"); book.setbName("java"); book.setbDesc("反射"); book.setbID("007"); book.setbName("Spring伟大的上帝"); book.setbDesc("AOP的思想这个世界本省来们虚拟的构建出来的!"); System.out.println(book); bookService.addBook(book); bookService.updateBook(book); bookService.deletecol("00"); int count=bookService.queryCount(); System.out.println(count); Book bookROW=bookService.queryRow("005"); System.out.println(bookROW); List<Book> list = bookService.queryAll(); System.out.println(list); } }