• Mybatis之动态sql和分页


    目录

    一、IDEA使用注意事项

     二.Mybatis动态SQL

     三、模糊查询

    四、结果集的处理

     五、第三方分页插件集成Mybatis使用

     六、特殊字符处理


    一、IDEA使用注意事项

    在同一个项目中 尽可能避免 多个模块中取同样的类名

     二.Mybatis动态SQL

    没有用foreach标签:

    1. @Test
    2. public void test3() {
    3. int[] ints = {1,2,3,4,5,6};
    4. //将数组变成字符串
    5. StringBuffer sb = new StringBuffer();
    6. for (int i:ints){
    7. sb.append(",").append(i);
    8. }
    9. String s = sb.toString();
    10. System.out.println(s.substring(1));
    11. }

     结果:

     

     这时候报错 这时就要用到foreach标签了

    foreach标签

    BookMapper.xml 写配置文件 selectByIn 写好SQL语句 然后写一个对应的方法在接口类中

    selectByIn 仿照 selectByPrimaryKey 写

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    2. <mapper namespace="com.cdl.mapper.BookMapper" >
    3. <resultMap id="BaseResultMap" type="com.cdl.model.Book" >
    4. <constructor >
    5. <idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
    6. <arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
    7. <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
    8. constructor>
    9. resultMap>
    10. <sql id="Base_Column_List" >
    11. bid, bname, price
    12. sql>
    13. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    14. select
    15. <include refid="Base_Column_List" />
    16. from t_mvc_book
    17. where bid = #{bid,jdbcType=INTEGER}
    18. select>
    19. <select id="selectByIn" resultMap="BaseResultMap" parameterType="java.util.List" >
    20. select
    21. <include refid="Base_Column_List" />
    22. from t_mvc_book
    23. where bid in
    24. <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
    25. #{bid}
    26. foreach>
    27. select>
    28. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    29. delete from t_mvc_book
    30. where bid = #{bid,jdbcType=INTEGER}
    31. delete>
    32. <insert id="insert" parameterType="com.cdl.model.Book" >
    33. insert into t_mvc_book (bid, bname, price
    34. )
    35. values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
    36. )
    37. insert>
    38. <insert id="insertSelective" parameterType="com.cdl.model.Book" >
    39. insert into t_mvc_book
    40. <trim prefix="(" suffix=")" suffixOverrides="," >
    41. <if test="bid != null" >
    42. bid,
    43. if>
    44. <if test="bname != null" >
    45. bname,
    46. if>
    47. <if test="price != null" >
    48. price,
    49. if>
    50. trim>
    51. <trim prefix="values (" suffix=")" suffixOverrides="," >
    52. <if test="bid != null" >
    53. #{bid,jdbcType=INTEGER},
    54. if>
    55. <if test="bname != null" >
    56. #{bname,jdbcType=VARCHAR},
    57. if>
    58. <if test="price != null" >
    59. #{price,jdbcType=REAL},
    60. if>
    61. trim>
    62. insert>
    63. <update id="updateByPrimaryKeySelective" parameterType="com.cdl.model.Book" >
    64. update t_mvc_book
    65. <set >
    66. <if test="bname != null" >
    67. bname = #{bname,jdbcType=VARCHAR},
    68. if>
    69. <if test="price != null" >
    70. price = #{price,jdbcType=REAL},
    71. if>
    72. set>
    73. where bid = #{bid,jdbcType=INTEGER}
    74. update>
    75. <update id="updateByPrimaryKey" parameterType="com.cdl.model.Book" >
    76. update t_mvc_book
    77. set bname = #{bname,jdbcType=VARCHAR},
    78. price = #{price,jdbcType=REAL}
    79. where bid = #{bid,jdbcType=INTEGER}
    80. update>
    81. mapper>
    BookMapper
    1. package com.cdl.mapper;
    2. import com.cdl.model.Book;
    3. import java.util.List;
    4. public interface BookMapper {
    5. int deleteByPrimaryKey(Integer bid);
    6. int insert(Book record);
    7. int insertSelective(Book record);
    8. Book selectByPrimaryKey(Integer bid);
    9. int updateByPrimaryKeySelective(Book record);
    10. int updateByPrimaryKey(Book record);
    11. //通过in关键字进行查询 熟悉foreach标签的使用
    12. //如果说参数是非实体类 那么记得加上注解 @param,bookIds是对应collection="bookIds"
    13. List selectByIn(@Param("bookIds") List bookIds);
    14. }
    1. package com.cdl.biz.impl;
    2. import com.cdl.biz.BookBiz;
    3. import com.cdl.mapper.BookMapper;
    4. import com.cdl.model.Book;
    5. import java.util.List;
    6. /**
    7. * @author cdl
    8. * @site www.cdl.com
    9. * @create 2022-08-10 22:49
    10. */
    11. public class BookBizImpl implements BookBiz {
    12. private BookMapper bookMapper;
    13. //alt + insert 快速提供set/get/tostring/构造函数
    14. //alt +enter 快速构建实现类 能够自动补全
    15. public BookMapper getBookMapper() {
    16. return bookMapper;
    17. }
    18. public void setBookMapper(BookMapper bookMapper) {
    19. this.bookMapper = bookMapper;
    20. }
    21. @Override
    22. public int deleteByPrimaryKey(Integer bid) {
    23. return bookMapper.deleteByPrimaryKey(bid);
    24. }
    25. @Override
    26. public Book selectByPrimaryKey(Integer bid) {
    27. return bookMapper.selectByPrimaryKey(bid);
    28. }
    29. @Override
    30. public List selectByIn(List bookIds) {
    31. return bookMapper.selectByIn(bookIds);
    32. }
    33. }

    测试类:
     

    1. package com.cdl.biz.impl;
    2. import com.cdl.mapper.BookMapper;
    3. import com.cdl.util.SessionUtil;
    4. import org.apache.ibatis.session.SqlSession;
    5. import org.junit.After;
    6. import org.junit.Before;
    7. import org.junit.Test;
    8. import java.util.Arrays;
    9. /**
    10. * @author cdl
    11. * @site www.cdl.com
    12. * @create 2022-08-10 22:55
    13. */
    14. public class BookBizImplTest {
    15. private BookBizImpl bookBiz;
    16. SqlSession sqlSession;
    17. @Before
    18. public void setUp() throws Exception {
    19. System.out.println("初始换方法。。。");
    20. BookBizImpl bookBiz = new BookBizImpl();
    21. //工具类中获取session对象
    22. sqlSession = SessionUtil.openSession();
    23. //从session对象中获取mapper对象
    24. BookMapper mapper = sqlSession.getMapper(BookMapper.class);
    25. bookBiz.setBookMapper(mapper);
    26. this.bookBiz = bookBiz;
    27. }
    28. @After
    29. public void tearDown() throws Exception {
    30. System.out.println("方法测试结束。。");
    31. }
    32. @Test
    33. public void deleteByPrimaryKey() {
    34. }
    35. @Test
    36. public void selectByPrimaryKey() {
    37. System.out.println("测试的业务方法。。。");
    38. //System.out.println(bookBiz.getBookMapper());
    39. System.out.println(bookBiz.selectByPrimaryKey(44));
    40. }
    41. @Test
    42. public void test3() {
    43. int[] ints = {};
    44. //将数组变成字符串
    45. StringBuffer sb = new StringBuffer();
    46. for (int i:ints){
    47. sb.append(",").append(i);
    48. }
    49. String s = sb.toString();
    50. System.out.println(s.substring(1));
    51. }
    52. @Test
    53. public void selectByIn(){
    54. bookBiz.selectByIn(Arrays.asList(new Integer[]{31,32,33,34})).forEach(System.out::println);
    55. }
    56. }

    结果:

     三、模糊查询

    #{...}

    ${...}

    Concat

    注意:#{...}自带引号,${...}sql注入的风险

    BookMapper.xml

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    2. <mapper namespace="com.cdl.mapper.BookMapper" >
    3. <resultMap id="BaseResultMap" type="com.cdl.model.Book" >
    4. <constructor >
    5. <idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
    6. <arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
    7. <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
    8. constructor>
    9. resultMap>
    10. <sql id="Base_Column_List" >
    11. bid, bname, price
    12. sql>
    13. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    14. select
    15. <include refid="Base_Column_List" />
    16. from t_mvc_book
    17. where bid = #{bid,jdbcType=INTEGER}
    18. select>
    19. <select id="selectByIn" resultMap="BaseResultMap" parameterType="java.util.List" >
    20. select
    21. <include refid="Base_Column_List" />
    22. from t_mvc_book
    23. where bid in
    24. <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
    25. #{bid}
    26. foreach>
    27. select>
    28. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    29. delete from t_mvc_book
    30. where bid = #{bid,jdbcType=INTEGER}
    31. delete>
    32. <insert id="insert" parameterType="com.cdl.model.Book" >
    33. insert into t_mvc_book (bid, bname, price
    34. )
    35. values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
    36. )
    37. insert>
    38. <insert id="insertSelective" parameterType="com.cdl.model.Book" >
    39. insert into t_mvc_book
    40. <trim prefix="(" suffix=")" suffixOverrides="," >
    41. <if test="bid != null" >
    42. bid,
    43. if>
    44. <if test="bname != null" >
    45. bname,
    46. if>
    47. <if test="price != null" >
    48. price,
    49. if>
    50. trim>
    51. <trim prefix="values (" suffix=")" suffixOverrides="," >
    52. <if test="bid != null" >
    53. #{bid,jdbcType=INTEGER},
    54. if>
    55. <if test="bname != null" >
    56. #{bname,jdbcType=VARCHAR},
    57. if>
    58. <if test="price != null" >
    59. #{price,jdbcType=REAL},
    60. if>
    61. trim>
    62. insert>
    63. <update id="updateByPrimaryKeySelective" parameterType="com.cdl.model.Book" >
    64. update t_mvc_book
    65. <set >
    66. <if test="bname != null" >
    67. bname = #{bname,jdbcType=VARCHAR},
    68. if>
    69. <if test="price != null" >
    70. price = #{price,jdbcType=REAL},
    71. if>
    72. set>
    73. where bid = #{bid,jdbcType=INTEGER}
    74. update>
    75. <update id="updateByPrimaryKey" parameterType="com.cdl.model.Book" >
    76. update t_mvc_book
    77. set bname = #{bname,jdbcType=VARCHAR},
    78. price = #{price,jdbcType=REAL}
    79. where bid = #{bid,jdbcType=INTEGER}
    80. update>
    81. <select id="selectBooksLike1" resultType="com.cdl.model.Book" parameterType="java.lang.String">
    82. select * from t_mvc_book where bname like #{bname}
    83. select>
    84. <select id="selectBooksLike2" resultType="com.cdl.model.Book" parameterType="java.lang.String">
    85. select * from t_mvc_book where bname like '${bname}'
    86. select>
    87. <select id="selectBooksLike3" resultType="com.cdl.model.Book" parameterType="java.lang.String">
    88. select * from t_mvc_book where bname like concat('%',#{bname},'%')
    89. select>
    90. mapper>
    interface BookMapper 
    1. package com.cdl.mapper;
    2. import com.cdl.model.Book;
    3. import org.apache.ibatis.annotations.Param;
    4. import java.util.List;
    5. public interface BookMapper {
    6. int deleteByPrimaryKey(Integer bid);
    7. int insert(Book record);
    8. int insertSelective(Book record);
    9. Book selectByPrimaryKey(Integer bid);
    10. int updateByPrimaryKeySelective(Book record);
    11. int updateByPrimaryKey(Book record);
    12. //通过in关键字进行查询 熟悉foreach标签的使用
    13. //如果说参数是非实体类 那么记得加上注解 @param,bookIds是对应collection="bookIds"
    14. List selectByIn(@Param("bookIds") List bookIds);
    15. List selectBooksLike1(@Param("bname") String bname);
    16. List selectBooksLike2(@Param("bname") String bname);
    17. List selectBooksLike3(@Param("bname") String bname);
    18. }
    interface BookBiz
    1. package com.cdl.biz;
    2. import com.cdl.model.Book;
    3. import java.util.List;
    4. /**
    5. * @author cdl
    6. * @site www.cdl.com
    7. * @create 2022-08-10 22:00
    8. */
    9. public interface BookBiz {
    10. int deleteByPrimaryKey(Integer bid);
    11. Book selectByPrimaryKey(Integer bid);
    12. List selectByIn( List bookIds);
    13. List selectBooksLike1(String bname);
    14. List selectBooksLike2(String bname);
    15. List selectBooksLike3(String bname);
    16. }
    BookBizImpl
    1. package com.cdl.biz.impl;
    2. import com.cdl.biz.BookBiz;
    3. import com.cdl.mapper.BookMapper;
    4. import com.cdl.model.Book;
    5. import java.util.List;
    6. /**
    7. * @author cdl
    8. * @site www.cdl.com
    9. * @create 2022-08-10 22:49
    10. */
    11. public class BookBizImpl implements BookBiz {
    12. private BookMapper bookMapper;
    13. //alt + insert 快速提供set/get/tostring/构造函数
    14. //alt +enter 快速构建实现类 能够自动补全
    15. public BookMapper getBookMapper() {
    16. return bookMapper;
    17. }
    18. public void setBookMapper(BookMapper bookMapper) {
    19. this.bookMapper = bookMapper;
    20. }
    21. @Override
    22. public int deleteByPrimaryKey(Integer bid) {
    23. return bookMapper.deleteByPrimaryKey(bid);
    24. }
    25. @Override
    26. public Book selectByPrimaryKey(Integer bid) {
    27. return bookMapper.selectByPrimaryKey(bid);
    28. }
    29. @Override
    30. public List selectByIn(List bookIds) {
    31. return bookMapper.selectByIn(bookIds);
    32. }
    33. public List selectBooksLike1(String bname){
    34. return bookMapper.selectBooksLike1(bname);
    35. }
    36. public List selectBooksLike2(String bname){
    37. return bookMapper.selectBooksLike2(bname);
    38. }
    39. public List selectBooksLike3(String bname){
    40. return bookMapper.selectBooksLike3(bname);
    41. }
    42. }

    测试类 测试:

    1. package com.cdl.biz.impl;
    2. import com.cdl.mapper.BookMapper;
    3. import com.cdl.util.SessionUtil;
    4. import org.apache.ibatis.session.SqlSession;
    5. import org.junit.After;
    6. import org.junit.Before;
    7. import org.junit.Test;
    8. import java.util.Arrays;
    9. /**
    10. * @author cdl
    11. * @site www.cdl.com
    12. * @create 2022-08-10 22:55
    13. */
    14. public class BookBizImplTest {
    15. private BookBizImpl bookBiz;
    16. SqlSession sqlSession;
    17. @Before
    18. public void setUp() throws Exception {
    19. System.out.println("初始换方法。。。");
    20. BookBizImpl bookBiz = new BookBizImpl();
    21. //工具类中获取session对象
    22. sqlSession = SessionUtil.openSession();
    23. //从session对象中获取mapper对象
    24. BookMapper mapper = sqlSession.getMapper(BookMapper.class);
    25. bookBiz.setBookMapper(mapper);
    26. this.bookBiz = bookBiz;
    27. }
    28. @After
    29. public void tearDown() throws Exception {
    30. System.out.println("方法测试结束。。");
    31. }
    32. @Test
    33. public void deleteByPrimaryKey() {
    34. }
    35. @Test
    36. public void selectByPrimaryKey() {
    37. System.out.println("测试的业务方法。。。");
    38. //System.out.println(bookBiz.getBookMapper());
    39. System.out.println(bookBiz.selectByPrimaryKey(44));
    40. }
    41. @Test
    42. public void test3() {
    43. int[] ints = {1,2,3,4,5};
    44. //将数组变成字符串
    45. StringBuffer sb = new StringBuffer();
    46. for (int i:ints){
    47. sb.append(",").append(i);
    48. }
    49. String s = sb.toString();
    50. System.out.println(s.substring(1));
    51. }
    52. @Test
    53. public void selectByIn(){
    54. bookBiz.selectByIn(Arrays.asList(new Integer[]{31,32,33,34})).forEach(System.out::println);
    55. }
    56. @Test
    57. public void selectBooksLike1() {
    58. //bookBiz.selectBooksLike1(selectBooksLike1:"%圣墟%").forEach(System.out::println);
    59. bookBiz.selectBooksLike1("%圣墟%").forEach(System.out::println);
    60. }
    61. @Test
    62. public void selectBooksLike2() {
    63. bookBiz.selectBooksLike2("%圣墟%").forEach(System.out::println);
    64. }
    65. @Test
    66. public void selectBooksLike3() {
    67. bookBiz.selectBooksLike3("圣墟").forEach(System.out::println);
    68. }
    69. }

     结果都一样:

    注意

    MyBatis中#和$的区别
    1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。
       如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by '111', 
           如果传入的值是id,则解析成的sql为order by "id".

    2. $将传入的数据直接显示生成在sql中。
       如:order by $user_id$,如果传入的值是111,那么解析成sql时的值为order by user_id,
           如果传入的值是id,则解析成的sql为order by id.
    3. #方式能够很大程度防止sql注入。
    4. $方式无法防止Sql注入。
    5. $方式一般用于传入数据库对象,例如传入表名. 
    6. 一般能用#的就别用$. 

    四、结果集的处理

    BookMapper.xml

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    2. <mapper namespace="com.cdl.mapper.BookMapper" >
    3. <resultMap id="BaseResultMap" type="com.cdl.model.Book" >
    4. <constructor >
    5. <idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
    6. <arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
    7. <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
    8. constructor>
    9. resultMap>
    10. <sql id="Base_Column_List" >
    11. bid, bname, price
    12. sql>
    13. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    14. select
    15. <include refid="Base_Column_List" />
    16. from t_mvc_book
    17. where bid = #{bid,jdbcType=INTEGER}
    18. select>
    19. <select id="selectByIn" resultType="com.cdl.model.Book" parameterType="java.util.List" >
    20. select
    21. <include refid="Base_Column_List" />
    22. from t_mvc_book
    23. where bid in
    24. <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
    25. #{bid}
    26. foreach>
    27. select>
    28. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    29. delete from t_mvc_book
    30. where bid = #{bid,jdbcType=INTEGER}
    31. delete>
    32. <insert id="insert" parameterType="com.cdl.model.Book" >
    33. insert into t_mvc_book (bid, bname, price
    34. )
    35. values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
    36. )
    37. insert>
    38. <insert id="insertSelective" parameterType="com.cdl.model.Book" >
    39. insert into t_mvc_book
    40. <trim prefix="(" suffix=")" suffixOverrides="," >
    41. <if test="bid != null" >
    42. bid,
    43. if>
    44. <if test="bname != null" >
    45. bname,
    46. if>
    47. <if test="price != null" >
    48. price,
    49. if>
    50. trim>
    51. <trim prefix="values (" suffix=")" suffixOverrides="," >
    52. <if test="bid != null" >
    53. #{bid,jdbcType=INTEGER},
    54. if>
    55. <if test="bname != null" >
    56. #{bname,jdbcType=VARCHAR},
    57. if>
    58. <if test="price != null" >
    59. #{price,jdbcType=REAL},
    60. if>
    61. trim>
    62. insert>
    63. <update id="updateByPrimaryKeySelective" parameterType="com.cdl.model.Book" >
    64. update t_mvc_book
    65. <set >
    66. <if test="bname != null" >
    67. bname = #{bname,jdbcType=VARCHAR},
    68. if>
    69. <if test="price != null" >
    70. price = #{price,jdbcType=REAL},
    71. if>
    72. set>
    73. where bid = #{bid,jdbcType=INTEGER}
    74. update>
    75. <update id="updateByPrimaryKey" parameterType="com.cdl.model.Book" >
    76. update t_mvc_book
    77. set bname = #{bname,jdbcType=VARCHAR},
    78. price = #{price,jdbcType=REAL}
    79. where bid = #{bid,jdbcType=INTEGER}
    80. update>
    81. <select id="selectBooksLike1" resultType="com.cdl.model.Book" parameterType="java.lang.String">
    82. select * from t_mvc_book where bname like #{bname}
    83. select>
    84. <select id="selectBooksLike2" resultType="com.cdl.model.Book" parameterType="java.lang.String">
    85. select * from t_mvc_book where bname like '${bname}'
    86. select>
    87. <select id="selectBooksLike3" resultType="com.cdl.model.Book" parameterType="java.lang.String">
    88. select * from t_mvc_book where bname like concat('%',#{bname},'%')
    89. select>
    90. <select id="list1" resultMap="BaseResultMap">
    91. select * from t_mvc_book
    92. select>
    93. <select id="list2" resultType="com.cdl.model.Book">
    94. select * from t_mvc_book
    95. select>
    96. <select id="list3" resultType="com.cdl.model.Book" parameterType="com.cdl.model.BookVo">
    97. select * from t_mvc_book where bid in
    98. <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
    99. #{bid}
    100. foreach>
    101. select>
    102. <select id="list4" resultType="java.util.Map">
    103. select * from t_mvc_book
    104. select>
    105. <select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
    106. select * from t_mvc_book where bid = #{bid}
    107. select>
    108. mapper>

     BookVo 

    1. package com.cdl.model;
    2. import java.util.List;
    3. /**
    4. * @author cdl
    5. * @site www.cdl.com
    6. * @create 2022-08-12 8:48
    7. */
    8. public class BookVo extends Book{
    9. private List bookIds;
    10. public List getBookIds() {
    11. return bookIds;
    12. }
    13. public void setBookIds(List bookIds) {
    14. this.bookIds = bookIds;
    15. }
    16. }

     interface BookMapper 

    1. package com.cdl.mapper;
    2. import com.cdl.model.Book;
    3. import com.cdl.model.BookVo;
    4. import org.apache.ibatis.annotations.Param;
    5. import java.util.List;
    6. import java.util.Map;
    7. public interface BookMapper {
    8. int deleteByPrimaryKey(Integer bid);
    9. int insert(Book record);
    10. int insertSelective(Book record);
    11. Book selectByPrimaryKey(Integer bid);
    12. int updateByPrimaryKeySelective(Book record);
    13. int updateByPrimaryKey(Book record);
    14. //通过in关键字进行查询 熟悉foreach标签的使用
    15. //如果说参数是非实体类 那么记得加上注解 @param,bookIds是对应collection="bookIds"
    16. List selectByIn(@Param("bookIds") List bookIds);
    17. List selectBooksLike1(@Param("bname") String bname);
    18. List selectBooksLike2(@Param("bname") String bname);
    19. List selectBooksLike3(@Param("bname") String bname);
    20. //list1和list2的结论是:对于单表查询而言,可以用它Resultmap/ReaultType接收 但是多表必须用Resultmap
    21. List list1();
    22. List list2();
    23. //如果要传入多个查询参数,必须以对象的方式进行传递
    24. List list3(BookVo vo);
    25. //说明了返回一条数据还是多条数据 都应该用java.util.Map" 接收
    26. // 如果是多条数据,那么返回的是list
    27. List list4();
    28. Map list5(Map map);
    29. }

     BookBizImpl

    1. package com.cdl.biz.impl;
    2. import com.cdl.biz.BookBiz;
    3. import com.cdl.mapper.BookMapper;
    4. import com.cdl.model.Book;
    5. import com.cdl.model.BookVo;
    6. import java.util.List;
    7. import java.util.Map;
    8. /**
    9. * @author cdl
    10. * @site www.cdl.com
    11. * @create 2022-08-10 22:49
    12. */
    13. public class BookBizImpl implements BookBiz {
    14. private BookMapper bookMapper;
    15. //alt + insert 快速提供set/get/tostring/构造函数
    16. //alt +enter 快速构建实现类 能够自动补全
    17. public BookMapper getBookMapper() {
    18. return bookMapper;
    19. }
    20. public void setBookMapper(BookMapper bookMapper) {
    21. this.bookMapper = bookMapper;
    22. }
    23. @Override
    24. public int deleteByPrimaryKey(Integer bid) {
    25. return bookMapper.deleteByPrimaryKey(bid);
    26. }
    27. @Override
    28. public Book selectByPrimaryKey(Integer bid) {
    29. return bookMapper.selectByPrimaryKey(bid);
    30. }
    31. @Override
    32. public List selectByIn(List bookIds) {
    33. return bookMapper.selectByIn(bookIds);
    34. }
    35. public List selectBooksLike1(String bname){
    36. return bookMapper.selectBooksLike1(bname);
    37. }
    38. public List selectBooksLike2(String bname){
    39. return bookMapper.selectBooksLike2(bname);
    40. }
    41. public List selectBooksLike3(String bname){
    42. return bookMapper.selectBooksLike3(bname);
    43. }
    44. @Override
    45. public List list1() {
    46. return bookMapper.list1();
    47. }
    48. @Override
    49. public List list2() {
    50. return bookMapper.list2();
    51. }
    52. @Override
    53. public List list3(BookVo vo) {
    54. return bookMapper.list3(vo);
    55. }
    56. @Override
    57. public List list4() {
    58. return bookMapper.list4();
    59. }
    60. @Override
    61. public Map list5(Map map) {
    62. return bookMapper.list5(map);
    63. }
    64. }
    //list1和list2的结论是:对于单表查询而言,可以用Resultmap/ReaultType接收 但是多表必须用Resultmap
    
    BookBizImplTest
    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. }

     

     

     

     

     list3

    //如果要传入多个查询参数,必须以对象的方式进行传递
    
    1. @Test
    2. public void list3() {
    3. BookVo vo = new BookVo();
    4. vo.setBookIds(Arrays.asList(new Integer[]{31,32,33,34}));
    5. bookBiz.list3(vo).forEach(System.out::println);
    6. }

     

     

     list4和list5

    //说明了返回一条数据还是多条数据 都应该用java.util.Map" 接收
    // 如果是多条数据,那么返回的是list
    
    1. @Test
    2. public void list4() {
    3. bookBiz.list4().forEach(System.out::println);
    4. }

    1. @Test
    2. public void list5() {
    3. Map map = new HashMap();
    4. map.put("bid",32);
    5. System.out.println(bookBiz.list5(map));
    6. }

     五、第三方分页插件集成Mybatis使用

    添加pom.xml的文件

    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>

    添加进BookMapper.xml

    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>

    BookMapper

    1. //利用第三方插件进行分页
    2. List listPager(Map map);

    BookBiz

    List listPager(Map map);
    BookBizImpl
    1. @Override
    2. public List listPager(Map map) {
    3. return bookMapper.listPager(map);
    4. }

    分析:此时还不能分页 ,因为没有使用MySQL的limit分页 就要使用代码进行分页

    PageBean 

    1. package com.cdl.util;
    2. import javax.servlet.http.HttpServletRequest;
    3. import java.io.Serializable;
    4. import java.util.Map;
    5. public class PageBean implements Serializable {
    6. private static final long serialVersionUID = 2422581023658455731L;
    7. //页码
    8. private int page=1;
    9. //每页显示记录数
    10. private int rows=10;
    11. //总记录数
    12. private int total=0;
    13. //是否分页
    14. private boolean isPagination=true;
    15. //上一次的请求路径
    16. private String url;
    17. //获取所有的请求参数
    18. private Map map;
    19. public PageBean() {
    20. super();
    21. }
    22. //设置请求参数
    23. public void setRequest(HttpServletRequest req) {
    24. String page=req.getParameter("page");
    25. String rows=req.getParameter("rows");
    26. String pagination=req.getParameter("pagination");
    27. this.setPage(page);
    28. this.setRows(rows);
    29. this.setPagination(pagination);
    30. this.url=req.getContextPath()+req.getServletPath();
    31. this.map=req.getParameterMap();
    32. }
    33. public String getUrl() {
    34. return url;
    35. }
    36. public void setUrl(String url) {
    37. this.url = url;
    38. }
    39. public Map getMap() {
    40. return map;
    41. }
    42. public void setMap(Map map) {
    43. this.map = map;
    44. }
    45. public int getPage() {
    46. return page;
    47. }
    48. public void setPage(int page) {
    49. this.page = page;
    50. }
    51. public void setPage(String page) {
    52. if(null!=page&&!"".equals(page.trim()))
    53. this.page = Integer.parseInt(page);
    54. }
    55. public int getRows() {
    56. return rows;
    57. }
    58. public void setRows(int rows) {
    59. this.rows = rows;
    60. }
    61. public void setRows(String rows) {
    62. if(null!=rows&&!"".equals(rows.trim()))
    63. this.rows = Integer.parseInt(rows);
    64. }
    65. public int getTotal() {
    66. return total;
    67. }
    68. public void setTotal(int total) {
    69. this.total = total;
    70. }
    71. public void setTotal(String total) {
    72. this.total = Integer.parseInt(total);
    73. }
    74. public boolean isPagination() {
    75. return isPagination;
    76. }
    77. public void setPagination(boolean isPagination) {
    78. this.isPagination = isPagination;
    79. }
    80. public void setPagination(String isPagination) {
    81. if(null!=isPagination&&!"".equals(isPagination.trim()))
    82. this.isPagination = Boolean.parseBoolean(isPagination);
    83. }
    84. /**
    85. * 获取分页起始标记位置
    86. * @return
    87. */
    88. public int getStartIndex() {
    89. //(当前页码-1)*显示记录数
    90. return (this.getPage()-1)*this.rows;
    91. }
    92. /**
    93. * 末页
    94. * @return
    95. */
    96. public int getMaxPage() {
    97. int totalpage=this.total/this.rows;
    98. if(this.total%this.rows!=0)
    99. totalpage++;
    100. return totalpage;
    101. }
    102. /**
    103. * 下一页
    104. * @return
    105. */
    106. public int getNextPage() {
    107. int nextPage=this.page+1;
    108. if(this.page>=this.getMaxPage())
    109. nextPage=this.getMaxPage();
    110. return nextPage;
    111. }
    112. /**
    113. * 上一页
    114. * @return
    115. */
    116. public int getPreivousPage() {
    117. int previousPage=this.page-1;
    118. if(previousPage<1)
    119. previousPage=1;
    120. return previousPage;
    121. }
    122. @Override
    123. public String toString() {
    124. return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination
    125. + "]";
    126. }
    127. }

    BookBiz

      List listPager(Map map, PageBean pageBean);
    BookBizImpl
    1. @Override
    2. public List listPager(Map map, PageBean pageBean) {
    3. //分页插件相关代码
    4. if(pageBean != null && pageBean.isPagination()){
    5. PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
    6. }
    7. List maps = bookMapper.listPager(map);
    8. if(pageBean != null && pageBean.isPagination()){
    9. //处理结果的前提 是需要分页
    10. PageInfo info = new PageInfo(maps);
    11. pageBean.setTotal(info.getTotal()+"");
    12. }
    13. return maps;
    14. }
    1. @Test
    2. public void listPager() {
    3. Map map = new HashMap();
    4. map.put("bname","圣墟");
    5. // bookBiz.listPager(map).forEach(System.out::println);
    6. //查询出第二页的20条数据
    7. PageBean pageBean = new PageBean();
    8. pageBean.setPage(2);
    9. pageBean.setRows(20);
    10. bookBiz.listPager(map,pageBean).forEach(System.out::println);
    11. }
    12. }

    45-65

     六、特殊字符处理

    >(>)  

        <(<) 

        &(&)

     空格( )

     

    BookMapper.xml

    1. <select id="list6" resultType="com.cdl.model.Book" parameterType="com.cdl.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.cdl.model.Book" parameterType="com.cdl.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>
    BookMapper
    1. /**
    2. * 处理特殊字符
    3. * @param bookVo
    4. * @return
    5. */
    6. List list6(BookVo bookVo);
    7. List list7(BookVo bookVo);

     BookVo 

    1. package com.cdl.model;
    2. import java.util.List;
    3. /**
    4. * @author cdl
    5. * @site www.cdl.com
    6. * @create 2022-08-12 8:48
    7. */
    8. public class BookVo extends Book{
    9. private List bookIds;
    10. private int min;
    11. private int max;
    12. public int getMin() {
    13. return min;
    14. }
    15. public void setMin(int min) {
    16. this.min = min;
    17. }
    18. public int getMax() {
    19. return max;
    20. }
    21. public void setMax(int max) {
    22. this.max = max;
    23. }
    24. public List getBookIds() {
    25. return bookIds;
    26. }
    27. public void setBookIds(List bookIds) {
    28. this.bookIds = bookIds;
    29. }
    30. }

    BookBiz

    1. /**
    2. * 特殊字符处理
    3. * @param bookVo
    4. * @return
    5. */
    6. List list6(BookVo bookVo);
    7. List list7(BookVo bookVo);
    BookBizImpl
    1. @Override
    2. public List list6(BookVo bookVo) {
    3. return bookMapper.list6(bookVo);
    4. }
    5. @Override
    6. public List list7(BookVo bookVo) {
    7. return bookMapper.list7(bookVo);
    8. }
    1. @Test
    2. public void list6() {
    3. BookVo bookVo = new BookVo();
    4. bookVo.setMax(45);
    5. bookVo.setMax(35);
    6. bookBiz.list6(bookVo).forEach(System.out::println);
    7. }
    8. @Test
    9. public void list7() {
    10. BookVo bookVo = new BookVo();
    11. bookVo.setMax(45);
    12. bookVo.setMax(35);
    13. bookBiz.list6(bookVo).forEach(System.out::println);
    14. }
    15. }

     

  • 相关阅读:
    在nodejs中使用typescript
    山东省技能兴鲁网络安全大赛 web方向
    【BP回归预测】改进的鲸鱼算法优化BP神经网络回归预测(多输入单输出)【含Matlab源码 2184期】
    服务器数据恢复- Ext4文件系统分区挂载报错的数据恢复案例
    514. 自由之路
    Java并发编程 | 从进程、线程到并发问题实例解决
    Auracast 广播音频知识点
    期货开户公司的选择和作用
    美国FBA海运详解:美国FBA海运费用价格有哪些
    用ChatGPT编写一个词卡显示网页
  • 原文地址:https://blog.csdn.net/weixin_62735525/article/details/126290653