您的位置:首页 > 移动开发

java.lang.IllegalArgumentException: Mapped Statements collection does not....

2015-09-19 17:11 519 查看
<span style="font-size:18px;">出现这个问题,是因为你的namespace命名问题:下面直接用代码来说吧!</span>
<pre name="code" class="html"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"></span>

<span style="font-size:24px;color:#ff0000;"><!-- 这里namespace的命名必须和对应的UserMapper.java文件的路径一样否则会报错 --></span>
<span style="font-size:18px;"><mapper namespace="MybatisTest2.dao.UserMapper">

<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true">
</cache>
<!-- 根据id查询得到一个user对象 -->
<select id="findById" parameterType="int" resultType="User">
select * from user where id = #{id}
</select>
<!-- 添加user对象 -->
<insert id="save" parameterType="User">
insert into user(name,age) values(#{name},#{age})
</insert>
<!-- 删除user对象 -->
<delete id="delete" parameterType="int">
delete from user where id = #{id}
</delete>
<!--更新对象 -->
<update id="update" parameterType="User">
update user set name = #{name},age = #{age} where id = #{id}
</update>
<!-- 查询对象 -->
<select id="findAll" resultType="User">
select * from user
</select>

</mapper></span>



另外很多朋友对spring整合mybatis的配置文件整不明白,或者配置总是出错下面我给出一个简洁的配置代码,对各个参数有相应的注释,希望帮的到你:

<pre name="code" class="html"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd ">

<description>Spring公共配置</description>

<!--1.配置数据源,这里用的是c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
<!--2.mybatis的SqlsessionFactory: SqlsessionFactoryBean
dataSource:引用数据源
typeAliasesPackage:指定实体类的包名,自动将实体的简单类名映射成别名
mapperLocations:扫描sql映射文件
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="MybatisTest2.model"/>
<property name="mapperLocations" value="classpath:MybatisTest2/mapping/*.xml" />
</bean>
<!-- 3.mybatis自动扫描加载sql的映射文件/接口(这里我扫描的是接口,映射文件在上面已经做了):MapperScannerConfigurer
basePackage:指定sql映射文件/接口所在的包,能动自动扫描,对用了@Autowired标签的自动注入
sqlSessionFactory:引用之前定义好的sqlsessionFactory
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="MybatisTest2.dao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!--4. 配置事务管理器 :DataSourceTransactionManager-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 5. 注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans></span>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: