anyline相关的介绍:简介 AnyLine,详细的内容请查看官网,主要就就是帮我们做了数据库操作相关的封装,让我们的开发更快,并且,不同于传统的orm框架,他再开始的时候不需要吗,我们做表和实体类的相关的映射,以及大量的配置。
1.查询
1.1简单查询
首先就是就简单的查询一张表的数据
DataSet game = anylineService.querys("game");
里面的game就是对应的你需要查询的数据库表的名字。
1.2条件查询
有时需要带条件的查询就需要构造一个configStore
ConfigStore configStore = anylineService.condition();
configStore.and(Compare.LIKE,"gname",name).and(Compare.GREAT_EQUAL,"gprice",88).and(Compare.IN,"gid",gids).and(Compare.NOT_IN,"gid",14);
configStore.or(Compare.EQUAL,"gname","Contractors");
DataSet game = anylineService.querys("game",configStore);
game.getRows().stream().forEach(System.out::println);
这个configStore就是我们构造的查询条件,里面可以是equal就是等于的意识,like就是模糊匹配的意识,当然还有很多这种,详情可以见下图。
1.3多表联查
有时需要对多表得查询就要构造RunPrepare
//这里inner就是内连接,当然你也可以用left或者是right
RunPrepare build = TableBuilder.init("orders").inner("game", "game.gid=orders.gid").build();
DataSet game = anylineService.querys(build, configStore);
2.插入
2.1单条数据插入
//这里ni创建得对象的成员属性名和表的对应字段要能匹配
Game game = new Game();
game.setGid(21);
game.setGname("元素1");
game.setGprice(67);
long game1 = anylineService.insert("game", game);
System.out.println(game1);
2.2数据批量插入
List<Game> games=new ArrayList<>();
for (int i = 0; i < 20; i++) {
Game game2 = new Game();
game2.setGid(22+i);
game2.setGname("元素"+21+i);
game2.setGprice(67+i);
games.add(game2);
}
anylineService.insert("game",games);
3.删除
3.1条数据删除
ConfigStore configStore = anylineService.condition();
configStore.and(Compare.EQUAL,"gid",21);
long game = anylineService.delete("game", configStore);
System.out.println(game);
4.修改
4.1单条数据修改
// Game game = new Game();
// game.setGid(21);
// game.setGname("元素喀什");
// game.setGprice(67);
// ConfigStore configStore = anylineService.condition();
// configStore.and(Compare.EQUAL,"gid",21);
// anylineService.update("game",game,configStore);
4.2批量修改
这里需要用到dataset对象进行删除
DataSet game = anylineService.querys("game");
List<DataRow> rows = game.getRows();
//这里之所以设置了两个值就是为了好看修改成功没有
rows.get(0).set("GNAME","基督教撒");
rows.get(0).set("GPRICE",9);
//这里需要告诉我们的主键是什么,他修改的时候就会以我们设置的列作为查询条件
String primaryKeyName = commonService.getPrimaryKey("game");
for (DataRow row : rows) {
row.setPrimaryKey(primaryKeyName);
}
System.out.println(game.getRows());
anylineService.update("game",game);
里面有个根据表名获取主键的名方法我就不提供了,我提供sql语句,自己封装在方法里面就可以了,这个实在mapper层做的
@Select("select column_name from information_schema.COLUMNS where TABLE_SCHEMA = (select database()) and TABLE_NAME= #{tableName} and column_key='PRI';")
String getPrimaryKey(@Param("tableName") String tableName);
好了这就是最基本的一些操作,希望对你有帮助。