美文网首页
MyBatis增强工具Extm

MyBatis增强工具Extm

作者: Meryl_258a | 来源:发表于2019-05-28 17:17 被阅读0次

Extm

项目介绍

Extm 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。使用Extm针对单表操作将不再手写SQL,从而提高开发效率。

安装教程

  1. [安装extm] 在Web项目的pop.xml添加extm引用

     <dependency>
         <groupId>com.github.meryl</groupId>
         <artifactId>extm</artifactId>
         <version>1.0.0</version>
     </dependency>
    
  2. 修改SpringMVC项目配置文件

    <bean id="sqlSessionFactory" class="com.extm.SqlSessionFactoryBeanEx">
       <property name="dataSource" ref="dataSource"></property>
       <property name="configLocation" value="classpath:mybatis.cfg.xml"/>
       <property name="mapperLocations" value="classpath*:com/*/*/sql/*.xml"/>
    </bean>  
    

    将 sqlSessionFactory的class修改为:“com.extm.SqlSessionFactoryBeanEx”

使用说明

extm 单表操作

  1. 获取数据库表对象

     Db.table("表名");
    

    注:下文的针对表的增删改查都基于此方法返回的对象

  2. Select语法(返回列表)

    1.查询全表:

     Db.table("user").select();
    

    2.指定返回字段:

     Db.table("user").fields("id","name","password").select();
    

    3.Where条件:

     (1) 不带参数的where条件: 
         List data = Db.table("user").fields("id","name","password").where("id=1 AND name='meryl'").select();
     (2) 带参数的where条件:
        方式一、 Map map = new HashMap(); 
                  map.put("id","1");
                  map.put("name","meryl");
                  List data = Db.table("user").fields("id","name","password").where("id=#{id} And name=#{name}",map).select();
    
       方式二、 List data = Db.table("user").fields("id","name","password").where("id=#{id} And name=#{name}",1,"meryl").select();
    
  3. Find语法(返回一条数据)

    1. 返回第一条:

       Db.table("user").find();
      
    2. 指定返回字段:

       Db.table("user").fields("id","name","password").find();
      
    3. 指定查询条件:

       3.1 用过参数限定(find参数与以下3.2 where参数一致):
           Map data = Db.table("user").fields("id","name","password").find("id=1 AND name='meryl'");
       3.2 Where条件: 
           (1) 不带参数的where条件: 
               Map data = Db.table("user").fields("id","name","password").where("id=1 AND name='meryl'").find();
           (2) 带参数的where条件:
               方式一、Map map = new HashMap(); 
                       map.put("id","1");
                       map.put("name","meryl");
                       Map data = Db.table("user").fields("id","name","password").where("id=#{id} And name=#{name}",map).find();
               方式二、Map data = Db.table("user").fields("id","name","password").where("id=#{id} And name=#{name}",1,"meryl").find();
      
  4. Count语法(返回记录的条数)

    与Find语法一致

  5. Insert语法(插入数据)

    1. 返回执行成功的记录条数:

       Map map = new HashMap(); 
       map.put("name","meryl");
       map.put("password","1");
       Integer successCount= db("user").insert(map);
      
    2. 返回当前插入的数据的键值

      2.1 针对支持自增长的数据库(Mysql,SqlServer,Sqlite):

       Map map = new HashMap();
       map.put("name","meryl");
       map.put("password","1");
       Long id= db("user").insert(map,"id");  //第二个参数为主键名称,如果返回插入的主键值,这里必传
      

      2.2 针对不支持自增长的数据库(Oracle):

       Map map = new HashMap();
       map.put("name","meryl");
       map.put("password","1");
       Long id= db("user").insert(map,"id","seq_id"); //第二个参数为主键名称,第三参数为序列名称,如果返回插入的主键值,这里必传
      

      注:insert 第一个参数为:要插入的数据,类型可以为自定义实体类型或Map;第二个参数为:主键名称;第三个参数为:序列名称【针对Oracle等不支持自增主键的数据库】)

  6. Update语法(更新数据)

    1. 更新全表

       Map map = new HashMap();
       map.put("name","meryl");
       map.put("password","1");
       Boolean isSuccess = Db.table("user").update(map);
      
    2. 更新指定的记录

       Map map = new HashMap();
       map.put("name","meryl");
       map.put("password","1");
       Boolean isSuccess = Db.table("user").where("id=1").update(map);
      
  7. Delete语法(删除数据)

    1. 删除全表

       Boolean isSuccess = Db.table("user").delete();
      
    2. 删除指定的记录

       2.1 用过参数限定(delete参数与以下2.2 where参数一致):
            Boolean isSuccess = Db.table("user").delete("id=1 AND name='meryl'");
       2.2 Where条件: 
            (1) 不带参数的where条件: 
                Boolean isSuccess = Db.table("user").where("id=1").delete();
            (2) 带参数的where条件:
               方式一、 Map map = new HashMap(); 
                       map.put("id","1");
                       Boolean isSuccess = Db.table("user").where("id=#{id} And name=#{name}",map).delete();
               方式二、  Boolean isSuccess = Db.table("user").where("id=#{id} And name=#{name}",1,"meryl").delete();
      
  8. Fields语法(指定要返回的字段)

  9. Where语法(指定查询条件)

     1. 不带参数的where:
         Db.table("user").where("id=1");或Db.table("user").where("id=#{id}",1);
     2. 带参数的where
         Map map = new HashMap();
         map.put("id","1");
         Db.table("user").where("id=#{id}",map);
    

JDF 魔术方法

db(): 和jdf包下面的getDao()方法相等
db("表名"): 和extm下的Db.table("表名")方法相等

相关文章

网友评论

      本文标题:MyBatis增强工具Extm

      本文链接:https://www.haomeiwen.com/subject/itzdtctx.html