Book.java
package com.jdbc;
public class Book {
private String id;
private String name;
private String author;
private Double price;
private String press;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", price=" + price + ", press=" + press
+ "]";
}
}
BookDao.java
package com.jdbc;
import java.util.List;
public interface BookDao {
/*update() 方法 */
// 向数据库中增加图书
public int addBook(Book b);
// 更新数据库中的图书
public int updateBook(Book b);
// 删除数据库中的图书
public int deleteBook (int id);
/*query() 方法 */
// 通过 id 查询图书信息
public Book findBookById(int id);
// 查询所有图书信息
public List<Book> findAllBook();
}
BookDaoImpl.java
package com.jdbc;
import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
public class BookDaoImpl implements BookDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
// 增加图书
@Override
public int addBook(Book b) {
// 定义SQL
String sql = "insert into book(id, name, author, price, press) value(?, ?, ?, ?, ?)";
// 定义数组来存储SQL语句中的参数
Object obj[] = new Object[]{
b.getId(),
b.getName(),
b.getAuthor(),
b.getPrice(),
b.getPress()
};
// 执行添加操作,返回的是受SQL语句影响的记录条数
int num = this.jdbcTemplate.update(sql, obj);
return num;
}
// 更新图书
@Override
public int updateBook(Book b) {
// 定义SQL;
String sql = "update book set name = ?, author = ?,price = ?, press = ? where id = ?";
// 定义数组俩存储SQL语句中的参数
Object params[] = new Object[]{
b.getName(),
b.getAuthor(),
b.getPrice(),
b.getPress(),
b.getId()
};
// 执行更新操作,返回的受SQL语句影响的记录条数
int num = this.jdbcTemplate.update(sql, params);
return num;
}
// 删除图书
@Override
public int deleteBook(int id) {
// 定义SQL
String sql = "delete from book where id = ?";
// 执行删除操作,返回的是受SQL语句影响的记录条数
int num = this.jdbcTemplate.update(sql,id);
return num;
}
/*query()方法 */
// 通过id查询图书信息
@Override
public Book findBookById(int id) {
// 定义SQL语句
String sql = "select * from book where id = ?";
// 创建一个新的 BeanPropertyRowMapper 对象
RowMapper<Book> rowMapper =
new BeanPropertyRowMapper<Book>(Book.class);
// 将 id 绑定到 SQL 语句中,并通过 RowMapper 返回一个Object类型的单行记录
return this.jdbcTemplate.queryForObject(sql, rowMapper, id);
}
// 查询所有图书信息
@Override
public List<Book> findAllBook() {
// 定义 SQL 语句
String sql = "select * from book";
// 创建一个新的 BeanPropertyRowMapper 对象
RowMapper<Book> rowMapper =
new BeanPropertyRowMapper<Book>(Book.class);
// 执行静态的 SQL 查询,并通过 RowMapper 返回结果
return this.jdbcTemplate.query(sql, rowMapper);
}
}
BookTest.java
package com.jdbc;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class BookTest {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("abc.xml");
JdbcTemplate jdTemplate =
(JdbcTemplate) applicationContext.getBean("jdbcTemplate");
jdTemplate.execute("create table book(" +
"id int primary key auto_increment not null," +
"name varchar(50) ," +
"author varchar(20) ," +
"price float(10) ," +
"press varchar(30) )" );
System.out.println("图书管理数据库book创建成功!");
}
/*插入*/
@Test
public void add_Book(){
// 加载配置文件
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("abc.xml");
// 获取 BookDao 实例
BookDao bookDao =
(BookDao) applicationContext.getBean("bookDao");
// 创建 Book 对象,并向 Account 对象中添加数据
Book book = new Book();
book.setId("042712");
book.setName("aichijimoren");
book.setAuthor("jushi");
book.setPrice(688.00);
book.setPress("yuanchubanshe");
// 执行 addBook()方法,并获取返回结果
int num = bookDao.addBook(book);
if(num > 0){
System.out.println("成功插入了" + num + "条数据!");
}
else{
System.out.println("插入操作执行失败!");
}
}
/*更新*/
@Test
public void update_Book(){
// 加载配置文件
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("abc.xml");
// 获取 BookDao 实例
BookDao bookDao =
(BookDao) applicationContext.getBean("bookDao");
// 创建 Book 对象,并向 Book 对象中添加数据
Book book = new Book();
book.setId("042709");
book.setName("tuheise");
book.setAuthor("shui0nan");
book.setPrice(68.00);
book.setPress("258chubanshe");
// 执行 updateBook()方法,并返回结果
int num = bookDao.updateBook(book);
if(num > 0){
System.out.println("成功修改了" + num + "条数据!");
}
else{
System.out.println("修改操作执行失败!");
}
}
/*删除*/
@Test
public void delete_Book(){
// 加载配置文件
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("abc.xml");
// 获取 BookDao 实例
BookDao bookDao =
(BookDao) applicationContext.getBean("bookDao");
// 执行 deleteBook()方法,并获取返回结果
int num = bookDao.deleteBook(42711);
if(num > 0){
System.out.println("成功删除了" + num + "条数据!");
}
else{
System.out.println("删除操作执行失败!");
}
}
/*使用query()方法进行条件查询 */
@Test
public void find_BookById(){
// 加载配置文件
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("abc.xml");
// 获取 BookDao 实例
BookDao bookDao =
(BookDao) applicationContext.getBean("bookDao");
// 执行 findBookById()方法
Book book = bookDao.findBookById(42705);
System.out.println("id name author price press");
System.out.println(book.getId() + " " +book.getName() + " " + book.getAuthor() +
" " + book.getPrice() + " " + book.getPress());
}
/*使用query()方法查询所有图书信息 */
@Test
public void find_AllBook(){
// 加载配置文件
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("abc.xml");
// 获取 BookDao 实例
BookDao bookDao =
(BookDao) applicationContext.getBean("bookDao");
// 执行 findAllAccountById()方法,获取Book对象集合
List<Book> book = bookDao.findAllBook();
// 循环输出集合中的对象
System.out.println("id name author price press");
for(Book b : book){
System.out.println(b.getId() + " " + b.getName() + " " + b.getAuthor() + " " +
b.getPrice() + " " + b.getPress());
}
}
}
abc.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- l 配置数据源 -->
<bean id="dataSource" class=
"org.springframework.jdbc.datasource.DriverManagerDataSource" >
<!-- 数据库驱动 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- 连接数据库的 url -->
<property name="url" value="jdbc:mysql://localhost/bookdata" />
<!-- 连接数据库的用户名 -->
<property name="username" value="root" />
<!-- 连接数据库的密码 -->
<property name="password" value="root" />
</bean>
<!-- 2 配置 JDBC 模板 -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 默认必须使用数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 定义id为bookDao的Bean -->
<bean id="bookDao" class="com.jdbc.BookDaoImpl">
<!-- 将 jdbcTemplate 注入到 accountDao 实例中 -->
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
</beans>
截图:(2张)