Spring Boot中的JdbcTemplate与MySQL集成

1、创建一个Spring Boot项目。

2、配置项目的依赖,包括Spring Boot和MySQL连接驱动(在pom.xml中)

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

3、使用JdbcTemplate创建一个MySQL数据库,并设计一个适当的数据表

-创建数据库

drop table if exists carbrand;
create table carbrand(
                         id int(12) auto_increment primary key comment '主键' ,
                         name varchar(100) comment'姓名',
                         country varchar(12) comment'国家'
)engine=InnoDB;

-创建表

insert into carbrand(name,country) values ('五菱','中国');

4、编写Spring Boot的配置文件,配置JdbcTemplate与MySQL的连接信息

5、在项目中创建一个DAO类,使用JdbcTemplate完成对数据库的增删改查操作

-创建DAO类

JdbcTemplate template;
@Autowired
public CarBrandDao(JdbcTemplate template){
    this.template=template;
}
public List<CarBrand> getAll(){
    String sql="select * from carbrand";
    return template.query(sql,(rs, rowNum) ->{
       CarBrand entity=new CarBrand();
       entity.setId(rs.getInt("id"));
       entity.setName(rs.getString("name"));
       entity.setCountry(rs.getString("country"));
       return entity;

    } );
}

增:

public int add(CarBrand entity){
    String sql="insert into carbrand(name,country) values(?,?)";
    return template.update(sql,entity.getName(),entity.getCountry());
}

删:

public int delete(Integer id){
    String sql="delete from carbrand where id=?";
    return template.update(sql,id);
}

改:

public int update(CarBrand entity){
    String sql="update carbrand set name=?,country=? where id=?";
    return template.update(sql,entity.getName(),entity.getCountry(),entity.getId());
}

查:

public CarBrand getById(Integer id){
    String sql="select * from carbrand where id=?";
    return template.queryForObject(sql,new Object[]{id},new BeanPropertyRowMapper<>(CarBrand.class));
}

6、在项目中创建一个Controller类,提供相应的接口访问数据库操作

7、实现以下功能:

(1)插入一条数据到数据库

@RequestMapping("/insert")
public String insert() {
    CarBrand entity=new CarBrand();
    entity.setName("大众");
    entity.setCountry("德国");
    return dao.add(entity)>0?"新增数据成功":"Fail";
}

(2)查询数据库中的所有数据

CarBrandDao dao;
@Autowired
public CarBrandController(CarBrandDao dao) {
    this.dao = dao;
}
@RequestMapping("/list")
public List<CarBrand> getAll() {

    return dao.getAll();
}

新增修改删除前的所有数据:

新增删除修改后的所有数据

(3)根据条件查询数据库中的数据

@RequestMapping("/getById/{userId}")
public CarBrand findById(@PathVariable("userId") Integer id) {
    return dao.getById(id);
}

-查询id为2的数据:

(4)更新数据库中的数据

@RequestMapping("/edit")
public String edit() {
    CarBrand entity=new CarBrand();
    entity.setName("比亚迪");
    entity.setCountry("中国");
    return dao.add(entity)>0?"修改数据成功":"Fail";
}

(5)删除数据库中的数据

@RequestMapping("/delete/{userId}")
public String delete(@PathVariable("userId") Integer id){
    return dao.delete(id)>0?"删除数据成功":"找不到该数据";
}

-删除id为1的数据

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值