自我掌控Sql利器-MyBatis

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 不足以及误处请大佬指责
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,694评论 0 4
  • 官方文档 简介 入门 XML配置 XML映射文件 动态SQL Java API SQL语句构建器 日志 一、 JD...
    拾壹北阅读 3,552评论 0 52
  • 官网下载pycharm-professional-2016.2.tar.gz CTRL+SHIFT+T打开终端,c...
    ShortLife阅读 404评论 0 0
  • 注:文章翻译自网络博客。 Using NSURLProtocol forInjecting Test Data S...
    瞎猫与死耗子阅读 615评论 2 0
  • 学习了PMP(项目管理)后,很多知识经过沉淀,转化为技能,在工作中就能随手用上,使得在工作上有更多的方法去解决问题...
    ChengGY阅读 3,149评论 0 3