1. 运行mybatis自动生成代码时,出现如下错误:java
缘由是缺乏时区的设定,须要加入 serverTimezone=GMT%2B8mysql
<!--数据库连接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/user_center?serverTimezone=GMT%2B8" userId="root" password="password"> </jdbcConnection>
其实还有另外一种写法:spring
<!--数据库连接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/user_center" userId="root" password="password"> <property name="serverTimezone" value="GMT%2B8"/> </jdbcConnection><!--MySQL 8.x 须要指定服务器的时区-->
2. 运行mybatis自动生成代码时,出现提示: Project src does not existsql
在src前加上项目名!!!如:数据库
<!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.cjsz.usercenter.mybatis.model" targetProject="user-center/src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator>
且 src/main/java、src/main/resources是不可以拆开的,不然不会提示Project src does not exist,也不会在对应目录生成文件!!服务器
3. 当生成完成后,出现的代码与咱们预料的不大同样,在model里出现不少不知道哪里来的其余字段,此时控制台提示:mybatis
Table Configuration scheme.table matched more than one table(xxx.table ... xxy.table ...)
这是由于,你的mysql数据库中,不只你须要生成的database xxx 含有表table,database xxy里也含有表table,因而它会将匹配到的全部表的字段都生成出来。app
解决方法:加入 nullCatalogMeansCurrent=truespring-boot
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/wyait?serverTimezone=GMT%2B8" userId="root" password="password"> <!-- 防止匹配到其余database里的相同名字的表 --> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection>
4. 在使用mybatis操做数据库时,出现相似1的错误,处理同样,只不过是在application.properties的mysql配置中加上时区。spa
5. 出现找不到mapper这样的错误时,检查配置文件中
mybatis.mapper-locations=classpath*:mapping/*Mapper.xml mybatis.type-aliases-package=com.cjsz.usercenter.mybatis.entity
是否与你实际的路径相匹配。
6. 使用mybatis时,启动工程出现如下相似的错误:
The alias 'GeneratedCriteria' is already mapped to the value 'com.cjsz.management.mybatis.model.MstRoleExample$GeneratedCriteria'.
具体缘由未知,因该是版本问题
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> 这里版本未2.0.1时会出现上面的错误,改成2.0.0
7. if text 失效
<if test="isDelete != null and isDelete != ''"> and m.is_delete=#{isDelete} </if> <!-- 当传入的 isDelete = null时出错。 == 数据库类型为char -->
解决方法:
<if test='isDelete != null and isDelete != ""'> and m.is_delete=#{isDelete} </if> <!-- 对于字符串的判断必须使用 ' 才行 -->