温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot怎么使用JdbcTemplate操作数据库

发布时间:2022-04-07 14:13:12 来源:亿速云 阅读:161 作者:iii 栏目:编程语言

这篇文章主要介绍了SpringBoot怎么使用JdbcTemplate操作数据库的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot怎么使用JdbcTemplate操作数据库文章都会有所收获,下面我们一起来看看吧。

JdbcTemplate 是 Spring 提供的一套 JDBC 模版框架,利用 AOP 技术来解决直接使用 JDBC 时大量重复代码的问题。虽然没有 MyBatis 那么灵活,但是比直接使用 JDBC 要方便很多。

一、创建表

CREATE TABLE `t_demo` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(120) NOT NULL,
  `num` int(11) NOT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='demo表';

SpringBoot怎么使用JdbcTemplate操作数据库

二、添加依赖、配置

1、首先编辑 pom.xml 文件,添加相关依赖。

<!-- spring-jdbc -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
 
<!-- 数据库驱动依赖 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
 
<!-- 数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.9</version>
</dependency>

2、编写配置

spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://localhost:3306/PiaoDB?useUnicode=swater&characterEncoding=UTF-8
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name = com.mysql.jdbc.Driver

三、编写代码

1、编写实体类

@Data
@Accessors(chain = true)
public class Demo {

    private Integer id;

    private String name;

    private Integer num;

    private Date createTime;

}

2、编写Dao代码

@Repository
public class DemoDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 新增数据
    public int addDemo(Demo demo) {
        return jdbcTemplate.update("INSERT INTO t_demo(name, num) VALUE (?, ?)",
                demo.getName(), demo.getNum());
    }

    // 修改数据
    public int updateDemo(Demo demo) {
        return jdbcTemplate.update("UPDATE t_demo SET name=?, num=? WHERE id=?",
                demo.getName(), demo.getNum(), demo.getId());
    }

    // 删除数据
    public int deleteDemoById(Integer id) {
        return jdbcTemplate.update("DELETE FROM t_demo WHERE id=?", id);
    }

    // 获取单条数据
    public Demo getDemoById(Integer id) {
        return jdbcTemplate.queryForObject("SELECT * FROM t_demo WHERE id=?",
                new BeanPropertyRowMapper<>(Demo.class), id);
    }

    // 获取多条数据
    public List<Demo> getAllDemos() {
        return jdbcTemplate.query("SELECT * FROM t_demo",
                new BeanPropertyRowMapper<>(Demo.class));
    }

}

3、编写Controller代码

@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private DemoDao demoDao;

    @RequestMapping("")
    public void test(){
        // 新增数据
        int num = demoDao.addDemo(new Demo().setName("piao").setNum(20));
        System.out.println("插入一条数据:" + num);

        // 修改数据
        int num2 = demoDao.updateDemo(new Demo().setId(15).setName("piao").setNum(22));
        System.out.println("更新一条数据:" + num2);

        // 删除数据
        int num3 = demoDao.deleteDemoById(13);
        System.out.println("删除一条数据:" + num3);

        // 查询单条数据
        Demo demo = demoDao.getDemoById(15);
        System.out.println("查询1条数据:" + demo.toString());

        // 查询多条数据
        List<Demo> demos = demoDao.getAllDemos();
        System.out.println("查询多条数据:" + demos);
    }

}

四、验证结果

SpringBoot怎么使用JdbcTemplate操作数据库

关于“SpringBoot怎么使用JdbcTemplate操作数据库”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“SpringBoot怎么使用JdbcTemplate操作数据库”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI