码迷,mamicode.com
首页 > 移动开发 > 详细

Mapper的使用

时间:2018-02-16 18:40:06      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:hub   tween   data-   plugins   添加   window   分页   git   config   

Mapper
原理:
* 通过Mybatis拦截器的原理,动态的帮我们实现单表的增删改查;
用法:
* 倒依赖:com.github.abe1533
* MyBatis-config.xml中配置通用Mapper在mybatis中的拦截器,   
<plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql" />
            <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
            <property name="rowBoundsWithCount" value="true" />
        </plugin>
        
        <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
            <!--主键自增回写方法,默认值MYSQL,详细说明请看文档 -->
            <property name="IDENTITY" value="MYSQL" />
            <!--通用Mapper接口,多个通用接口用逗号隔开 -->
            <property name="mappers" value="com.github.abel533.mapper.Mapper" />
        </plugin>
    </plugins>
API:
  1.  selectOne():查询一个对象,(会把查询对象的非空属性作为查询条件,不同的属性作为条件之间的关系是and,返回是多个对象则抛出异常);
  2. select():查询多个对象(集合);
  3. selectCount():查询总条数信息  select count(*) from 表名 where ...
  4. selectByPrimaryKey():根据主键查询(@ID注解的);
  5. insert():插入数据,不管数据是否为null;
  6. insertSelective():只插入非空字段的数据;
  7. delete():根据条件删除;
  8. deleteByPrimaryKey():根据主键删除;
  9. updateByPrimaryKeySeletive():根据根据主键(修改条件)修改此对象的其他属性.例如:update 表名 set age = ? where 主键 = ?;(age和主键都要自己设置)
  10. 复杂条件的查询,修改和删除 : 通过example对象来实现
    

// 创建Example对象,并且指定要操作的实体类的Class对象

      Example example = new Example(User.class);

      // 创建查询条件对象,默认是and关系

      example.createCriteria().andEqualTo("sex", 2).andBetween("age", 16, 24);// 查询女性,并且年龄在1624

      // 添加查询条件,or关系

      example.or(example.createCriteria().andEqualTo("userName", "lisi"));// 或者用户名是lisi

      // 实现排序,多个排序规则以‘,‘隔开

      example.setOrderByClause("age desc");

     

      List<User> users = this.userMapper.selectByExample(example);






Mapper的使用

标签:hub   tween   data-   plugins   添加   window   分页   git   config   

原文地址:https://www.cnblogs.com/xiaolige/p/8450340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!