spring中jdbcTemplate的使用

首页 / 🍁编程类 / 正文

spring中需要的配置文件
springJDBC.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: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/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入jdbc.properties文件 -->
    <context:property-placeholder location="jdbc.properties" />

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>


    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

mysql配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/****?serverTimezone=UTC
jdbc.username=root
jdbc.password=****

maven中依赖

<dependencies>
        <!-- Spring核心依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.3.1</version>
        </dependency>

        <!-- Spring dao依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <!-- Spring web依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <!-- Spring test依赖:方便做单元测试和集成测试 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.3.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.11</version>
        </dependency>

    </dependencies>

user类

public class User {

    int id;
    String username;
    String password;
    String gender;
    String addr;

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public User() {

    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender='" + gender + '\'' +
                ", addr='" + addr + '\'' +
                '}';
    }
}

测试类

import java.util.List;
//指定当前测试类在spring的测试环境执行,此时就可以通过注入的方式直接获取ioc容器
@RunWith(SpringJUnit4ClassRunner.class)
//设置spring测试环境的配置文件
@ContextConfiguration("classpath:SpringJDBC.xml")
public class TestClass {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testinsert() {
        String sql = "insert into tb_user values(?,?,?,?,?)";
        jdbcTemplate.update(sql, 7, "cehsi", "ceshi", "男", "陕西");

    }
    //public static void main(String[] args) {
        //ApplicationContext ioc=new ClassPathXmlApplicationContext("SpringJDBC.xml");
        //这种直接把<bean标签里面的id值拿出来
        //Student student = (Student) ioc.getBean("studentone");
        //JdbcTemplate jdbcTemplate=ioc.getBean(JdbcTemplate.class);
        //String sql="insert into tb_user values(?,?,?,?,?)";
        //jdbcTemplate.update(sql,6,"cehsi","ceshi","男","陕西");

    //}

    @Test
    public void testselect(){
        String sql="select * from tb_user where id=?";
        User user=jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),5);
        System.out.println(user);
    }

    @Test
    public void testselectALL(){
        String sql="select * from tb_user";
        List<User> user= jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class));
        System.out.println(user);
    }
    
}

遇见的问题

  1. 视频中老师讲解是用注入的方式写的
  2. 运用的@RunWith和@ContextConfiguration和@Autowired注解
  3. public static void main(String[] args)这个方法里面我中配置文件形式跑;了下,也可以正常运行
  4. 重点遇见问题!!!!下面这个
  5. new BeanPropertyRowMapper<>(User.class) 报错,错误信息为Diamond types are not supported at language level '5'
  6. 解决办法https://blog.csdn.net/dongzhensong/article/details/88799477
评论区
头像