使用JdbcTemplate遇到的问题

博客围绕Spring和MyBatis开发展开,介绍了将数据库配置文件单独提取配置的方法,对比了ClassPathXmlApplicationContext和AnnotationConfigApplicationContext的使用场景,还阐述了所学注解的作用。同时,详细列举了开发中遇到的多个问题,如找不到bean、注解无法导入等,并给出相应解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题1:明明设置了这个名字的bean类,但是就是找不到

解决:可能是在使用注解配置的时候,生成的不是想要的 例如: configerable 和 configertion

//配置注解的格式以及扫描注解的格式很重要
//设置当前为配置类
//@Configurable //用这个注解的话,在扫描的时候不需要{}
@Configuration  //使用这个注解的话,扫描注解需要有{}
//开启注解扫描
@ComponentScan("com.by")

方法二:实现层(Impl)没有设置repository 或者是component的注解

@Component("UserDao")
//@Repository
//要一个就行
public class UserDaoImpl implements UserDao {

方法三: 可能是:在写这里的时候写错了

  ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
  //  ApplicationContext ac  = new AnnotationConfigApplicationContext("application.xm;");

方法四:

用方法类的话,写成.class类型的,不能直接使用接口,使用接口的实现类

    @Test
    public void test01(){
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
       
       OrderService orderService = ac.getBean(OrderService.class);
       orderService.save02();
        OrderDao orderDao = (OrderDao) ac.getBean("orderDaoImpl");
        orderDao.save01();

    }

方法四:使用纯注解开发模式的时候,扫描配置文件的时候,加上了双引号

//错误格式  
// ApplicationContext ac = new AnnotationConfigApplicationContext("OrderConfig.class");
ApplicationContext ac = new AnnotationConfigApplicationContext("OrderConfig.class");

问题2 : @PostContract 和 @PreDestroy 无法导入的原因是 :没有将javax.connection的架包导进来

解决方法:在xml的配置文件中添加导包

  <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>

问题三: jdbcTemplate的使用,在进行测试的时候,测试名字不能和包名一样。

解决方法:

会出现系统无法识别是包名还是类名,将测试的类名改了就可以了

还有一种找错的方法,看导入的包的种类,如果没有使用想要的包名,肯定是在本类里边关于导包的地方出现错误

问题四: 无法加载应用程序上下文

解决方法:

问题五:java.lang.IllegalStateException: Mapped class was not specified

Java . lang . illegalstateexception:未指定映射类

解决方法:添加实体类即可

问题六:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.by.service.AccountService' available

org . spring framework . beans . factory . nosuchbean definitionexception:没有“com.by.service.AccountService”类型的合格bean可用

解决方法:在是实现接口的时候,没有给名字

@Service("AccountService")//加上这个就可以了
public class AccountServiceImpl  implements AccountService {

问题七:Parameter index out of range (2 > number of parameters, which is 1).

参数越界问题,根据id删除,只需要写出所需要的参数,例如当数据库中还有name时,不需要回去name的值

 public void delete(Account account) {
        String sql ="delete  from account where  id = ?";
        Object [] acc ={account.getId()};
//错误写法
//Object [] acc ={account.getId(),account.getname};
        int delete = jdbcTemplate.update(sql, acc);
        System.out.println(delete);
ApplicationContext ac= new ClassPathXmlApplicationContext("application.xml");
        AccountService accountService = ac.getBean(AccountService.class);
        Account account = new Account();
        account.setId(1);
        accountService.delete(account);

将数据库的配置文件单独提取出来一个文件在配置

在application.xml的文件中,增加content的配置

<?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">

相对于上边的文件,只是更改了红色方框里的内容

ClassPathXmlApplicationContext 和 AnnotationConfigApplicationContext 的区别使用

当我们使用注解配置时,就不能再用之前的ClassPathXmlApplicationContext()构造方法创建ioc容器了,而是要使用AnnotationConfigApplicationContext()方法创建ioc容器

当使用xml的文件的时候,使用ClassPathXmlApplicationContext()构造方法去建立ioc容器;

当使用注解的时候,使用AnnotationConfigApplicationContext()方法建立ioc容器

现在所学的注解的作用:

@PostContract 和 @PreDestroy 需要配置导包才能使用

@Component("UserDao")和@Repository :自动注入所需要的属性

@Autowired:自动装配

@service :这个的后边建议加上名字

@Repose

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值