spring boot——templateJdbc整合

一、简述
Spring对数据库的操作在jdbc上做了更深层次的封装,而JdbcTemplate便是Spring提供的一个操作数据库的便捷工具。通过借助JdbcTemplate来执行所有数据库操作,例如插入,更新,删除和从数据库中检索数据,并且有效避免直接使用jdbc带来的繁琐编码。

  1. JdbcTemplate主要提供以下五种类型的方法:
execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句。
update、batchUpdate方法:用于执行新增、修改、删除等语句。
query方法及queryForXXX方法:用于执行查询相关的语句。
call方法:用于执行数据库存储过程和函数相关的语句。
  1. 项目结构

在这里插入图片描述
二、项目部分

  1. 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
-- ----------------------------
  1. 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>
  1. 数据源配置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
  1. 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;
 }
}
  1. 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);
 }
}
  1. service层
@Service
public class ProductService {
 @Autowired
    private ProductDao dao;
    public void save(Product product){
     dao.addProduct(product);
    }
}
  1. 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";
  }
}
  1. 启动类
@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);
 }
}
  1. 结果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值