@RequestMapping("user")@RestControllerpublicclassUserController{@AutowiredprivateUserServiceuserService;//查询@GetMapping("select")publicRSselect(){//查询列表List<User>list=userService.select();list=userService.selectBy(Wrapper.where(Cond.gt(User::getId,10)).and(Cond.lt(User::getId,20)));//指定查询list=userService.select(newSelect().column(User::getId,User::getUserName,User::getMobilePhone).where().gt(User::getId,10));//查询一条Useruser=userService.selectById(1L);user=userService.selectOneBy(Wrapper.where(eq(User::getId,1001)));//sql语义化查询《20岁且是女性的用户根据创建时间倒序,获取前10条》list=userService.select(newSelect().column(User::getId,User::getUserName,User::getMobilePhone).where().eq(User::getAge,22).and().eq(User::getGender,0).back().orderByDesc(User::getCreateTime).page(0,10));//联表查询《20岁且是女性的用户根据创建时间倒序,查询前10条用户的信息和地址》Selectselect=newSelect();select.column(User::getId,User::getUserName,User::getMobilePhone,UserAddress::getProvince,UserAddress::getCity,UserAddress::getArea,UserAddress::getDetails);select.innerJoin(UserAddress.class).on().eq(UserAddress::getId,User::getId);select.where().gt(User::getAge,22).and().eq(User::getGender,0);select.orderByDesc(User::getCreateTime);select.page(0,10);//查询MapMap<String,Object>map=userService.selectMap(select);List<Map<String,Object>>mapList=userService.selectMapList(select);returnsuper.successHint("获取成功",list);}//分页@GetMapping("getList")publicMapgetList(HttpServletRequestrequest){// 查询对象Selectselect=newSelect();ReqPageHelper<User>pageHelper=newReqPageHelper<>(request);pageHelper.paging(select,userService);returnpageHelper.toResult("获取列表成功");}//统计(函数使用)@GetMapping("getStatistics")publicResultgetStatistics(){Selectselect=newSelect();select.column(SqlFun.count(User::getId),"count").column(SqlFun.avg(User::getAge));select.where().gt(SqlFun.date_format(User::getCreateTime,"%Y-%m-%d"),"2024-06-24");select.groupBy(User::getGender);select.orderByDesc("count");List<Map<String,Object>>mapList=userService.selectMapList(select);returnResult.success(mapList);}//更新@PostMapping("update")publicRSupdate(Useruser){//根据bean内部id更新longi=userService.updateByBeanId(user);//根据条件更新//i = userService.updateBy(Wrapper.where(Cond.gt(User::getAge, 22)).and(Cond.eq(User::getGender, 1)));//指定更新某个字段 UPDATE user SET gender = 1, name = 'Jovi' ,age = age + 1 WHERE = id = 111userService.update(newUpdate<User>().set(User::getGender,1).set(User::getUserName,"Jovi").setAdd(User::getAge,User::getAge,1).where().eq(User::getId,111).back());if(i>0){returnsuper.successHint("更新成功");}returnsuper.othersHint("更新失败");}//删除@PostMapping("deleteById")publicRSdeleteById(Integer[]id){//根据id删除longi=userService.deleteById(id);//根据条件删除//i = userService.deleteBy(Wrapper.where(gt(User::getAge, 22)).and(eq(User::getGender, 1)));if(i>0){returnsuper.successHint("删除成功");}returnsuper.othersHint("删除失败");}//插入@PostMapping("add")publicRSadd(){List<User>userList=newArrayList<>();for(inti=0;i<100;i++){Useruser=newUser(i,"name"+i);userList.add(user);}userService.insert(userList);returnsuccessHint("成功");}}
SqlBean 1.6.2 发布,Mybatis 自动建表、维护表结构
SqlBean
介绍
SqlBean是一款通过Java语法生成SQL语句的ORM插件,其理念是弱化在开发阶段对数据库的操作,无需先建表再开发,可直接编写实体类和业务代码,项目启动后自动创建表和维护表结构。内置大量常用方法,支持灵活的条件查询,无DAO层,在项目中几乎不需要编写SQL语句,帮助你快速进行业务功能开发,除了支持Mybatis也同时支持Spring Jdbc。
🚀特点: 多数据源, 动态Schema, 读写分离, 自动建表, 自动维护表结构, 联表查询, 乐观锁, 分页
💻环境: JDK8+, Mybatis3.2.4+, (Spring MVC 4.1.2+, Spring Boot 1.x, Spring Boot 2.x)
💿数据库: Mysql, MariaDB, Oracle, Sqlserver2008+, Postgresql, DB2, Derby, Sqlite, HSQL, H2
SqlBean For Android请移步这里👉 gitee(推荐)、 github
SuperCode代码生成项目👉 https://gitee.com/iJovi/supercode
SqlBean使用例子以及代码生成例子点击这里👉 https://gitee.com/iJovi/sqlbean-example
快速开始
1.引入Maven依赖
2.标注实体类
3.无需Dao层,Service层接口只需继承SqlBeanService<实体类, id类型>
4.Service实现类只需继承MybatisSqlBeanServiceImpl<实体类, id类型>和实现你的Service接口
5.Controller层
本次更新内容:
1:修复根据id查询获取id字段的所属类是父类的问题;