SpringBoot结合Mybatis
标签(空格分隔): PageHelper Mybatis POI
老规矩还是结合之前的学生demo做例子。
Mybatis应用
- 依赖添加
个人认为mybatis比较适合多条件查询的方式.这里不希望引战,最终选择以项目为主或者你的BOSS,我的BOSS是数据库行家,所以喜欢将sql掌握在自己手中,所以之前的jpa方案衍生了成mybatis,写个demo分享给大家.
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
个人习惯吧,dao和mapper都属数据库操作所以放在repository包下.
- 非映射
应用入口添加扫描包地址.创建映射接口
<!--application-->
@MapperScan(basePackages="com.xiaojinzi.repository.mapper")
<!--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);
<!--dao-->
@Autowired
private StuInformationMapper stuInformationMapper;
public StuInformation findOne(String stuid){
return stuInformationMapper.findOne(stuid);
}
<!--service-->
/** Mybatis操作 .*/
/** 查询单个 .*/
StuInformation findByMbOne(String stuid);
<!--serviceimpl-->
@Override
public StuInformation findByMbOne(String stuid) {
return stuInformationDao.findOne(stuid);
}
<!--controller-->
/**
* mybatis 单个查询
* @param stuid
* @return
*/
@GetMapping("/find/{stuid}")
public ResultVo findByMbOne(@PathVariable String stuid){
StuInformation result = stuInfomationService.findByMbOne(stuid);
return ResultVoUtil.success(result);
}
单查询
PageHelper分页
对于分页最好的方式是封装一个分页utils,这里就没做封装了.
- 映射
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
个人习惯吧,dao和mapper都属数据库操作所以放在repository包下.mybatis特色映射文件放到
<!--映射文件路径-->
mybatis:
mybatis.mapper-locations: classpath:/mybatis/mapper/*Mapper.xml
config-location: classpath:/mybatis/mybatis-config.xml
<!--mapper-->
/**
* 组合条件查询
* @param stuName
* @param minAge
* @param maxAge
* @return
*/
List<StuInformation> findByCondition(@Param("stuName") String stuName,@Param("minAge") Integer minAge,@Param("maxAge") Integer maxAge);
<!--dao-->
public Map<String,Object> findByCondition(Integer page,Integer size,String stuName,Integer minAge,Integer maxAge){
Map<String,Object> map = new HashMap<>();
PageHelper.startPage(page,size);
List<StuInformation> list = stuInformationMapper.findByCondition(stuName,minAge,maxAge);
Page<StuInformation> listCountary = (Page<StuInformation>)list;
Long count = listCountary.getTotal();
map.put("total",count);
map.put("data",list);
return map;
}
<!--controller-->
/**
* 多条件组合加分页
* @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<String,Object> map = stuInfomationService.findByCondition(page,size,stuName,minAge,maxAge);
return ResultVoUtil.success(map.get("data"));
}
<!--mybatis-config.xml-->
<!--resource下mybatis-->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<typeAlias type="com.xiaojinzi.dataobject.StuInformation" alias="StuInformation" />
</typeAliases>
<plugins>
<!--mybatis分页插件-->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<property name="offsetAsPageNum" value="false"/>
<property name="rowBoundsWithCount" value="false"/>
<property name="pageSizeZero" value="true"/>
<property name="reasonable" value="false"/>
<property name="supportMethodsArguments" value="false"/>
<property name="returnPageInfo" value="none"/>
</plugin>
</plugins>
<mappers>
<mapper resource="mybatis/mapper/StuInformationMapper.xml"/>
</mappers>
</configuration>
<!--resource下mapper-->
<!--StuInformationMapper.xml-->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xiaojinzi.repository.mapper.StuInformationMapper">
<resultMap id="stuInformationResult" type="com.xiaojinzi.dataobject.StuInformation">
<id column="stu_id" property="stuId"/>
<id column = "stu_name" property = "stuName"/>
<id column = "stu_sex" property = "stuSex" />
<id column = "stu_magor" property = "stuMagor" />
<id column = "stu_age" property = "stuAge" />
<id column = "stu_grade" property = "stuGrade" />
<id column = "stu_department" property = "stuDepartment" />
<id column = "stu_class" property = "stuClass" />
</resultMap>
<!--组合条件查询-->
<select id="findByCondition" resultMap="stuInformationResult" resultType="com.xiaojinzi.dataobject.StuInformation">
select * from stu_information where 1=1
<if test="stuName!=null">
and stu_name= #{stuName,jdbcType=VARCHAR}
</if>
<choose>
<when test="maxAge!=null">
<if test="minAge!=null">
and stu_age BETWEEN #{minAge,jdbcType=BIGINT} AND #{maxAge,jdbcType=BIGINT}
</if>
</when>
<otherwise>
<if test="minAge!=null">
and stu_age >= #{minAge,jdbcType=BIGINT}
</if>
</otherwise>
</choose>
</select>
</mapper>
多条件查询
- 本篇博客撰写人: XiaoJinZi 转载请注明出处
- 关于POIExcel请关注博客POIEXCEL导出
- 学生能力有限 附上邮箱: 986209501@qq.com 不足以及误处请大佬指责
网友评论