mybatis 总结

属性配置

1. mybatis.configuration.mapUnderscoreToCamelCase=true

mapUnderscoreToCamelCase用于映射表中的字段与model中成员的映射关系

映射关系为:表中字段去掉"_",并转为驼峰

比如:

//映射转换方式
role_id => roleId

如果配置为false,则只有一模一样的字段才可以映射到

2. mybatis.mapper-locations

用于设置mapper.xml的映射目录

比如

mybatis.mapper-locations=classpath:/mybatis/mapper/*.xml

这个路径必须写正确,之前写错误有报错以下错误,且还是在运行期发生与数据库访问时报错,无法再编译期检查出来,要格外小心

2019-08-28 13:52:33.769 ERROR 26395 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.grady.mybatisdemo.mapper.UserMapper.findAllUser] with root cause

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.grady.mybatisdemo.mapper.UserMapper.findAllUser
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.binding.MapperProxy.lambda$cachedMapperMethod$0(MapperProxy.java:61) ~[mybatis-3.5.2.jar:3.5.2]
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_191]
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:61) ~[mybatis-3.5.2.jar:3.5.2]

3. 一定要加上@MapperScan("com.grady.mybatisdemo.mapper")注解

mapperScan中需填入mapper.java所在的包的路径,方便mybatis生成增强对象

经测试@mapperScan可以加载使用@Bean @Configuration @Component @Service 注解的类上,也是可以生效的

但建议还是加载Application的类上,这样更加一目了然

4. mybatis.typeAliasesPackage=com.grady.mybatisdemo.model

mybatis.typeAliasesPackage用于指定包的别名映射关系,之后在xml中就直接使用类名就可以了

比如:

  <select id="findAllUser" resultType="User">
SELECT
*
FROM
user
</select>
PS:这个属性虽好,但建议谨慎使用,因为它固定了model类生成的目录,而大型项目往往进行模块划分,不能保证所有的model在同一个目录下,而且还需要去属性文件中去寻找目录的位置,相对会有些繁琐(==IDE也不能通过左击直接进入model类中)

5. 开启Mybatis SQL 日志

logging.level.com.grady.mybatisdemo.mapper=DEBUG

Springboot mybatis总结的更多相关文章

  1. 第五章 springboot + mybatis(转载)

    本编博客转发自:http://www.cnblogs.com/java-zhao/p/5350021.html springboot集成了springJDBC与JPA,但是没有集成mybatis,所以 ...

  2. 第九章 springboot + mybatis + 多数据源 (AOP实现)

    在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...

  3. 第五章 springboot + mybatis

    springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成.集成方式相当简单. 1.项目结构 2.pom.xml <!-- 与数 ...

  4. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

  5. Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦

    个人博客 对于技术人员来说,拥有自己的个人博客应该是一件令人向往的事情,可以记录和分享自己的观点,想到这件事就觉得有意思,但是刚开始写博客的时候脑海中是没有搭建个人博客这一想法的,因为刚起步的时候连我 ...

  6. springboot mybatis 事务管理

    本文主要讲述springboot提供的声明式的事务管理机制. 一.一些概念 声明式的事务管理是基于AOP的,在springboot中可以通过@Transactional注解的方式获得支持,这种方式的优 ...

  7. SpringBoot+Mybatis+Freemark 最简单的例子

    springboot-sample 实现最简单的 SpringBoot + Mybatis + Freemarker 网页增删改查功能,适合新接触 Java 和 SpringBoot 的同学参考 代码 ...

  8. springboot + mybatis 前后端分离项目的搭建 适合在学习中的大学生

    人生如戏,戏子多半掉泪! 我是一名大四学生,刚进入一家软件件公司实习,虽说在大学中做过好多个实训项目,都是自己完成,没有组员的配合.但是在这一个月的实习中,我从以前别人教走到了现在的自学,成长很多. ...

  9. springboot+mybatis+redis实现分布式缓存

    大家都知道springboot项目都是微服务部署,A服务和B服务分开部署,那么它们如何更新或者获取共有模块的缓存数据,或者给A服务做分布式集群负载,如何确保A服务的所有集群都能同步公共模块的缓存数据, ...

  10. Java逆向工程SpringBoot + Mybatis Generator + MySQL

    Java逆向工程SpringBoot+ Mybatis Generator + MySQL Meven pop.xml文件添加引用: <dependency> <groupId> ...

随机推荐

  1. 深入理解springboot的自动注入

    一.开篇   在平时的开发过程中用的最多的莫属springboot了,都知道springboot中有自动注入的功能,在面试过程中也会问到自动注入,你知道自动注入是怎么回事吗,springboot是如何 ...

  2. docker-compose: 未找到命令,安装docker-compose

    1.安装扩展源 sudo yum -y install epel-release 2.安装python-pip模块 sudo yum install python-pip 3.通过命令进行安装 cd ...

  3. Codeforces Round #783 (Div. 2)

    A. Direction Change 题意 从(1,1)点出发到(n,m),每次可以向上下左右四个方向移动,但是不能与上次移动方向相同 最少要移动多少不,如果不能到达输出 -1 思路 假设n< ...

  4. Host–Parasite(主从关系): Graph LSTM-in-LSTM for Group Activity Recognition

    This article aims to tackle the problem of group activity recognition in the multiple-person scene. ...

  5. .NET ORM框架HiSql实战-第三章-使用自定义编号生成【申请编号】

    一.引言 上一篇.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查) 中菜单编号采用的是雪花ID,生成的编号无法自定义.比如本系统的一个申请业务,需要按前缀+日期+流水号 ...

  6. SpringBoot事件监听器源码分析

    本文涉及到Spring的监听器,如果不太了解请先阅读之前的Spring监听器的文章. SpringBoot事件监听器初始化 SpringBoot中默认定义了11个事件监听器对象,全部定义在META-I ...

  7. 集合-新特性foreach循环遍历集合或项目

    1.增强for循环对集合的遍历 点击查看代码 @Test //集合遍历 public void test3(){ Collection coll = new ArrayList(); coll.add ...

  8. mvc Ensure that HttpConfiguration.EnsureInitialized()

    The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called ...

  9. SpringBoot数据库管理 - 用flyway对数据库管理和迁移

    上文介绍了Liquibase,以及和SpringBoot的集成.除了Liquibase之外,还有一个组件Flyway也是经常被使用到的类似的数据库版本管理中间件.本文主要介绍Flyway, 以及Spr ...

  10. Android多版本flavor配置之资源文件和清单文件合并介绍

    知识背景 Android studio升级到3.0之后,gradle增加了多维度管理配置,便于同一个项目中创建应用的不同版本,分别管理依赖项并签署配置.创建产品风味与创建构建类型类似:只需将它们添加到 ...