参考:
Spring boot Mybatis 整合(完整版)——没有坑点,一次成功
https://blog.csdn.net/Winter_chen001/article/details/77249029
https://blog.csdn.net/pucao_cug/article/details/64499355
https://blog.csdn.net/Winter_chen001/article/details/80010967
https://blog.csdn.net/qq_40307945/article/details/81351302
Mybatis Generator最完整配置详解
https://www.jianshu.com/p/e09d2370b796
文章目录
问题1 Invalid bound statement
Springboot mybatis集成 Invalid bound statement (not found)
https://blog.csdn.net/qq_35981283/article/details/78590090
mybatis.typeAliasesPackage=/*pojo,entity,model 包名/
mybatis.mapperLocations=classpath:mapper/*.xml /*resource 下的mybatis xml*/
注意拼写,mapping、mapper。
问题2 Unsatisfied dependency expressed through.。。
关于SpringBoot bean无法注入的问题(与文件包位置有关)改变自动扫描的包
https://blog.csdn.net/u014695188/article/details/52263903
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘adminController’:
Unsatisfied dependency expressed through field ‘adminService’;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name ‘adminServiceImpl’:
Unsatisfied dependency expressed through field ‘adminMapper’;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name ‘adminMapper’ defined in file [D:\Development\IdeaProjects\oj\target\classes\com\simon\oj\dao\AdminMapper.class]:
Unsatisfied dependency expressed through bean property ‘sqlSessionFactory’;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]:
Factory method ‘sqlSessionFactory’ threw exception; nested exception is org.springframework.core.NestedIOException:
Failed to parse mapping resource: ‘file [D:\Development\IdeaProjects\oj\target\classes\mapper\AdminMapper.xml]’;
nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML.
The XML location is ‘file [D:\Development\IdeaProjects\oj\target\classes\mapper\AdminMapper.xml]’.
Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.simon.oj.dao.AdminMapper.BaseResultMap
根据错误最根本的信息Result Maps collection already contains
,定位问题其实是xml中有重名的对象。重名是因为在使用mybatis-generator时不小心多运行了一次生成,导致了mapper.xml中的内容重复。
Mybatis异常-Result Maps collection already contains value for xxx
https://blog.csdn.net/loongshawn/article/details/51656699
selectAll
<!-- 注意,该属性只针对MyBatis3Simple有用;
如果选择的runtime是MyBatis3Simple,那么会生成一个SelectAll方法,如果指定了selectAllOrderByClause,那么会在该SQL中添加指定的这个order条件;
-->
<property name="selectAllOrderByClause" value="age desc,username asc"/>
没有selectAll ,因为runtime 不是 MyBatis3Simple
<context id="DB2Tables" targetRuntime="MyBatis3">
MyBatis3 MyBatis3Simple
<context id="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
context的targetRuntime属性设置为MyBatis3Simple是为了避免生成Example相关的代码和方法。如果需要则改为Mybatis3.
defaultModelType="flat"目的是使每个表只生成一个实体类
来自MyBatis学习笔记(五):代码生成器
https://zjxkenshine.github.io/2018/03/29/MyBatis学习笔记(五):代码生成器/
实际尝试的时候,当<table>
标签下有<property>
标签,且<property>
标签在<generatedKey>
后面的时候,会报如下错误:
The content of element type “table” must match “(property*,generatedKey?,domainObjectRenamingRule?,columnRenamingRule?,(columnOverride|ignoreColumn|ignoreColumnsByRegex)*)”.
错误时因为没有按顺序!!!
https://blog.csdn.net/Jalon2015/article/details/49422973
先写<property>
,后写<generatedKey>
,就好了。
The <table> Element
http://www.mybatis.org/generator/configreference/table.html
The <generatedKey> Element
http://www.mybatis.org/generator/configreference/generatedKey.html
实际上,只要runtime选择了MyBatis3Simple,就会自动有selectAll 方法生成。
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
模式下生成的Mapper
public interface AdminMapper {
int deleteByPrimaryKey(String idadmin);
int insert(Admin record);
Admin selectByPrimaryKey(String idadmin);
List<Admin> selectAll();
int updateByPrimaryKey(Admin record);
}
<context id="DB2Tables" targetRuntime="MyBatis3">
模式下生成的Mapper
public interface AdminMapper {
int deleteByPrimaryKey(String idadmin);
int insert(Admin record);
int insertSelective(Admin record);
Admin selectByPrimaryKey(String idadmin);
int updateByPrimaryKeySelective(Admin record);
int updateByPrimaryKey(Admin record);
}
指定generatedKey与否
在generatorConfig的table标签下,有没有<generatedKey column="idadmin" sqlStatement="JDBC" identity="true"/>
区别如下。
有,insert的时候,不插入主键字段,因为主键自增。
<?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.simon.oj.dao.AdminMapper">
<resultMap id="BaseResultMap" type="com.simon.oj.pojo.Admin">
<id column="idadmin" jdbcType="VARCHAR" property="idadmin" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="permission" jdbcType="INTEGER" property="permission" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete from admin
where idadmin = #{idadmin,jdbcType=VARCHAR}
</delete>
<insert id="insert" keyColumn="idadmin" keyProperty="idadmin" parameterType="com.simon.oj.pojo.Admin" useGeneratedKeys="true">
insert into admin (password, permission)
values (#{password,jdbcType=VARCHAR}, #{permission,jdbcType=INTEGER})
</insert>
<update id="updateByPrimaryKey" parameterType="com.simon.oj.pojo.Admin">
update admin
set password = #{password,jdbcType=VARCHAR},
permission = #{permission,jdbcType=INTEGER}
where idadmin = #{idadmin,jdbcType=VARCHAR}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select idadmin, password, permission
from admin
where idadmin = #{idadmin,jdbcType=VARCHAR}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select idadmin, password, permission
from admin
order by idadmin desc
</select>
</mapper>
无,insert的时候插入主键在内的三个字段。
<?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.simon.oj.dao.AdminMapper">
<resultMap id="BaseResultMap" type="com.simon.oj.pojo.Admin">
<id column="idadmin" jdbcType="VARCHAR" property="idadmin" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="permission" jdbcType="INTEGER" property="permission" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete from admin
where idadmin = #{idadmin,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.simon.oj.pojo.Admin">
insert into admin (idadmin, password, permission
)
values (#{idadmin,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{permission,jdbcType=INTEGER}
)
</insert>
<update id="updateByPrimaryKey" parameterType="com.simon.oj.pojo.Admin">
update admin
set password = #{password,jdbcType=VARCHAR},
permission = #{permission,jdbcType=INTEGER}
where idadmin = #{idadmin,jdbcType=VARCHAR}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select idadmin, password, permission
from admin
where idadmin = #{idadmin,jdbcType=VARCHAR}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select idadmin, password, permission
from admin
order by idadmin desc
</select>
</mapper>
目前使用的时mybatis3simple,如果使用mybatis3生成,如何进行更复杂的查询。
不用Simple模式
Mybatis-Generator 进阶用法 快速开发 跟sql说再见
https://www.jianshu.com/p/0155be036298
MyBatis的Mapper接口以及Example的实例函数及详解
https://blog.csdn.net/biandous/article/details/65630783
[mybatis]Example的用法
https://blog.csdn.net/zhemeban/article/details/71901759
Example类用法
https://blog.csdn.net/biandous/article/details/65630783
=====####注意。example没加条件:没写example.createCriteria();
执行查询:selectByExample(example).====就会查询全部。
拓展:
如何使用动态SQL语句?
https://blog.csdn.net/chiclewu/article/details/17082555
insert与insertseletive的区别
对于insert:
//插入一条数据
//支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)
//优先使用传入的参数值,参数值空时,才会使用序列、UUID,自动增长
但是要注意假如数据库字段有default,default是不会起作用的
对于InsertSelective:
//插入一条数据,只插入不为null的字段,不会影响有默认值的字段
//支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)
//优先使用传入的参数值,参数值空时,才会使用序列、UUID,自动增长
insertSelective不会忽略default,即为空的字段default自动填充
作者:russ44
来源:CSDN
原文:https://blog.csdn.net/russ44/article/details/79041944
Mybatis中updateByPrimaryKeySelective和updateByPrimaryKey更新区别
https://blog.csdn.net/shixiansen6535/article/details/81535980
insert 带Selective,只插入对应字段,
insert不带Selective,插入对应字段,没插入的字段置为null
(我使用的场景下,insert不带Selective,把其他置为null,后续update不带selective就可以更新null字段,所以无需Selective)
update 带selective 字段不为空再更新
update 不带selective 直接更新全部
(我使用场景下,本来就第一次insert之后没有填满,需要update补全的字段,所以无需加Selective)