Spring之JdbcTemplate基本使用(Curd )

Spring之jdbcTemplate配置环境

  • 1JdbcTemplate基本使用-开发步骤(理解)
  • 2.代码实现
    • 2.1 导入spring-jdbc和spring-tx坐标
    • 2.2 配置Spring容器创建JdbcTemplate对象(spring产生模板对象代码实现(抽取jdbc.properties))
    • 2.3 创建数据库表和实体
    • 2.4 编写测试代码
    • 2.5 测试结果
    • 2.6-JdbcTemplate基本使用-常用操作(应用)
  • 3 JdbcTemplate基本使用-知识要点(理解,记忆)

1JdbcTemplate基本使用-开发步骤(理解)

①导入spring-jdbc和spring-tx坐标

②创建数据库表和实体

③创建JdbcTemplate对象

④执行数据库操作

2.代码实现

2.1 导入spring-jdbc和spring-tx坐标


  <dependencies>
	  <dependency>
		  <groupId>org.springframework</groupId>
		  <artifactId>spring-context</artifactId>
		  <version>5.0.6.RELEASE</version>
	  </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.23</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.21</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
  </dependencies>

2.2 配置Spring容器创建JdbcTemplate对象(spring产生模板对象代码实现(抽取jdbc.properties))

jabc.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/li
jdbc.username=root
jdbc.password=517818li

AppliciationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation=
		       "http://www.springframework.org/schema/beans
		        http://www.springframework.org/schema/beans/spring-beans.xsd
				http://www.springframework.org/schema/aop
				http://www.springframework.org/schema/aop/spring-aop.xsd
				http://www.springframework.org/schema/context
				http://www.springframework.org/schema/context/spring-context.xsd">
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<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>
</beans>

2.3 创建数据库表和实体

Spring之JdbcTemplate基本使用(Curd )_第1张图片

package com.it.domain;

public class Acount{
    private int id;
    private int uid;
    private double money;

    @Override
    public String toString() {
        return "Acount{" +
                "id=" + id +
                ", uid=" + uid +
                ", money=" + money +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}

2.4 编写测试代码

    public void test1(){
        ApplicationContext app = new ClassPathXmlApplicationContext("AppliciationContext.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate)app.getBean("jdbcTemplate");
        int row = jdbcTemplate.update("insert into account values (?,?,?)",5,41,200);
        System.out.println(row);

    }

2.5 测试结果

Spring之JdbcTemplate基本使用(Curd )_第2张图片

2.6-JdbcTemplate基本使用-常用操作(应用)

package com.itheima.test;

import com.itheima.domain.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
	//修改更新
    @Test
    public void testUpdate(){
        jdbcTemplate.update("update account set money=? where name=?",10000,"tom");
    }
	//删除
    @Test
    public void testDelete(){
        jdbcTemplate.update("delete from account where name=?","tom");
    }
//聚合查询
    @Test
    public void testQueryCount(){
        Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println(count);
    }
	//查询一个
    @Test
    public void testQueryOne(){
        Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom");
        System.out.println(account);
    }
	//查询所有
    @Test
    public void testQueryAll(){
        List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(accountList);
    }
}

3 JdbcTemplate基本使用-知识要点(理解,记忆)

①导入spring-jdbc和spring-tx坐标

②创建数据库表和实体

③创建JdbcTemplate对象

		JdbcTemplate jdbcTemplate = newJdbcTemplate();
	       jdbcTemplate.setDataSource(dataSource);

④执行数据库操作

更新操作:

    jdbcTemplate.update (sql,params)

查询操作:

    jdbcTemplate.query (sql,Mapper,params)

jdbcTemplate.queryForObject(sql,Mapper,params)

你可能感兴趣的:(Spring)