• mybatis02


    目录

    一,mybatis动态sql

     2.foreach

    二,模糊查询

    三,查询返回结果集的处理

    四,分页查询

    五,特殊字符处理 


    一,mybatis动态sql

    1.if:条件语句

     2.foreach

     imp代码层: 

     与xml代码对应mapper:

     

      在biz层写入相应的代码:

    进行测试:

     

     结果

      完整代码xml:

    1. <select id="selectByIn" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    2. select
    3. <include refid="Base_Column_List" />
    4. from t_mvc_book
    5. where bid in
    6. <foreach collection="bookIds" open="(" close= ")" separator="," item="bid">
    7. #{bid}
    8. foreach>
    9. select>

    二,模糊查询

      1.#{...}

    mapper:

     biz:

     impl: 

     测试:

    结果

        2.${...}

        3.Concat

    接下来下面这两个亦是如此

     

     

     但是它们三个的SQL语句都有所不同

     

     

     

     

    三,查询返回结果集的处理

     1.resultMap
                    适合使用返回值是自定义实体类的情况

     2.resultType
                    适合使用返回值的数据类型是非自定义的,即jdk的提供的类型

     注意事项:使用resultMap返回自定义类型集合

                       使用resultType返回List

                       使用resultType返回单个对象

                       使用resultType返回List,适用于多表查询返回结果集

                       使用resultType返回Map,适用于多表查询返回单个结果集

    xml:

    1. <select id="list1" resultMap="BaseResultMap">
    2. select * from t_mvc_book
    3. select>
    4. <select id="list2" resultType="com.hmj.model.Book">
    5. select * from t_mvc_book
    6. select>
    7. <select id="list3" resultType="com.hmj.model.Book" parameterType="com.hmj.model.BookVo">
    8. select * from t_mvc_book where bid in
    9. <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
    10. #{bid}
    11. foreach>
    12. select>
    13. <select id="list4" resultType="java.util.Map">
    14. select * from t_mvc_book
    15. select>
    16. <select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
    17. select * from t_mvc_book where bid = #{bid}
    18. select>

    mapper:

    1. // list1/list2的结论是,对于单表查询而言,可以用resultmap/resultType接收,但是多表必须用ResultMap接收
    2. List list1();
    3. List list2();
    4. // 如果要传入多个查询参数,必须以对象的方式进行传递
    5. List list3(BookVo vo);
    6. List list4();
    7. Map list5(Map map);

    biz:

    1. List list1();
    2. List list2();
    3. List list3(BookVo vo);
    4. List list4();
    5. Map list5(Map map);

    impl:

    1. @Override
    2. public List<Book> list1() {
    3. return bookMapper.list1();
    4. }
    5. @Override
    6. public List<Book> list2() {
    7. return bookMapper.list2();
    8. }
    9. @Override
    10. public List<Book> list3(BookVo vo) {
    11. return bookMapper.list3(vo);
    12. }
    13. @Override
    14. public List<Map> list4() {
    15. return bookMapper.list4();
    16. }
    17. @Override
    18. public Map list5(Map map) {
    19. return bookMapper.list5(map);
    20. }

    test测试类:

    1. @Test
    2. public void list1() {
    3. bookBiz.list1().forEach(System.out::println);
    4. }
    5. @Test
    6. public void list2() {
    7. bookBiz.list2().forEach(System.out::println);
    8. }
    9. @Test
    10. public void list3() {
    11. BookVo vo = new BookVo();
    12. vo.setBookIds(Arrays.asList(new Integer[]{31,32,33,34}));
    13. bookBiz.list3(vo).forEach(System.out::println);
    14. }
    15. @Test
    16. public void list4() {
    17. bookBiz.list4().forEach(System.out::println);
    18. }
    19. @Test
    20. public void list5() {
    21. Map map = new HashMap();
    22. map.put("bid",32);
    23. System.out.println(bookBiz.list5(map));
    24. }

    四,分页查询

       1.为什么要重写mybatis的分页
      Mybatis的分页功能很弱,它是基于内存的分页(查出所有记录再按偏移量offset和边界limit取结果),在大数据量的情况下这样的分页基本上是没有用的

         2.使用分页插件的步骤
    导入pom依赖
    Mybatis.cfg.xml配置拦截器
    使用PageHelper进行分页
    处理分页结果

     Pom依赖

    1. <dependency>
    2. <groupId>com.github.pagehelpergroupId>
    3. <artifactId>pagehelperartifactId>
    4. <version>5.1.2version>
    5. dependency>

    Mybatis.cfg.xml配置拦截器

    1. <plugins>
    2. <plugin interceptor="com.github.pagehelper.PageInterceptor">
    3. plugin>
    4. plugins>

    使用分页插件

    1. <select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
    2. select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
    3. select>

    Mapper层

    List listPager(Map map);

    Service层

    1. List listPager(Map map, PageBean pageBean);
    1. @Override
    2. public List listPager(Map map, PageBean pageBean) {
    3. if(pageBean != null && pageBean.isPagination()){
    4. PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
    5. }
    6. List list = bookMapper.listPager(map);
    7. if(pageBean != null && pageBean.isPagination()){
    8. PageInfo pageInfo = new PageInfo(list);
    9. System.out.println("页码:"+pageInfo.getPageNum());
    10. System.out.println("页大小:"+pageInfo.getPageSize());
    11. System.out.println("总记录:"+pageInfo.getTotal());
    12. pageBean.setTotal(pageInfo.getTotal()+"");
    13. }
    14. return list;
    15. }

    五,特殊字符处理 

    >(>)   

        <(<)  

        &(&)

     空格( )

     

    相关代码配置

    1. <select id="list6" resultType="com.hmj.model.Book" parameterType="com.hmj.model.BookVo">
    2. select * from t_mvc_book
    3. <where>
    4. <if test="null != min and min != ''">
    5. if>
    6. <if test="null != max and max != ''">
    7. price ]]>
    8. if>
    9. where>
    10. select>
    11. <select id="list7" resultType="com.hmj.model.Book" parameterType="com.hmj.model.BookVo">
    12. select * from t_mvc_book
    13. <where>
    14. <if test="null != min and min != ''">
    15. and #{min} < price
    16. if>
    17. <if test="null != max and max != ''">
    18. and #{max} > price
    19. if>
    20. where>
    21. select>

    Mapper

    1. /**
    2. * 处理特殊字符
    3. * @param bookVo
    4. * @return
    5. */
    6. List list6(BookVo bookVo);
    7. /**
    8. * 处理特殊字符
    9. * @param bookVo
    10. * @return
    11. */
    12. List list7(BookVo bookVo);

     结果

     list6

    list 7

     

  • 相关阅读:
    软件测试基础——功能测试,软件测试提升完整指南
    EMP.DLL是什么东西?怎么解决EMP.DLL文件缺失
    多水站送水桶装水配送员公众号H5开发
    建立量化交易趋势跟踪策略的五个指标
    armv8单独编译Qt的串口模块
    软件设计模式系列之十三——享元模式
    PostgreSQL使用(一)
    HCM 初学 ( 三 ) - 快速输入PA70、PA71 浏览员工信息PA10
    FireMonkey 做界面的一个小技巧
    折叠式菜单怎么做编程,初学编程系统化教程初级1上线
  • 原文地址:https://blog.csdn.net/hmjcxy/article/details/126299800