mybatis-使用

mybatis很方便,不用像hibernate一样关心太多的关联关系,也不用写JPA
本文主要介绍mybatis的一些常用用法和注意事项

EXAMPLE使用

    Example example = new Example(demo.class);
    // 排序
    example.setOrderByClause("yourCode desc");
    //yourCode 是实体类中的字段名称,非数据库字段名称
    example.createCriteria().andEqualTo("yourCode", String.valueOf(lawCase.getMediationMechanismId()));
    List<demo> list = demoMapper.selectByExample(example);
    if (list.size() > 0) {
      demo demo = list.get(0);
      disputeDetail.setMediateOrganization(demo.getDeptname());
    }

mybatis Generator使用

mybatis Generator可以很方便的通过数据库生成mapper和相关domain文件, 只需要加一下配置即可,网上有很多教程。
ECLIPSE 推荐命令 mybatis-generator:generate

打包插件

    
            org.mybatis.generator
            mybatis-generator-maven-plugin
            1.3.5
            
                true
                true
            
            
                
                
                    org.mybatis.generator
                    mybatis-generator-core
                    1.3.5
                
                
                    tk.mybatis
                    mapper
                    3.4.6
                
            
        

mabatis 常见使用说明

result数据类型接收问题

resultType 和 resultMap的区别,如果是DTO接收数据则使用resultType,如果没有DTO则使用resultMAP接收。

1 mybatis ADDBATCH插入数据问题

如果不设置allowMultiQueries=true, 批量插入时候会出现数据字段丢失问题。

jdbc.url=jdbc:mysql://218.94.1.197:18366/business_dev?useUnicode=yes&characterEncoding=UTF-8&allowMultiQueries=true

mybatis批量更新数据使用LIST

int updateBatch(@Param(value = "list") List<YourDto> list);
<update id="updateBatch" parameterType="java.util.List">
        <foreach collection="list" item="bean" index="index" open="" close="" separator=";">
            UPDATE green_beans
            <set>
                stock=#{bean.stock}
            </set>
            <where>
                beanUid = #{bean.beanUid}
            </where>
        </foreach>
    </update>

总结

mybatis常用注意事项和用法。


   Reprint policy


《mybatis-使用》 by jackromer is licensed under a Creative Commons Attribution 4.0 International License
 Previous
coneyEdit使用笔记 coneyEdit使用笔记
简介coney edit 是一个很好的协助开发的软件尤其是数据批量处理上有很强大的功能 使用 conney edit的一些常使用的示例 字符串列表转大写(驼峰) setempId setempName setjob
2019-09-03
Next 
JDK8多线程并发处理 JDK8多线程并发处理
jdk8提供了新的链式编程和多线程并发处理方法,异常统一处理, CompletableFuture。 CompletableFuturepackage com.jackromer.demo.Thread; import java.ut
2019-08-28
  目录