Extm 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。使用Extm针对单表操作将不再手写SQL,从而提高开发效率。
Extm比当下流行的Mybatis Plus使用更加方便快捷。在使用Extm时,你将不再创建实例类,避免了书写数据实例类的烦恼。
安装教程
- [安装extm] 在Web项目的pop.xml添加extm引用 - <dependency> <groupId>com.github.meryl</groupId> <artifactId>extm</artifactId> <version>1.0.0</version> </dependency>
- 修改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 单表操作
- 获取数据库表对象 - Db.table("表名");- 注:下文的针对表的增删改查都基于此方法返回的对象 
- 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();
- Find语法(返回一条数据) - 返回第一条: - Db.table("user").find();
- 指定返回字段: - Db.table("user").fields("id","name","password").find();
- 指定查询条件: - 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();
 
- Count语法(返回记录的条数) - 与Find语法一致 
- Insert语法(插入数据) - 返回执行成功的记录条数: - Map map = new HashMap(); map.put("name","meryl"); map.put("password","1"); Integer successCount= db("user").insert(map);
- 返回当前插入的数据的键值 - 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等不支持自增主键的数据库】) 
 
- Update语法(更新数据) - 更新全表 - Map map = new HashMap(); map.put("name","meryl"); map.put("password","1"); Boolean isSuccess = Db.table("user").update(map);
- 更新指定的记录 - Map map = new HashMap(); map.put("name","meryl"); map.put("password","1"); Boolean isSuccess = Db.table("user").where("id=1").update(map);
 
- Delete语法(删除数据) - 删除全表 - Boolean isSuccess = Db.table("user").delete();
- 删除指定的记录 - 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();
 
- Fields语法(指定要返回的字段) 
- 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);
 
  
  
  
 
 
  
 
 
 