mybatis常用技巧

mybatis常用技巧

  • list的for循环
  • 分页
  • insert
  • update
  • #{} 预处理
  • ${} 字符串替换
  • 批量提交
  • mybatis plus实用工具类
    • TableInfoHelper
  • mapper.xml文件常用标签
    • select标签
    • insert标签
    • update标签
    • delete标签
    • if标签
    • choose标签
    • where标签
    • set标签
    • trim标签
    • foreach
  • mybatis QueryWrapper
  • mybatis-plus打印日志

list的for循环

mybatis-plus

	IPage> page = this.baseMapper.selectMyPage(new Page>(pageIndex, pageSize), paramMap);
	@Select({""})List<Customer > findByTableIdList(@Param("tableIdList")List<String> tableIdList);

分页

service

		paramMap.put("cust_name", "%公司");paramMap.put("clue_level_code ", "6");IPage<Map<String, Object>> page = this.baseMapper.selectMyPage(new Page<Map<String,Object>>(pageIndex, pageSize), paramMap);

mapper

	@Select({""})IPage<Map<String, Object>> selectMyPage(IPage<Map<String, Object>> page, @Param("param")Map<String, Object> param);

insert

void insertMapObject(@Param("param")Map<String, Object> param);
	<insert id="insertMapObject" parameterType="Map">insert into t_customer (<if test="param.ID != null and param.ID != '' ">ID if><if test="param.CUST_ID != null and param.CUST_ID != '' ">, CUST_ID if><if test="param.CUST_NAME != null and param.CUST_NAME != '' ">, CUST_NAME if>) values (<if test="param.ID != null and param.ID != '' ">#{param[${'ID'}]} if><if test="param.CUST_ID != null and param.CUST_ID != '' ">, #{param[${'CUST_ID'}]} if><if test="param.CUST_NAME != null and param.CUST_NAME != '' ">, #{param[${'CUST_NAME'}]} if>)insert>

写法二
动态变量map参数,
缺点:必须确保key就是字段名

	@Select({""})void insertMapObject(@Param("param")Map<String, Object> param);

update

void updateMapObject(@Param("param")Map<String, Object> param, @Param("id")String id);
	<update id="updateMapObject" parameterType="Map">update t_customer set id = #{id}<if test="param.ID != null and param.ID != '' ">, ID = #{param.ID} if><if test="param.CUST_ID != null and param.CUST_ID != '' ">, CUST_ID = #{param.CUST_ID} if><if test="param.CUST_NAME != null and param.CUST_NAME != '' ">, CUST_NAME = #{param.CUST_NAME} if>where id = #{id} update>

写法二
动态变量map参数,
缺点:必须确保key就是字段名

	@Select({""})void updateMapObject(@Param("param")Map<String, Object> param, @Param("id")String id);

#{} 预处理

#{}可以防止sql注入

${} 字符串替换

批量提交

mybatis plus实用工具类

TableInfoHelper

// 实体类反射表辅助类
com.baomidou.mybatisplus.core.metadata.TableInfoHelper// 获取实体映射表信息
public static TableInfo getTableInfo(Class<?> clazz)// 获取所有实体映射表信息
public static List<TableInfo> getTableInfos()
    public boolean saveOrUpdate(T entity) {if (null != entity) {Class<?> cls = entity.getClass();TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");String keyProperty = tableInfo.getKeyProperty();Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity);}return false;}

mapper.xml文件常用标签

select标签

<select id="selectByPage" parameterType="" resultType="" resultMap="" flushCache="" useCache="">select * from t_table where id = #{param.id}
select>
  • parameterType: 参数类型,可选属性。
    MyBatis 可以通过 TypeHandler推断出具体传入语句的参数,默认值为 unset
  • resultType: 期望返回的数据类型的类的全限定名。
  • resultMap: 结果集映射,不可以和 resultType 同时使用。一般如果返回数据类型是常用的类型
    比如 String Map List 的时候,可以使用 resultType
    如果返回的是简单 POJO 类的时候,也可以直接使用 resultType
    如果是复杂的映射或者连级查询的时候就需要使用 resultMap
  • flushCache :将其设置为 true,任何时候只要语句被调用,都会导致本地缓存和二级缓存都会被清空,默认值:false。
  • useCache: 将其设置为 true,将会导致本条语句的结果被二级缓存,默认值:对 select 元素为 true。
List<Map<String, Object>> selectByPage(Page<Map<String, Object>> page, @Param("param")Map<String, Object> param);
<select id="selectByPage" resultType="Map">
select * from t_table t1 where 1=1
<if test="param.id !=null and param.id !=''">and t1.ID=#{param.id}if>
select>

insert标签

update标签

delete标签

if标签

choose标签

where标签

where标签会除去多余的 andor

	<select id="selectByPage" resultType="Map">select * from t_table<where><if test="param.isEnable !=null and param.isEnable !=''"> and IS_ENABLE=#{param.isEnable}if><if test="param.updateControlId !=null and param.updateControlId !=''"> and UPDATE_CONTROL_ID=#{param.updateControlId}if>where>select>

set标签

set标签会除去多余的逗号 (,

	<update id="update">upate t_table<set><if test="param.isEnable !=null and param.isEnable !=''"> IS_ENABLE=#{param.isEnable},if><if test="param.updateControlId !=null and param.updateControlId !=''">UPDATE_CONTROL_ID=#{param.updateControlId},if>set>where id = #{param.id}update>

trim标签

去掉前缀and

	<select id="selectByPage" resultType="Map">select * from t_table<trim prefix="where" prefixOverrides="and"><if test="param.isEnable !=null and param.isEnable !=''"> and IS_ENABLE=#{param.isEnable}if><if test="param.updateControlId !=null and param.updateControlId !=''"> and UPDATE_CONTROL_ID=#{param.updateControlId}if>trim>select>

去掉后缀逗号,

	<insert id="insert">insert into t_table<trim prefix="(" suffix=")" suffixOverrides=","><if test="param.isEnable !=null">IS_ENABLE,if><if test="param.updateControlId !=null">UPDATE_CONTROL_ID,if>trim><trim prefix="values(" suffix=")" suffixOverrides=","><if test="param.isEnable !=null">#{param.isEnable},if><if test="param.updateControlId !=null">#{param.updateControlId},if>trim>insert>

foreach

	<delete id="insert">delete t_tablewhere id in<foreach collection="param.ids" index="i" item="id" open="(" separator="," close=")">#{id}foreach>delete>

mybatis QueryWrapper

		QueryWrapper<AccMaterial> queryWrapper = new QueryWrapper<AccMaterial>();queryWrapper.lambda().eq(AccMaterial::getMaterialName, mapParam.get("materialName"));int count = this.count(queryWrapper);
		QueryWrapper<AccMaterial> queryWrapper = new QueryWrapper<AccMaterial>();queryWrapper.lambda().eq(AccMaterial::getMaterialName, mapParam.get("materialName"));queryWrapper.and(qw->qw.isNotNull("SDP_USER_ID").or().eq("SDP_USER_ID", mapParam.get("sdpUserId")));

mybatis-plus打印日志

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部