[关闭]
@PheonixHkbxoic 2018-03-07T18:09:41.000000Z 字数 1648 阅读 924

SpringBoot-Mybatis

SpringBoot



1.注意点:

  1. <mapper namespace="cn.pheker.springbootdemo.dao.UserMapper">
  2. <resultMap id="BaseResultMap" type="cn.pheker.springbootdemo.model.User">
  3. <id column="id" jdbcType="VARCHAR" property="id" />
  4. <result column="login_name" jdbcType="VARCHAR" property="loginName" />
  5. <result column="real_name" jdbcType="VARCHAR" property="realName" />
  6. <result column="password" jdbcType="VARCHAR" property="password" />
  7. <result column="email" jdbcType="VARCHAR" property="email" />
  8. <result column="telephone" jdbcType="VARCHAR" property="telephone" />
  9. <result column="address" jdbcType="VARCHAR" property="address" />
  10. </resultMap>
  11. <select id="selectAll" resultMap="BaseResultMap">
  12. select
  13. <include refid="Base_Column_List" />
  14. from t_user
  15. </select>
  16. </mapper>

1. namespace是Mapper接口的全路径名

2. resultMap的type一般是对应的实体类全路径名

3. id名须与Mapper接口中的方法名一致

4. 须在Mapper接口上添加注解@Mapper

5. 须在启动类上添加注解

@MapperScan("cn.pheker.springbootdemo.mapper")
用来扫描*Mapper.xml这样的文件,参数是xml的basePackage,可配置多个

6. application.yml配置文件中配置

  1. mybatis:
  2. mapperLocations: classpath*:cn/pheker/springbootdemo/mapper/*.xml
  3. typeAliasesPackage: cn.pheker.springbootdemo.model

7.BindingException异常

  1. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.pheker.demo.springbootdemo.dao.TUserMapper.queryAll

发生此异常时须仔细检查上面注意事项,如果还是有问题,则在pom文件的build下添加

  1. <!--打包时包含源代码包下的资源文件-->
  2. <resources>
  3. <resource>
  4. <directory>src/main/java</directory>
  5. <includes>
  6. <include>**/*.properties</include>
  7. <include>**/*.xml</include>
  8. </includes>
  9. </resource>
  10. </resources>

2.网上文章

SpringBoot集成Mybatis

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注