SpringBoot结合Mybatis
标签(空格分隔): PageHelper Mybatis POI
老规矩还是结合之前的学生demo做例子。
Mybatis应用
- 依赖添加
个人认为mybatis比较适合多条件查询的方式.这里不希望引战,最终选择以项目为主或者你的BOSS,我的BOSS是数据库行家,所以喜欢将sql掌握在自己手中,所以之前的jpa方案衍生了成mybatis,写个demo分享给大家.
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.2.0
个人习惯吧,dao和mapper都属数据库操作所以放在repository包下.
- 非映射
应用入口添加扫描包地址.创建映射接口
@MapperScan(basePackages="com.xiaojinzi.repository.mapper")
/**
* id查询
* @param stuid
* @return
*/
@Select("select * from stu_information where stu_id=#{stuid,jdbcType=VARCHAR}")
@Results({
@Result(column = "stu_id",property = "stuId"),
@Result(column = "stu_name",property = "stuName"),
@Result(column = "stu_sex",property = "stuSex"),
@Result(column = "stu_magor",property = "stuMagor"),
@Result(column = "stu_age",property = "stuAge"),
@Result(column = "stu_grade",property = "stuGrade"),
@Result(column = "stu_department",property = "stuDepartment"),
@Result(column = "stu_class",property = "stuClass")
})
StuInformation findOne(@Param("stuid") String stuid);
@Autowired
private StuInformationMapper stuInformationMapper;
public StuInformation findOne(String stuid){
return stuInformationMapper.findOne(stuid);
}
/** Mybatis操作 .*/
/** 查询单个 .*/
StuInformation findByMbOne(String stuid);
@Override
public StuInformation findByMbOne(String stuid) {
return stuInformationDao.findOne(stuid);
}
/**
* mybatis 单个查询
* @param stuid
* @return
*/
@GetMapping("/find/{stuid}")
public ResultVo findByMbOne(@PathVariable String stuid){
StuInformation result = stuInfomationService.findByMbOne(stuid);
return ResultVoUtil.success(result);
}
PageHelper分页
对于分页最好的方式是封装一个分页utils,这里就没做封装了.
- 映射
com.github.pagehelper
pagehelper
4.1.0
个人习惯吧,dao和mapper都属数据库操作所以放在repository包下.mybatis特色映射文件放到
mybatis:
mybatis.mapper-locations: classpath:/mybatis/mapper/*Mapper.xml
config-location: classpath:/mybatis/mybatis-config.xml
/**
* 组合条件查询
* @param stuName
* @param minAge
* @param maxAge
* @return
*/
List findByCondition(@Param("stuName") String stuName,@Param("minAge") Integer minAge,@Param("maxAge") Integer maxAge);
public Map findByCondition(Integer page,Integer size,String stuName,Integer minAge,Integer maxAge){
Map map = new HashMap<>();
PageHelper.startPage(page,size);
List list = stuInformationMapper.findByCondition(stuName,minAge,maxAge);
Page listCountary = (Page)list;
Long count = listCountary.getTotal();
map.put("total",count);
map.put("data",list);
return map;
}
/**
* 多条件组合加分页
* @param page
* @param size
* @param stuName
* @param minAge
* @param maxAge
* @return
*/
@GetMapping("/find/condition")
public ResultVo findByCondition(@RequestParam(name = "page",defaultValue = "1")Integer page
,@RequestParam(name="size",defaultValue = "10") Integer size
,@RequestParam(name="stuName",required = false)String stuName
,@RequestParam(name="minAge",defaultValue = "1")Integer minAge,@RequestParam(name = "maxAge",required = false)Integer maxAge){
Map map = stuInfomationService.findByCondition(page,size,stuName,minAge,maxAge);
return ResultVoUtil.success(map.get("data"));
}
- 本篇博客撰写人: XiaoJinZi 转载请注明出处
- 关于POIExcel请关注博客POIEXCEL导出
- 学生能力有限 附上邮箱: [email protected] 不足以及误处请大佬指责