Spring  applicationconfig.xml如下

    1. <?xml version="1.0" encoding="UTF-8"?>
    1. <beans xmlns="http://www.springframework.org/schema/beans"
    1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    1. xmlns:p="http://www.springframework.org/schema/p"
    1. xmlns:aop="http://www.springframework.org/schema/aop"
    1. xmlns:context="http://www.springframework.org/schema/context"
    1. xmlns:jee="http://www.springframework.org/schema/jee"
    1. xmlns:tx="http://www.springframework.org/schema/tx"
    1. xsi:schemaLocation="
    1. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    1. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    1. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    1. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
    1. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    1. <!-- 配置数据源 ,连接池用的阿里druid-->
    1. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    1. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    1. <!--
    1. <property name="url" value="jdbc:mysql://IP+数据库"/>
    1. <property name="username" value="用户名"/>
    1. <property name="password" value="密码"/>
    1. -->
    1. <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
    1. <property name="username" value="root"/>
    1. <property name="password" value="root"/>
    1. </bean>
    1. <!-- 配置mybatis的sqlSessionFactory -->
    1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    1. <property name="dataSource" ref="dataSource" />
    1. <!-- 自动扫描mappers.xml文件 -->
    1. <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    1. <!-- mybatis配置文件 -->
    1. <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    1. </bean>
    1. <!-- DAO -->
    1. <bean id="infoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
    1. <property name="mapperInterface" value="net.xjdsz.dao.InfoDao" />
    1. <property name="sqlSessionFactory" value="sqlSessionFactory"></property>
    1. </bean>
    1. </beans>

info.xml
    1. <?xml version="1.0" encoding="UTF-8" ?>
    1. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    1. <mapper namespace="net.xjdsz.dao.InfoDao">
    1. <!-- 查询条件:账号密码用户类型. 0第一个参数,1第二个参数,对应dao接口参数 -->
    1. <select id="FindAllInfos" resultType="net.xjdsz.model.Info">
    1. SELECT * FROM info limit 1
    1. </select>
    1. <!--
    1. <select id="getAllUsers" resultMap="userResult">
    1. SELECT USER_CODE,USER_NAME,USER_PWD,CREATE_DATE
    1. FROM BLOG_USER
    1. </select>
    1. -->
    1. </mapper>

mybatis-config.xml
    1. <?xml version="1.0" encoding="UTF-8"?>
    1. <!DOCTYPE configuration PUBLIC
    1. "-//mybatis.org//DTD Config 3.0//EN"
    1. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    1. <configuration>
    1. <mappers>
    1. <mapper resource="mapper/info.xml"/>
    1. </mappers>
    1. </configuration>


会报错:
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-config/ApplicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [E:\个人资料\个人代码\spring-batis\target\classes\mapper\info.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for net.xjdsz.dao.InfoDao.FindAllInfos

原因是,加载了两次mapper,注释掉红框即可

修改后依然会报错:
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'infoDao' defined in class path resource [spring-config/ApplicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.apache.ibatis.session.SqlSessionFactory' for property 'sqlSessionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.apache.ibatis.session.SqlSessionFactory' for property 'sqlSessionFactory': no matching editors or conversion strategy found

原因是,sqlSessionFactory注入的时候,关键字用错了,value应该改成ref???
 
 运行代码,运行成功
    1. package net.xjdsz.service.impl;
    1. import net.xjdsz.dao.InfoDao;
    1. import net.xjdsz.model.Info;
    1. import net.xjdsz.service.InfoService;
    1. import org.springframework.context.ApplicationContext;
    1. import org.springframework.context.support.ClassPathXmlApplicationContext;
    1. /**
    1. * Created by dingshuo on 2017/1/3.
    1. */
    1. public class InfoServiceImpl implements InfoService {
    1. private InfoDao infoDao;
    1. @Override
    1. public Info DoWork() {
    1. Info info=infoDao.FindAllInfos();
    1. return info;
    1. }
    1. public static void main(String[] args){
    1. ApplicationContext ctx = null;
    1. ctx = new ClassPathXmlApplicationContext("spring-config/ApplicationContext.xml");
    1. InfoDao infoDao=(InfoDao)ctx.getBean("infoDao");
    1. Info aaa=infoDao.FindAllInfos();
    1. System.out.println(aaa.toString());
    1. }
    1. }
运行结果
    1. Connected to the target VM, address: '127.0.0.1:58958', transport: 'socket'
    1. 一月 03, 2017 5:31:51 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    1. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a0ac6e3: startup date [Tue Jan 03 17:31:51 CST 2017]; root of context hierarchy
    1. 一月 03, 2017 5:31:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    1. 信息: Loading XML bean definitions from class path resource [spring-config/ApplicationContext.xml]
    1. 一月 03, 2017 5:31:52 下午 com.alibaba.druid.pool.DruidDataSource info
    1. 信息: {dataSource-1} inited
    1. Disconnected from the target VM, address: '127.0.0.1:58958', transport: 'socket'
    1. ID:1,TM:Thu Nov 10 00:00:00 CST 2016,DESC:nihao ,FLAG:0
    1. Process finished with exit code 0

Spring-Mybatis 异常记录(1)的更多相关文章

  1. Maven 搭建SpringMvc+Spring+Mybatis详细记录

    总觉得,看比人写的总是那么好,每次搭建框架时都会找博客,找教程来跟着一步一步走,虽然很快搭建成功了,但是经常情况是我并不知道我干了什么,也不记得具体步骤,到底为什么要这么做,今天我详细记录了一下自己搭 ...

  2. Mybatis 异常记录(1): Invalid bound statement (not found)

    错误信息: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.pingan.cr ...

  3. spring奇怪异常记录(会逐渐记录)

    1 严重: Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error cr ...

  4. Spring + MyBatis 框架下处理数据库异常

    一.概述 使用JDBC API时,很多操作都要声明抛出java.sql.SQLException异常,通常情况下是要制定异常处理策略.而Spring的JDBC模块为我们提供了一套异常处理机制,这套异常 ...

  5. Spring+MyBatis时Access denied for user '高欢欢'@'localhost' (using password: YES)的异常解决方案

    今天在做spring+mybatis整合的时候系统只要一运行就会报下面的错误,搞了几个小时硬是没有找的原因 警告: com.mchange.v2.resourcepool.BasicResourceP ...

  6. spring,mybatis事务管理配置与@Transactional注解使用[转]

    spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...

  7. spring+mybatis+mina+logback框架搭建

    第一次接触spring,之前从来没有学过spring,所以算是赶鸭子上架,花了差不多一个星期来搭建,中间遇到各种各样的问题,一度觉得这个框架搭建非常麻烦,没有一点技术含量,纯粹就是配置,很低级!但随着 ...

  8. SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现

    上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置. 1.    外部架包依赖引入 外部依赖包引入 ...

  9. spring,mybatis事务管理配置与@Transactional注解使用

    spring,mybatis事务管理配置与@Transactional注解使用[转]   spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是 ...

  10. Spring+MyBatis双数据库配置

    Spring+MyBatis双数据库配置 近期项目中遇到要调用其它数据库的情况.本来仅仅使用一个MySQL数据库.但随着项目内容越来越多,逻辑越来越复杂. 原来一个数据库已经不够用了,须要分库分表.所 ...

随机推荐

  1. 小白日记52:kali渗透测试之Web渗透-HTTPS攻击(Openssl、sslscan、sslyze、检查SSL的网站)

    HTTPS攻击 全站HTTPS正策划稿那位潮流趋势 如:百度.阿里 HTTPS的作用 CIA 解决的是信息传输过程中数据被篡改.窃取 [从中注入恶意代码,多为链路劫持] 加密:对称.非对称.单向 HT ...

  2. SQL Server 的事务和锁(一)

    最近在项目中进行压力测试遇到了数据库的死锁问题,简言之,如下的代码在 SERIALIZABLE 隔离级别造成了死锁: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 SELECT @ ...

  3. [转]poll技术

    poll()函数:这个函数是某些Unix系统提供的用于执行与select()函数同等功能的函数,下面是这个函数的声明: #include <poll.h> int poll(struct ...

  4. UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position xxx ordinal

    python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't deco ...

  5. Oracle基础 (十三)日期函数

    日期函数 SYSDATE --当前系统时间 select sysdate from dual; EXTRACT --获取当前年份 select extract(year from sysdate) f ...

  6. 怒刷DP之 HDU 1087

    Super Jumping! Jumping! Jumping! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64 ...

  7. codeforces 675A A. Infinite Sequence(水题)

    题目链接: A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input st ...

  8. hdu-5681 zxa and wifi(dp)

    题目链接: zxa and wifi Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Othe ...

  9. ASP.NET MVC 4框架揭秘(微软6任MVP,高级软件顾问蒋金楠新作)

    http://www.cnblogs.com/artech/

  10. OC Protocol----协议

    类似Java的泛型与接口的结合体,用于类型的<>中,可以多继承(按照OC的说法叫遵从某些协议) 1.定义协议 @protocol Client <NSObject> -(voi ...