Sprig Boot中的JdbcTemplate

   JdbcTemplate是Spring对JDBC的封装,目的是使JDBC更加易于使用。JdbcTemplate是Spring的一部分,可处理资源的建立和释放,帮助我们避免一些常见的错误,比如忘记关闭连接。它运行核心的JDBC工作流,如Statement的建立和执行,而我们只需要提供SQL语句和提取结果。
   在JdbcTemplate中执行SQL语句的方法大致分为以下3类。
   void execute (String sql):可以执行所有SQL语句,一般用于执DDL (Data Definition Language,数据定义语言)语句。
   int update ( String sql,Object...args):用于执行INSERT、UPDATE、DELETE等DML(Data Manipulation Language,数据操纵语言)语句。其中参数sql表示SQL语句,允许有“?”占位符;参数args表示一个可变参数,表示用来替换“?”占位符的实际参数。
  queryXxx:用于执行DQL (Data Query Language,数据查询语言)语句,主要有TqueryForObject (String sql, Class requiredType, Object..args)和List query (String sql, RowMapper, Object...args)这两个方法。

创建Spring Boot项目,在pom.xml文件中引入如下配置:

<!--MySQL依赖-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!--JDBC依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

在application.yml配置文件中配置数据源信息:

spring:
datasource:
url: jdbc:mysql://localhost:3306/DataApplication?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver

sql:
init:
schema-locations: classpath:db/schema.sql
mode: always
continue-on-error: true
data-locations: classpath:db/data.sql

创建实体类Customer

创建Dao层接口与实现类:

接口CustomerDao

实现类CustomerDaoImal

package dao;

import entity.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;


import java.util.List;

@Repository
public class CustomerDaoImpl implements CustomerDao{
@Autowired
private JdbcTemplate jdbcTemplate;
//添加客户信息
public int saveCustomer(Customer customer){
String sql="insert into tb_customer(jobNo,name,departMeant) values(?,?,?)";
int result=jdbcTemplate.update(sql,new Object[]
{customer.getJobNo(),customer.getName(),customer.getDepartment()});
return result;
}
//获取所有客户信息
public List<Customer> getAllCustomer(){
String sql="select *from tb_customer";
return jdbcTemplate.query(sql,
new BeanPropertyRowMapper<>(Customer.class));
}
//根据id查询客户信息
public Customer getCustomerById(Integer id){
String sql="select *from tb_customer where id=?";
return jdbcTemplate.queryForObject(sql,
new BeanPropertyRowMapper<>(Customer.class),id);
}
//修改客户信息
public int updateCustomer(Customer customer){
String sql="update tb_customer set jobNo=?,name=?,departMeant=? where id=?";
return jdbcTemplate.update(sql,customer.getJobNo(),customer.getName(),customer.getDepartment(),
customer.getId());
}
//删除客户信息
public int deleteCustomer(Integer id){
String sql="delete from tb_customer where id=?";
return jdbcTemplate.update(sql,id);
}
}

创建服务层接口与实现类

服务层接口CustomerService

创建服务层实现类CustomerServiceImpl

创建控制类CustomerController

import entity.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import service.CustomerService;

@RequestMapping("/user")
@RestController
public class CustomerController {
// @Autowired
CustomerService customerService;

//添加客户信息
@GetMapping("/save")
public String saveCustomer() {
Customer customer = new Customer();
customer.setJobNo("J00353");
customer.setName("张三峰");
customer.setDepartment("软件学院");
int result=customerService.saveCustomer(customer);
if(result>0){
return "添加客户成功! ";
}else{
return "添加客户失败! ";
}
}
//获取客户信息
@GetMapping("/getAll")
public String getAllCustomer(){
return customerService.getAllCustomer().toString();
}
//根据id查询客户信息
@GetMapping("/getCustomer/{id}")
public Customer getCustomerById(@PathVariable Integer id){
return customerService.getCustomerById(id);
}
//修改客户信息
@GetMapping("/update")
public String updateCustomer(Customer customer){
customer.setId(9);;
customer.setName("孙贺祥");
customer.setJobNo("J00110");
customer.setDepartment("软件学院");
int result=customerService.updateCustomer(customer);
if(result>0){
return "修改客户信息成功! ";
}else{
return "修改客户信息失败! ";
}
}
//删除客户信息
@GetMapping("/delete/{id}")
public String deleteCustomer(@PathVariable Integer id){
int result=customerService.deleteCustomer(id);
if(result>0){
return "删除客户信息成功! ";
}else{
return "删除客户信息失败! ";
}
}
}

与数据库的连接

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值