Spring JdbcTemplate基本使用

1.开发步骤

①导入spring-jdbc和spring-tx依赖
②创建数据库表和实体
③创建JdbcTemplate对象
④执行数据库操作

2.导入依赖

//引入spring-jdbc、spring-tx依赖
implementation 'org.springframework:spring-jdbc:5.3.14'
implementation 'org.springframework:spring-tx:5.3.14'

3.创建数据库表和实体

在这里插入图片描述

data class Account(var name:String? = null, var money: Double = 0.0)

4.创建JdbcTemplate对象

	@Test
    // 测试JdbcTemplate开发步骤
    fun test1(){
        // 设置数据源对象
        val app = ClassPathXmlApplicationContext("applicationContext.xml")
        val dataSource = app.getBean("dataSource") as DruidDataSource

        val jdbcTemplate = JdbcTemplate()
        // 设置数据源对象 连接数据库
        jdbcTemplate.dataSource = dataSource
        // 执行数据
        val row = jdbcTemplate.update("insert into account values (?,?)", "zhangsan", 123.2)
        println(row)
    }

使用模板创建

	<!-- 自动配置数据源  -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- JdbcTemplate  -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
	@Test
    fun test2(){
        // 设置数据源对象
        val app = ClassPathXmlApplicationContext("applicationContext.xml")
        val jdbcTemplate = app.getBean("jdbcTemplate") as JdbcTemplate

        // 执行数据
        val row = jdbcTemplate.update("insert into account values (?,?)", "lisi", 200)
        println(row)
    }

5.增删改查

	@Test
    fun test2(){
        // 设置数据源对象
        val app = ClassPathXmlApplicationContext("applicationContext.xml")
        val jdbcTemplate = app.getBean("jdbcTemplate") as JdbcTemplate

        // 执行数据
        val row = jdbcTemplate.update("insert into account values (?,?)", "lisi", 200)
        println(row)
    }

	@Test
    fun test3(){
        val app = ClassPathXmlApplicationContext("applicationContext.xml")
        val jdbcTemplate = app.getBean("jdbcTemplate") as JdbcTemplate

        // 删除数据
        val row = jdbcTemplate.update("delete from account where name=?","wangwu")
        println(row)
    }

	@Test
    fun test4(){
        val app = ClassPathXmlApplicationContext("applicationContext.xml")
        val jdbcTemplate = app.getBean("jdbcTemplate") as JdbcTemplate

        // 修改数据
        val row = jdbcTemplate.update("update account set money=? where name=?", 10000, "lisi")
        println(row)
    }

查(全部)

	@Test
    fun test5() {
        val app = ClassPathXmlApplicationContext("applicationContext.xml")
        val jdbcTemplate = app.getBean("jdbcTemplate") as JdbcTemplate

        // 查询全部
        val query:List<Account> = jdbcTemplate.query("select * from account", BeanPropertyRowMapper<Account>(Account::class.java))
        
        println(query)
        // 输出:[Account(name=zhangsan, money=123.2), Account(name=lisi, money=10000.0)]

    }

查(单条)

	@Test
    fun test6() {
        val app = ClassPathXmlApplicationContext("applicationContext.xml")
        val jdbcTemplate = app.getBean("jdbcTemplate") as JdbcTemplate

        // 查询单条
        val query:Account = jdbcTemplate.queryForObject("select * from account where name=?",BeanPropertyRowMapper<Account>(Account::class.java),"lisi")

        println(query)
        // 输出:Account(name=lisi, money=10000.0)


    }

聚合查询

	@Test
    fun test7() {
        val app = ClassPathXmlApplicationContext("applicationContext.xml")
        val jdbcTemplate = app.getBean("jdbcTemplate") as JdbcTemplate

        // 聚合查询
        val query:Long = jdbcTemplate.queryForObject("select count(*) from account ", Long::class.java)

        println(query)
        // 输出:2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值