使用JdbcTemplate遇到的问题

问题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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用JdbcTemplate时,报错"No DataSource set"通常是因为没有设置数据源。在使用JdbcTemplate之前,需要先配置并设置一个数据源。你可以在xml配置文件中创建一个DataSource对象,并将其注入到JdbcTemplate中。一种解决方法是加载xml配置文件,并通过代码块来创建JdbcTemplate对象。首先,你可以在UserDaoImpl类中将jdbcTemplate用final修饰,然后在代码块中读取xml配置文件来创建JdbcTemplate对象。具体的代码如下: ```java private final JdbcTemplate jdbcTemplate; { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class); } ``` 通过上述代码,你可以将xml配置文件中的数据源配置加载到JdbcTemplate对象中,从而解决"No DataSource set"的问题。请确保你的xml配置文件中已经正确配置了数据源的相关信息,并且文件名称和路径与代码中的一致。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JdbcTemplate基本使用](https://download.csdn.net/download/weixin_38606076/14939780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Spring5学习JdbcTemplate遇到问题](https://blog.csdn.net/xk1835217729/article/details/123315404)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值