SpringBoot中JDBCTemplate的基本使用

SpringBoot中JDBCTemplate的基本使用

在springboot中如果不自己引入数据源,默认会使用HikariDataSource 数据源,以下就以默认数据源为例

  • 准备工作
  1. 在pom.xml中导入两个需要使用的相关依赖
<!--        引入spring-boot-starter-jdbc的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
<!--        引入mysql连接池-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
  1. 在resource目录下的配置文件application.xml里配置数据源

    #注册驱动
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    #指定url,xxx是数据库的名字,serverTizone=UTF解决时区问题,&useUnicode=true&characterEncoding=utf-8指定字符的编码集
    spring.datasource.url=jdbc:mysql://localhost:3306/xxx?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    #数据库的用户名
    spring.datasource.username=root
    #密码
    spring.datasource.password=123456
    

    当然也可以使用xxx.yml配置文件,只需改变格式即可

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

    准备工作完成,开始编写代码

  • 测试

    编写一个Controller,注入 jdbcTemplate,编写测试方法进行访问测试;

    import org.springframework.jdbc.core.JdbcTemplate;
    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 java.util.List;
    import java.util.Map;
    
    @RestController
    @RequestMapping("/jdbc")
    public class TestController {
    
        @Autowired//由于使用了默认的数据源,jdbcTemplate会自动装配数据源,使用起来也不用再自己来关闭数据库连接
        JdbcTemplate jdbcTemplate;
    
    
        @GetMapping("/list")//查询数据表中的全部数据
        public List<Map<String, Object>> userList(){
            String sql = "select * from xxx";//xxx为数据表的名字
            List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
            return maps;
        }
    
        //新增一个用户
        @GetMapping("/add")
        public String addUser(){
            //插入语句xxx为表名...为数据表的各项属性,中间以逗号隔开
            String sql = "insert into xxx(...) values (,,,)";//,,,为将要插入的值
            jdbcTemplate.update(sql);
            //查询
            return "Success";
        }
        
    }
    

    常用的jdbcTemplate方法

    • execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;
    • update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;
    • query方法及queryForXXX方法:用于执行查询相关语句;

    简单的增删改查就完成了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值