spring 数据库 图书表
Book.java
package com.yan.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;
}
public String toString() {
return "Bookdata [id=" + id + "," + "name=" + name + ", author" + author + ", price" + price + ", press" + press + "]";
}
}
Bookdao.java
package com.yan.jdbc;
public interface Bookdao {
public int addBook(Book b);
}
Bookdaoimp.java
package com.yan.jdbc;
import org.springframework.jdbc.core.JdbcTemplate;
public class Bookdaoimp implements Bookdao{
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int addBook(Book b) {
String sql = "insert into book(name,author,price,press) value(?,?,?,?)";
Object[] obj = new Object[] {
b.getName(),
b.getAuthor(),
b.getPrice(),
b.getPress()
};
int num = this.jdbcTemplate.update(sql,obj);
return num;
}
public int updateBook(Book book) {
// TODO Auto-generated method stub
return 0;
}
public int deleteBook(int id) {
// TODO Auto-generated method stub
return 0;
}
}
JdbcTemplateTest.java
package com.yan.jdbc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateTest {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.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 addBookTest() {
@SuppressWarnings("resource")
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
Bookdao bookDao =
(Bookdao) applicationContext.getBean("book");
Book book = new Book();
book.setName("buai");
book.setAuthor("aibuwan");
book.setPrice( (double)9);
book.setPress("686chubanshe");
int num = bookDao.addBook(book);
if(num > 0) {
System.out.println("成功插入了" + num + "条数据");
} else {
System.out.println("插入操作执行失败");
}
}
}
applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="dataSource" class=
"org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/bookdata" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="book" class="com.yan.jdbc.Bookdaoimp">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
</beans>
截图: (2张)