SpringBoot-通过JdbcTemplates访问MySQL

数据库准备

开门见山吧,首先我们在MySQL中创建数据库:

CREATE DATABASE IF NOT EXISTS springboot_project DEFAULT CHARSET utf8;

然后在springboot_project数据库中创建temp表:

CREATE TABLE `temp` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) DEFAULT NULL,
  `code` VARCHAR(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

工程引入jar依赖

首先我们在pom.xml中引入jdbc的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

然后引入mysql连接和druid连接池:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.29</version>
</dependency>

当然我们最后需要Controller层做个简单的测试,所以把web依赖也引入进来:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

配置相关文件

在application-dev.properties文件配置mysql的驱动类、数据库连接地址、账号和密码:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_project
spring.datasource.username=root
spring.datasource.password=Anbang713

编码实现

1、实体类

public class Temp {

  private Integer id;
  private String name;
  private String code;

  // 这里省略getter和setter
}

2、定义持久层接口

public interface TempDao {

  int save(Temp entity) throws Exception;

  Temp get(Integer id);
}

3、持久层接口实现类

@Repository
public class TempDaoImpl implements TempDao {

  @Autowired
  private JdbcTemplate jdbcTemplate;

  @Override
  public int save(Temp entity) throws Exception {
    if (entity.getId() != null) {
      return jdbcTemplate.update("update temp set name = ?, code = ? where id = ?",
          entity.getName(), entity.getCode(), entity.getId());
    }
    return jdbcTemplate.update("insert into temp(name, code) values(?,?)", entity.getName(),
        entity.getCode());
  }

  @Override
  public Temp get(Integer id) {
    List<Temp> result = jdbcTemplate.query("select * from temp where id = ?", new Object[] {
        id }, new BeanPropertyRowMapper(Temp.class));
    if (result == null || result.isEmpty()) {
      return null;
    }
    return result.get(0);
  }

}

4、定义业务层接口

public interface TempService {
  int save(Temp entity) throws Exception;

  Temp get(Integer id);
}

5、业务层接口实现类

@Service
public class TempServiceImpl implements TempService {

  @Autowired
  private TempDao tempDao;

  @Override
  public int save(Temp entity) throws Exception {
    return tempDao.save(entity);
  }

  @Override
  public Temp get(Integer id) {
    return tempDao.get(id);
  }

}

6、写一个Controller

@RestController
@RequestMapping("/temp/*")
public class TempController {

  @Autowired
  private TempService tempService;

  @RequestMapping(value = "{id}", method = RequestMethod.GET)
  public Temp get(@PathVariable("id") Integer id) {
    return tempService.get(id);
  }

  @RequestMapping(value = "save", method = RequestMethod.POST)
  public Integer save(@RequestBody Temp entity) throws Exception {
    return tempService.save(entity);
  }
}

至此,我们所有的编码都已经实现了,我们的整个代码结构看起来是这样的:

7、测试

 首先我们通过save接口新建一条数据:

然后我们通过get接口取出id为1的数据: 

我们再通过save接口修改id为1的name字段,将zhangsan修改为lisi:

源代码地址:https://gitee.com/chengab/SpringBoot/tree/master/springboot/src/main/java/com/study/springboot/jdbctemplates

参考博客:SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值