-
首先mapper里的xml文件要放在正确的位置下,如果放在main后面的test文件夹下,则可能找不到。
-
如果源码中断点打不了,可以把SqlSessionFactoryBean给复制到项目中包下再打断点。/** * Set locations of MyBatis mapper files that are going to be merged into the {@code SqlSessionFactory} * configuration at runtime. * * This is an alternative to specifying "<sqlmapper>" entries in an MyBatis config file. * This property being based on Spring's resource abstraction also allows for specifying * resource patterns here: e.g. "classpath*:sqlmap/*-mapper.xml". */ public void setMapperLocations(Resource[] mapperLocations) { this.mapperLocations = mapperLocations; }
-
看到result maps collection already contains value这个错误,下意识认为是alias中的type与resultmap中的type冲突导致。
或者为自动扫描、手动扫描时进行了重复加载导致。
经过了一顿艰辛的折腾后,发现该问题为:
sql语句返回时,使用<select id="xxx" resultType="example">与<resultMap id="example" type=“xxx”>相冲突导致的。
将resulttype更改为resultmap即可解决该问题。
-
-
-
关于这个问题网上也有一些解决方案,但基本上都是自己遇到了哪种就记录哪种,其实有很多原因都会导致启动报错,在这里做个记录:
1、http://www.cnblogs.com/huanmieuroshui/archive/2012/12/18/2822754.html,网友已经说得很清楚了
2、parameterType中的问题。这里的类名如果找不到也会报这个错,比如你之前是将该类名写死在这里,之后由于重构将该类转移到其他包中,如果这里不修改也会报这个错
3、还是parameterType中的问题,第2点是关于自定义类的,当你使用基本类型的时候,比如int、string等,千万不要写错了,比如写成strnig,咋一看看不出来,结果该问题就很难找
4、如果是自定义resultMap,如果这里写成resultType,也会报这个错。
总之,报这个错的原因很多,我这里进行总结一下,如果以后还遇到其他原因导致这个错,我也会及时更新。
另外,记录一下这种错误的查找方法,就是先将整个xml文件的一半注释掉,类似与“二分查找”一样,逐渐收拢范围。
还是先学校下mybatis中的各种参数把 -
MyBatis的mapping.xml中有两个重要的属性:parameterType和resultMap属性(parameterMap现在不建议使用)
parameterType用来指定传入参数的类型,比如Bean或Map\List。
- <configuration>
- <typeAliases>
- <typeAlias alias="Product" type="com.zainagou.supplier.entity.Product"/>
- </typeAliases>
- <!-- 映射map -->
- <mappers>
- </mappers>
- </configuration>
resultMap是MyBatis中最重要最强大的元素。resultMap封装查询的结果集,type可以是Bean、Object、Primitive type。
- <?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" >
- <mapper namespace="com.zainagou.supplier.mapper.ProductCategoryMapper">
- <parameterMap id="productCategory" type="com.zainagou.supplier.entity.ProductCategory"></parameterMap>
- <resultMap id="resultMap" type="com.zainagou.supplier.entity.ProductCategory" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="p_id" property="pid" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, fid, name
- </sql>
- <select id="getProductCategorys" resultMap="resultMap" parameterMap="productCategory">
- select <include refid="Base_Column_List" /> from product_category where 00=0
- <if test="name!=null and name!=''">
- and name like "%"#{name,jdbcType=VARCHAR}"%"
- </if>
- </select>
- </mapper>
从上面mapping.xml可以看出:
resultMap中, column是列名,property是列映射到实体对象的属性,jdbcType是列在数据库中的数据类型,格式统一都是大写,和MySql中的数据类型多数保持一致,少数有差异。
这种方式可以解决列名不匹配。
-
-
resultType:sql的执行结果
-
parameterType:表示输入参数的类型,
-
-
resultMap封装查询的结果集
-
MyBatis的mapping.xml中有两个重要的属性:parameterType和resultMap属性(parameterMap现在不建议使用)
parameterType用来指定传入参数的类型,比如Bean或Map\List。
- <configuration>
- <typeAliases>
- <typeAlias alias="Product" type="com.zainagou.supplier.entity.Product"/>
- </typeAliases>
- <!-- 映射map -->
- <mappers>
- </mappers>
- </configuration>
resultMap是MyBatis中最重要最强大的元素。resultMap封装查询的结果集,type可以是Bean、Object、Primitive type。
- <?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" >
- <mapper namespace="com.zainagou.supplier.mapper.ProductCategoryMapper">
- <parameterMap id="productCategory" type="com.zainagou.supplier.entity.ProductCategory"></parameterMap>
- <resultMap id="resultMap" type="com.zainagou.supplier.entity.ProductCategory" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="p_id" property="pid" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, fid, name
- </sql>
- <select id="getProductCategorys" resultMap="resultMap" parameterMap="productCategory">
- select <include refid="Base_Column_List" /> from product_category where 00=0
- <if test="name!=null and name!=''">
- and name like "%"#{name,jdbcType=VARCHAR}"%"
- </if>
- </select>
- </mapper>
从上面mapping.xml可以看出:
resultMap中, column是列名,property是列映射到实体对象的属性,jdbcType是列在数据库中的数据类型,格式统一都是大写,和MySql中的数据类型多数保持一致,少数有差异。
这种方式可以解决列名不匹配。
parameterType指定输入参数为pojo自定义对象时,在sql中使用${}和#{}获取pojo的属性。
MyBatis的mapping.xml中有两个重要的属性:parameterType和resultMap属性(parameterMap现在不建议使用)
parameterType用来指定传入参数的类型,比如Bean或Map\List。
- <configuration>
- <typeAliases>
- <typeAlias alias="Product" type="com.zainagou.supplier.entity.Product"/>
- </typeAliases>
- <!-- 映射map -->
- <mappers>
- </mappers>
- </configuration>
resultMap是MyBatis中最重要最强大的元素。resultMap封装查询的结果集,type可以是Bean、Object、Primitive type。
- <?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" >
- <mapper namespace="com.zainagou.supplier.mapper.ProductCategoryMapper">
- <parameterMap id="productCategory" type="com.zainagou.supplier.entity.ProductCategory"></parameterMap>
- <resultMap id="resultMap" type="com.zainagou.supplier.entity.ProductCategory" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="p_id" property="pid" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, fid, name
- </sql>
- <select id="getProductCategorys" resultMap="resultMap" parameterMap="productCategory">
- select <include refid="Base_Column_List" /> from product_category where 00=0
- <if test="name!=null and name!=''">
- and name like "%"#{name,jdbcType=VARCHAR}"%"
- </if>
- </select>
- </mapper>
从上面mapping.xml可以看出:
resultMap中, column是列名,property是列映射到实体对象的属性,jdbcType是列在数据库中的数据类型,格式统一都是大写,和MySql中的数据类型多数保持一致,少数有差异。
这种方式可以解决列名不匹配。
resultType:sql查询结果集使用resultType映射,要求sql查询字段名和pojo的属性名一致,才能映射成功。
resultMap: 如果sql查询结果集的字段名和pojo的属性名不一致,使用resultMap将sql查询字段名和pojo的属性作一个对应关系 ,首先需要定义resultMap,最终要使用pojo作为结果集映射对象。
建议:一般情况下建议使用resultType,因为简单方便。
针对一对多查询或要使用延迟加载 ,必须使用resultMap。
MyBatis的mapping.xml中有两个重要的属性:parameterType和resultMap属性(parameterMap现在不建议使用)
parameterType用来指定传入参数的类型,比如Bean或Map\List。
- <configuration>
- <typeAliases>
- <typeAlias alias="Product" type="com.zainagou.supplier.entity.Product"/>
- </typeAliases>
- <!-- 映射map -->
- <mappers>
- </mappers>
- </configuration>
resultMap是MyBatis中最重要最强大的元素。resultMap封装查询的结果集,type可以是Bean、Object、Primitive type。
- <?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" >
- <mapper namespace="com.zainagou.supplier.mapper.ProductCategoryMapper">
- <parameterMap id="productCategory" type="com.zainagou.supplier.entity.ProductCategory"></parameterMap>
- <resultMap id="resultMap" type="com.zainagou.supplier.entity.ProductCategory" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="p_id" property="pid" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, fid, name
- </sql>
- <select id="getProductCategorys" resultMap="resultMap" parameterMap="productCategory">
- select <include refid="Base_Column_List" /> from product_category where 00=0
- <if test="name!=null and name!=''">
- and name like "%"#{name,jdbcType=VARCHAR}"%"
- </if>
- </select>
- </mapper>
从上面mapping.xml可以看出:
resultMap中, column是列名,property是列映射到实体对象的属性,jdbcType是列在数据库中的数据类型,格式统一都是大写,和MySql中的数据类型多数保持一致,少数有差异。
这种方式可以解决列名不匹配。
resultType:实现查询时,需要自定义pojo,pojo的属性名和sql查询列名一致。企业开发中resultType简单方便建议使用。
resultMap:可以将sql查询结果信息中部分属性映射到一个pojo中。需要程序员进行映射配置,比较麻烦。
针对一对一查询建议使用resultType。
如果一对一查询要使用mybatis提供的延迟加载 功能,必须使用resultMap。
MyBatis的mapping.xml中有两个重要的属性:parameterType和resultMap属性(parameterMap现在不建议使用)
parameterType用来指定传入参数的类型,比如Bean或Map\List。
- <configuration>
- <typeAliases>
- <typeAlias alias="Product" type="com.zainagou.supplier.entity.Product"/>
- </typeAliases>
- <!-- 映射map -->
- <mappers>
- </mappers>
- </configuration>
resultMap是MyBatis中最重要最强大的元素。resultMap封装查询的结果集,type可以是Bean、Object、Primitive type。
- <?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" >
- <mapper namespace="com.zainagou.supplier.mapper.ProductCategoryMapper">
- <parameterMap id="productCategory" type="com.zainagou.supplier.entity.ProductCategory"></parameterMap>
- <resultMap id="resultMap" type="com.zainagou.supplier.entity.ProductCategory" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="p_id" property="pid" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, fid, name
- </sql>
- <select id="getProductCategorys" resultMap="resultMap" parameterMap="productCategory">
- select <include refid="Base_Column_List" /> from product_category where 00=0
- <if test="name!=null and name!=''">
- and name like "%"#{name,jdbcType=VARCHAR}"%"
- </if>
- </select>
- </mapper>
从上面mapping.xml可以看出:
resultMap中, column是列名,property是列映射到实体对象的属性,jdbcType是列在数据库中的数据类型,格式统一都是大写,和MySql中的数据类型多数保持一致,少数有差异。
这种方式可以解决列名不匹配。
1 延迟加载
延迟加载意义:在需求允许的情况下,先查询单表,当需要关联其它表查询时,进行延迟加载,去关联查询,达到目标:不需要关联信息时不查询,需要时再查询。好处:提高数据库的性能。
MyBatis的mapping.xml中有两个重要的属性:parameterType和resultMap属性(parameterMap现在不建议使用)
parameterType用来指定传入参数的类型,比如Bean或Map\List。
- <configuration>
- <typeAliases>
- <typeAlias alias="Product" type="com.zainagou.supplier.entity.Product"/>
- </typeAliases>
- <!-- 映射map -->
- <mappers>
- </mappers>
- </configuration>
resultMap是MyBatis中最重要最强大的元素。resultMap封装查询的结果集,type可以是Bean、Object、Primitive type。
- <?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" >
- <mapper namespace="com.zainagou.supplier.mapper.ProductCategoryMapper">
- <parameterMap id="productCategory" type="com.zainagou.supplier.entity.ProductCategory"></parameterMap>
- <resultMap id="resultMap" type="com.zainagou.supplier.entity.ProductCategory" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="p_id" property="pid" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, fid, name
- </sql>
- <select id="getProductCategorys" resultMap="resultMap" parameterMap="productCategory">
- select <include refid="Base_Column_List" /> from product_category where 00=0
- <if test="name!=null and name!=''">
- and name like "%"#{name,jdbcType=VARCHAR}"%"
- </if>
- </select>
- </mapper>
从上面mapping.xml可以看出:
resultMap中, column是列名,property是列映射到实体对象的属性,jdbcType是列在数据库中的数据类型,格式统一都是大写,和MySql中的数据类型多数保持一致,少数有差异。
这种方式可以解决列名不匹配。
1.1 为什么使用缓存
将从数据库中查询出来的数据缓存起来,缓存介质:内存、磁盘,从缓存中取数据,而不从数据库查询,减少了数据库的操作,提高了数据处理性能。
1.2 一级缓存
Mybatis默认提供一级缓存,缓存范围是一个sqlSession。
在同一个SqlSession中,两次执行相同的sql查询,第二次不再从数据库查询。
测试:
-
-
MyBatis的mapping.xml中有两个重要的属性:parameterType和resultMap属性(parameterMap现在不建议使用)
parameterType用来指定传入参数的类型,比如Bean或Map\List。
- <configuration>
- <typeAliases>
- <typeAlias alias="Product" type="com.zainagou.supplier.entity.Product"/>
- </typeAliases>
- <!-- 映射map -->
- <mappers>
- </mappers>
- </configuration>
resultMap是MyBatis中最重要最强大的元素。resultMap封装查询的结果集,type可以是Bean、Object、Primitive type。
- <?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" >
- <mapper namespace="com.zainagou.supplier.mapper.ProductCategoryMapper">
- <parameterMap id="productCategory" type="com.zainagou.supplier.entity.ProductCategory"></parameterMap>
- <resultMap id="resultMap" type="com.zainagou.supplier.entity.ProductCategory" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="p_id" property="pid" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, fid, name
- </sql>
- <select id="getProductCategorys" resultMap="resultMap" parameterMap="productCategory">
- select <include refid="Base_Column_List" /> from product_category where 00=0
- <if test="name!=null and name!=''">
- and name like "%"#{name,jdbcType=VARCHAR}"%"
- </if>
- </select>
- </mapper>
从上面mapping.xml可以看出:
resultMap中, column是列名,property是列映射到实体对象的属性,jdbcType是列在数据库中的数据类型,格式统一都是大写,和MySql中的数据类型多数保持一致,少数有差异。
这种方式可以解决列名不匹配。
MyBatis之传入参数——parameterType(转)
鸣谢:http://blog.youkuaiyun.com/liaoxiaohua1981/article/details/6862764
--------------------------------------------------------------------
在MyBatis的select、insert、update、delete这些元素中都提到了parameterType这个属性。MyBatis现在可以使用的parameterType有基本数据类型和JAVA复杂数据类型
- 基本数据类型:包含int,String,Date等。基本数据类型作为传参,只能传入一个。通过#{参数名} 即可获取传入的值
- 复杂数据类型:包含JAVA实体类、Map。通过#{属性名}或#{map的KeyName}即可获取传入的值
基本数据类型参数示例:
根据班级ID查询教师列表
xml文件
<select id="selectTeacher" parameterType="int" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} </select>
java代码
List<Teacher> tList = teacherMapper.selectTeacher(2); for (Teacher entityTemp : tList) { System.out.println(entityTemp.toString()); }
JAVA实体类型参数示例:
xml文件 (注:parameterType="com.myapp.domain.Teacher" 可简写为 parameterType="Teacher" )
<select id="selectTeacher" parameterType="com.myapp.domain.Teacher" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} </select>
java代码
java代码 Teacher queryTeacher=new Teacher(); queryTeacher.setId(2); List<Teacher> tList = teacherMapper.selectTeacher(queryTeacher); for (Teacher entityTemp : tList) { System.out.println(entityTemp.toString()); }
Map参数示例:
xml文件
<select id="selectTeacher" parameterType="Map" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} and sex=#{sex} </select>
java代码
Map<String,String> map=new HasMap<String,String>(); map.put("id","2"); map.put("sex","男"); List<Teacher> tList = teacherMapper.selectTeacher(map); for (Teacher entityTemp : tList) { System.out.println(entityTemp.toString()); }
另外MyBatis还提供了一个使用注解来参入多个参数的方式。这种方式需要在接口的参数上添加@Param注解
示例:
接口方法
public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex);
XML文件
<select id="selectTeacher" resultType="com.myapp.domain.Teacher"> select * from Teacher where c_id=#{id} and sex=#{sex} </select>
测试代码
List<Teacher> tList = teacherMapper.selectTeacher("2","男"); for (Teacher entityTemp : tList) { System.out.println(entityTemp.toString());
MyBatis的mapping.xml中有两个重要的属性:parameterType和resultMap属性(parameterMap现在不建议使用)
parameterType用来指定传入参数的类型,比如Bean或Map\List。
- <configuration>
- <typeAliases>
- <typeAlias alias="Product" type="com.zainagou.supplier.entity.Product"/>
- </typeAliases>
- <!-- 映射map -->
- <mappers>
- </mappers>
- </configuration>
resultMap是MyBatis中最重要最强大的元素。resultMap封装查询的结果集,type可以是Bean、Object、Primitive type。
- <?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" >
- <mapper namespace="com.zainagou.supplier.mapper.ProductCategoryMapper">
- <parameterMap id="productCategory" type="com.zainagou.supplier.entity.ProductCategory"></parameterMap>
- <resultMap id="resultMap" type="com.zainagou.supplier.entity.ProductCategory" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="p_id" property="pid" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, fid, name
- </sql>
- <select id="getProductCategorys" resultMap="resultMap" parameterMap="productCategory">
- select <include refid="Base_Column_List" /> from product_category where 00=0
- <if test="name!=null and name!=''">
- and name like "%"#{name,jdbcType=VARCHAR}"%"
- </if>
- </select>
- </mapper>
从上面mapping.xml可以看出:
resultMap中, column是列名,property是列映射到实体对象的属性,jdbcType是列在数据库中的数据类型,格式统一都是大写,和MySql中的数据类型多数保持一致,少数有差异。
这种方式可以解决列名不匹配。