一、简述
Spring对数据库的操作在jdbc上做了更深层次的封装,而JdbcTemplate便是Spring提供的一个操作数据库的便捷工具。通过借助JdbcTemplate来执行所有数据库操作,例如插入,更新,删除和从数据库中检索数据,并且有效避免直接使用jdbc带来的繁琐编码。
- JdbcTemplate主要提供以下五种类型的方法:
execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句。
update、batchUpdate方法:用于执行新增、修改、删除等语句。
query方法及queryForXXX方法:用于执行查询相关的语句。
call方法:用于执行数据库存储过程和函数相关的语句。
- 项目结构
二、项目部分
- SQL脚本内容
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `product`
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(11) DEFAULT NULL,
`price` float(4,1) DEFAULT NULL,
`num` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of product
-- ----------------------------
- pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
- 数据源配置application.properties
server.port=8084
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/products?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8
spring.datasource.username=root
#特别需要注意密码等号前面的
spring.datasource.password=root
- pojo类
public class Product {
private int id;
private String name;
private float price;
private int num;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Product() {
super();
}
public Product(String name, float price,
int num) {
super();
this.name = name;
this.price = price;
this.num = num;
}
}
- dao层,包含进行的操作
@Repository//一定要有这个才能运行
public class ProductDao {
@Autowired
private JdbcTemplate jdbcTemplate;//jdbc模版
public void addProduct(Product product){
int i =jdbcTemplate.update("INSERT INTO product(name,price,num)VALUES(?,?,?)",
new Object[]{product.getName(),product.getPrice(),product.getNum()} );
System.out.println(i);
}
}
- service层
@Service
public class ProductService {
@Autowired
private ProductDao dao;
public void save(Product product){
dao.addProduct(product);
}
}
- controller层
@RestController
public class ProductController {
@Autowired
private ProductService produrtService;
@RequestMapping("/product")
public String productTest(Product product)
{
Product p = new Product("月亮与六便士", 35, 10);
produrtService.save(p);
return "success";
}
}
- 启动类
@SpringBootApplication(scanBasePackages= {"com.templateJdbctemplateJdbc.Controller","com.templateJdbctemplateJdbc.dao","com.templateJdbctemplateJdbc.Service"})
public class ProductTest {
public static void main(String[] args) {
SpringApplication.run(ProductTest.class, args);
}
}
- 结果