- 引入相关jar包
- 创建数据源DataSource数据库连接池
- 创建JdbcTemplate对象,将DataSource注入
- 将service中注入dao,在dao中注入JdbcTemplate
创建数据源DataSource数据库连接池org.springframework spring-core4.3.7.RELEASE org.springframework spring-beans4.3.7.RELEASE org.springframework spring-context4.3.7.RELEASE org.springframework spring-jdbc4.3.7.RELEASE org.springframework spring-tx4.3.7.RELEASE org.junit.jupiter junit-jupiterRELEASE compile org.springframework spring-aop4.2.2.RELEASE org.aspectj aspectjrt1.9.1 org.apache.geronimo.bundles aspectjweaver1.6.8_2 mysql mysql-connector-java8.0.27 com.alibaba druid1.1.4 org.springframework spring-orm4.3.2
创建JdbcTemplate对象,将DataSource注入
将service中注入dao,在dao中注入JdbcTemplate
创建dao接口数据库访问层public interface BookDao {
//增加图书
public int addBook(Book book);
//修改图书
public int updateBookById(Book book);
//删除图书
public int deleteBookById(Integer id);
//查询图书所有数量
public int countBooks();
//查询图书通过id
public Book queryBookById(Integer id);
//查询图书列表
public List queryBookList();
}
创建实现类@Repository
public class BookDaoImpl implements BookDao {
@Autowired
private JdbcTemplate jdbcTemplate;
String sql=null;
@Override public int addBook(Book book) { sql="insert into book values(?,?,?,?)"; Object[] args={book.getBookId(),book.getBookAuthor(),book.getBookName(),book.getBookPrice()}; int update = jdbcTemplate.update(sql, args); return update; } @Override public int updateBookById(Book book) { sql="update book set book_name=? ,book_author=?,book_price=? where book_id=?"; Object[] args={book.getBookName(),book.getBookAuthor(),book.getBookPrice(),book.getBookId()}; int update = jdbcTemplate.update(sql, args); return update; } @Override public int deleteBookById(Integer id) { sql="delete from book where book_id=?"; int update = jdbcTemplate.update(sql, id); return update; } @Override public int countBooks() { sql="select count(*) from book"; Integer integer = jdbcTemplate.queryForObject(sql, Integer.class); return integer; } @Override public Book queryBookById(Integer id) { sql="select * from book where book_id=?"; Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper创建实体类Book(Book.class), id); return book; } @Override public List queryBookList() { sql="select * from book"; List query = jdbcTemplate.query(sql, new BeanPropertyRowMapper (Book.class)); return query; }
package com.wt.pojo; public class Book { private Integer bookId; private String bookName; private double bookPrice; private String bookAuthor; public Book() { } public Book(Integer bookId, String bookName, double bookPrice, String bookAuthor) { this.bookId = bookId; this.bookName = bookName; this.bookPrice = bookPrice; this.bookAuthor = bookAuthor; } public Integer getBookId() { return bookId; } public void setBookId(Integer bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public double getBookPrice() { return bookPrice; } public void setBookPrice(double bookPrice) { this.bookPrice = bookPrice; } public String getBookAuthor() { return bookAuthor; } public void setBookAuthor(String bookAuthor) { this.bookAuthor = bookAuthor; } @Override public String toString() { return "Book{" + "bookId=" + bookId + ", bookName='" + bookName + ''' + ", bookPrice=" + bookPrice + ", bookAuthor='" + bookAuthor + ''' + '}'; } }创建service
package com.wt.service; import com.wt.pojo.Book; import java.util.List; public interface BookService { //增加图书 public int addBook(Book book); //修改图书 public int updateBookById(Book book); //删除图书 public int deleteBookById(Integer id); //查询图书所有数量 public int countBooks(); //查询图书通过id public Book queryBookById(Integer id); //查询图书列表 public List创建service实现类queryBookList(); }
package com.wt.service.impl; import com.wt.dao.BookDao; import com.wt.pojo.Book; import com.wt.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class BookServiceImpl implements BookService { @Autowired BookDao bookDao; @Override public int addBook(Book book) { return bookDao.addBook(book); } @Override public int updateBookById(Book book) { return bookDao.updateBookById(book); } @Override public int deleteBookById(Integer id) { return bookDao.deleteBookById(id); } @Override public int countBooks() { return bookDao.countBooks(); } @Override public Book queryBookById(Integer id) { return bookDao.queryBookById(id); } @Override public List创建测试类queryBookList() { return bookDao.queryBookList(); } }
在这里插入代码片package com.wt.test; import com.wt.pojo.Book; import com.wt.service.BookService; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class Mytest { @Test public void test1(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml"); BookService bookService = applicationContext.getBean("bookServiceImpl", BookService.class); //测试新增 Book book=new Book(); book.setBookAuthor("尚硅谷"); book.setBookName("数据结构与算法"); book.setBookPrice(98); bookService.addBook(book); //测试查询数据条数 int count = bookService.countBooks(); System.out.println("数据总数:"+count); //测试查询某条数据通过id Book book1 = bookService.queryBookById(2); System.out.println("查询结果是:"+book1); //测试查询所有数据 Listlist = bookService.queryBookList(); for(Book book2:list){ System.out.println(book2); } //修改某条数据 Book book3=new Book(); book3.setBookId(2); book3.setBookName(" *** 作系统实战"); book3.setBookPrice(99); book3.setBookAuthor("千峰教育集团"); int i = bookService.updateBookById(book3); System.out.println("修改成功与否"); //删除某条数据 int i1 = bookService.deleteBookById(2); System.out.println("删除成功与否"+i1); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)