qq_51972040 2024-04-05 17:46 采纳率: 0%
浏览 5

spring,junit5中,使用JdbcTemplate添加数据报错

package com.clq.spring.junit;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringJUnitConfig(locations = "classpath:bean.xml")
public class JdbcTemplateTest {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Test
    public void testUpdate() {
        String sql = "INSERT INTO t_emp VALUES(NULL,?,?,?)";
        int rows = jdbcTemplate.update(sql,"林平之", 20, "未知");
        System.out.println(rows);
    }
}
<?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">
    <!--引入外部属性文件创建数据源的对象-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="driver" value="${jdbc.driver}"/>
        <property name="name" value="${jdbc.name}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置 JdbcTemplate    -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
        <property name="dataSource" ref="druidDataSource"/>
    </bean>

</beans>


连接数据库的文件
jdbc.name=root
jdbc.password=3333
jdbc.url=jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver
报错:
java.lang.IllegalStateException: Failed to load ApplicationContext for [MergedContextConfiguration@78d39a69 testClass = com.clq.spring.junit.JdbcTemplateTest, locations = ["classpath:bean.xml"], classes = [], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = [], contextCustomizers = [], contextLoader = org.springframework.test.context.support.DelegatingSmartContextLoader, parent = null]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'druidDataSource' defined in class path resource [bean.xml]: Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Driver' for property 'driver'; Cannot convert value of type 'java.lang.String' to required type 'java.sql.Driver' for property 'driver': no matching editors or conversion strategy found

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-05 17:46
    关注

    引自免费微信小程序:皆我百晓生

    首先,你需要确保在Spring项目的pom.xml文件中已经包含了DruidDataSource这个依赖。如果你还没有,可以添加到你的pom.xml文件中的 <dependency> 标签中。

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

    然后,你需要创建一个名为 data-source.xml 的文件,并在其中配置你的DruidDataSource信息。例如:

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

    接下来,你需要配置JdbcTemplate对象,将数据源设置为你的DruidDataSource。在这个例子中,我们假设DruidDataSource的名字是testData-source,那么你应该修改你的代码为:

    @Autowired
    private JdbcTemplate jdbcTemplate;
    

    在你的测试方法中,你需要使用JdbcTemplate的方法来插入数据,例如:

    public void testUpdate() {
        String sql = "INSERT INTO t_emp VALUES(null,?,?,?)";
        int rows = jdbcTemplate.update(sql,"林平之", 20, "未知");
        System.out.println(rows);
    }
    

    在这个例子中,sql是一个插入语句,null代表空值,20代表记录编号,未知代表记录内容。你需要根据你的实际需求来填充这些字段。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月5日

悬赏问题

  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大
  • ¥15 使用spss做psm(倾向性评分匹配)遇到问题