通用XML可以让开发只需要配置一个XML模板即可!
传统的Mybatis开发模式是一个实体类的增删改查操作都需要生成一个对应的Mapper.xml文件去定制操作过程,这样更加灵活,但无疑会影响我们的开发效率,例如修改一个字段,需要大量修改XML的内容,在开发周期表结构频繁的变动节奏中,我们需要的是一个通用性更高的方式去使用Ourbaits,也可以说,就像Hibernate那样方便但又不失灵活性!Ourbatis可以满足这一切。
Ourbatis是一款Mybatis增强器,通过配置一个通用的xml模板去定制通用方法:
整个流程中的核心是ourbatis.xml
文件,通过它可以渲染出多个业务类的Mapper.xml文本,然后将之热加载入Mybatis容器中,让我们可以配置一个xml模板,就可以通用的去操作数据库!
Ourbatis系列文章:
Ourbatis项目地址:
Github:https://github.com/ainilili/ourbatis
Gitee:https://gitee.com/ainilili/ourbatis
以Spring Boot 2.0.5.RELEASE
版本为例,在可以正常使用Mybatis的项目中,pom.xml
添加如下依赖:
com.smallnico
ourbatis-spring-boot-starter
${ourbatis.version}
在配置文件中增加一下配置:
ourbatis.domain-locations=实体类所在包名
紧接着,您的Mapper接口继承SimpleMapper
即可
import org.nico.ourbatis.domain.User;
public interface UserMapper extends SimpleMapper{
}
SimpleMapper
提供以下方法:
public T selectById(K key);
public T selectEntity(T condition);
public List selectList(T condition);
public long selectCount(Object condition);
public List selectPage(Page
之后在您的程序里就可以直接使用:
@PostMapping("/test")
@ResponseBody
public String test(){
StringBuilder builder = new StringBuilder();
User user = new User();
user.setAddress(UUID.randomUUID().toString());
user.setAge(18);
user.setBalance(new BigDecimal("10.1"));
user.setCityId(2);
user.setName("Nico");
//插入
int modify = userMapper.insert(user);
builder.append("插入测试:" + modify + System.lineSeparator());
user.setId(null);
modify = userMapper.insertBatch(Arrays.asList(user, user));
builder.append("批量插入测试:" + modify + System.lineSeparator());
user.setId(null);
modify = userMapper.insertSelective(user);
builder.append("插入不为空测试:" + modify + System.lineSeparator());
//查询
user = userMapper.selectById(user.getId());
builder.append("ID查询测试:" + user + System.lineSeparator());
user = userMapper.selectEntity(new User().setAddress(user.getAddress()));
builder.append("实体查询测试:" + user + System.lineSeparator());
List users = userMapper.selectPage(Page.start(1L, 2L));
builder.append("分页查询:" + users + System.lineSeparator());
users = userMapper.selectPage(Page.start(1L, 2L, new User()));
builder.append("分页条件测试:" + users + System.lineSeparator());
users = userMapper.selectPage(Page.start(1L, 2L, "age desc"));
builder.append("分页排序查询:" + users + System.lineSeparator());
users = userMapper.selectPage(Page.start("age desc"));
builder.append("排序测试:" + users + System.lineSeparator());
users = userMapper.selectPage(Page.start(1L, 2L, new User().setName("Nico1"), "age desc"));
builder.append("分页条件排序查询:" + users + System.lineSeparator());
users = userMapper.selectPage(Page.start(new User().setName("Nico1"), "age desc"));
builder.append("条件排序查询:" + users + System.lineSeparator());
PageResult pageResult = userMapper.selectPageResult(Page.start(1L, 2L, new User().setName("Nico1"), "age desc"));
builder.append("ps查询:" + pageResult + System.lineSeparator());
long count = userMapper.selectCount(new User());
builder.append("数量查询测试:" + count + System.lineSeparator());
users = userMapper.selectList(new User().setAddress(user.getAddress()));
builder.append("列表查询测试:" + users + System.lineSeparator());
Integer id = userMapper.selectId(new User().setAddress(user.getAddress()));
builder.append("查询ID测试:" + id + System.lineSeparator());
List ids = userMapper.selectIds(new User().setAddress(user.getAddress()));
builder.append("查询ID列表测试:" + ids + System.lineSeparator());
//更新
user.setAge(19);
modify = userMapper.update(user);
builder.append("更新测试:" + modify + System.lineSeparator());
user.setAge(20);
modify = userMapper.updateBatch(Arrays.asList(user));
builder.append("批量更新测试:" + modify + System.lineSeparator());
modify = userMapper.updateSelective(new User().setId(user.getId()).setAge(100));
builder.append("更新不为空测试:" + modify + System.lineSeparator());
//删除
user.setAge(100);
modify = userMapper.delete(user);
builder.append("删除测试:" + modify + System.lineSeparator());
user.setId(null);
userMapper.insert(user);
modify = userMapper.deleteBatch(Arrays.asList(user.getId()));
builder.append("批量删除测试:" + modify + System.lineSeparator());
user.setId(null);
userMapper.insert(user);
modify = userMapper.deleteById(user.getId());
builder.append("主键删除测试:" + modify + System.lineSeparator());
return builder.toString();
}
该接口测试
插入测试:1
批量插入测试:2
插入不为空测试:1
ID查询测试:User [id=75]
实体查询测试:User [id=72]
分页查询:[User [id=1], User [id=3]]
分页条件测试:[User [id=1], User [id=3]]
分页排序查询:[User [id=11], User [id=10]]
排序测试:[User [id=11], User [id=10], User [id=9], User [id=5], User [id=4], User [id=3], User [id=1], User [id=13], User [id=14], User [id=15], User [id=19], User [id=20], User [id=21], User [id=25], User [id=26], User [id=27], User [id=31], User [id=32], User [id=33], User [id=37], User [id=38], User [id=39], User [id=43], User [id=44], User [id=45], User [id=49], User [id=50], User [id=51], User [id=55], User [id=56], User [id=57], User [id=61], User [id=62], User [id=63], User [id=67], User [id=68], User [id=69], User [id=72], User [id=73], User [id=74], User [id=75]]
分页条件排序查询:[User [id=1]]
条件排序查询:[User [id=1]]
ps查询:PageResult [total=1, results=[User [id=1]]]
数量查询测试:41
列表查询测试:[User [id=72], User [id=73], User [id=74], User [id=75]]
查询ID测试:72
查询ID列表测试:[72, 73, 74, 75]
更新测试:1
批量更新测试:1
更新不为空测试:1
删除测试:1
批量删除测试:1
主键删除测试:1
demo地址:https://github.com/ainilili/ourbatis-simple