目录
在同一个项目中 尽可能避免 多个模块中取同样的类名
没有用foreach标签:
- @Test
- public void test3() {
- int[] ints = {1,2,3,4,5,6};
- //将数组变成字符串
- StringBuffer sb = new StringBuffer();
- for (int i:ints){
- sb.append(",").append(i);
- }
- String s = sb.toString();
- System.out.println(s.substring(1));
- }
结果:
这时候报错 这时就要用到foreach标签了
foreach标签
BookMapper.xml 写配置文件 selectByIn 写好SQL语句 然后写一个对应的方法在接口类中
selectByIn 仿照 selectByPrimaryKey 写
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.cdl.mapper.BookMapper" >
- <resultMap id="BaseResultMap" type="com.cdl.model.Book" >
- <constructor >
- <idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
- constructor>
- resultMap>
- <sql id="Base_Column_List" >
- bid, bname, price
- sql>
-
-
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from t_mvc_book
- where bid = #{bid,jdbcType=INTEGER}
- select>
-
- <select id="selectByIn" resultMap="BaseResultMap" parameterType="java.util.List" >
- select
- <include refid="Base_Column_List" />
- from t_mvc_book
- where bid in
- <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
- #{bid}
- foreach>
- select>
-
-
-
-
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from t_mvc_book
- where bid = #{bid,jdbcType=INTEGER}
- delete>
- <insert id="insert" parameterType="com.cdl.model.Book" >
- insert into t_mvc_book (bid, bname, price
- )
- values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
- )
- insert>
- <insert id="insertSelective" parameterType="com.cdl.model.Book" >
- insert into t_mvc_book
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="bid != null" >
- bid,
- if>
- <if test="bname != null" >
- bname,
- if>
- <if test="price != null" >
- price,
- if>
- trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="bid != null" >
- #{bid,jdbcType=INTEGER},
- if>
- <if test="bname != null" >
- #{bname,jdbcType=VARCHAR},
- if>
- <if test="price != null" >
- #{price,jdbcType=REAL},
- if>
- trim>
- insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.cdl.model.Book" >
- update t_mvc_book
- <set >
- <if test="bname != null" >
- bname = #{bname,jdbcType=VARCHAR},
- if>
- <if test="price != null" >
- price = #{price,jdbcType=REAL},
- if>
- set>
- where bid = #{bid,jdbcType=INTEGER}
- update>
- <update id="updateByPrimaryKey" parameterType="com.cdl.model.Book" >
- update t_mvc_book
- set bname = #{bname,jdbcType=VARCHAR},
- price = #{price,jdbcType=REAL}
- where bid = #{bid,jdbcType=INTEGER}
- update>
- mapper>
BookMapper
- package com.cdl.mapper;
-
- import com.cdl.model.Book;
-
- import java.util.List;
-
- public interface BookMapper {
- int deleteByPrimaryKey(Integer bid);
-
- int insert(Book record);
-
- int insertSelective(Book record);
-
- Book selectByPrimaryKey(Integer bid);
-
- int updateByPrimaryKeySelective(Book record);
-
- int updateByPrimaryKey(Book record);
-
- //通过in关键字进行查询 熟悉foreach标签的使用
- //如果说参数是非实体类 那么记得加上注解 @param,bookIds是对应collection="bookIds"
- List
selectByIn(@Param("bookIds") List bookIds); -
- }
- package com.cdl.biz.impl;
-
- import com.cdl.biz.BookBiz;
- import com.cdl.mapper.BookMapper;
- import com.cdl.model.Book;
-
- import java.util.List;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:49
- */
- public class BookBizImpl implements BookBiz {
-
- private BookMapper bookMapper;
-
- //alt + insert 快速提供set/get/tostring/构造函数
- //alt +enter 快速构建实现类 能够自动补全
-
- public BookMapper getBookMapper() {
- return bookMapper;
- }
-
- public void setBookMapper(BookMapper bookMapper) {
- this.bookMapper = bookMapper;
- }
-
- @Override
- public int deleteByPrimaryKey(Integer bid) {
- return bookMapper.deleteByPrimaryKey(bid);
- }
-
- @Override
- public Book selectByPrimaryKey(Integer bid) {
- return bookMapper.selectByPrimaryKey(bid);
- }
-
- @Override
- public List
selectByIn(List bookIds) { - return bookMapper.selectByIn(bookIds);
- }
- }
测试类:
- package com.cdl.biz.impl;
-
- import com.cdl.mapper.BookMapper;
- import com.cdl.util.SessionUtil;
- import org.apache.ibatis.session.SqlSession;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
-
- import java.util.Arrays;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:55
- */
- public class BookBizImplTest {
-
- private BookBizImpl bookBiz;
- SqlSession sqlSession;
-
- @Before
- public void setUp() throws Exception {
- System.out.println("初始换方法。。。");
- BookBizImpl bookBiz = new BookBizImpl();
- //工具类中获取session对象
- sqlSession = SessionUtil.openSession();
- //从session对象中获取mapper对象
- BookMapper mapper = sqlSession.getMapper(BookMapper.class);
- bookBiz.setBookMapper(mapper);
- this.bookBiz = bookBiz;
- }
-
- @After
- public void tearDown() throws Exception {
- System.out.println("方法测试结束。。");
- }
-
- @Test
- public void deleteByPrimaryKey() {
- }
-
- @Test
- public void selectByPrimaryKey() {
-
- System.out.println("测试的业务方法。。。");
- //System.out.println(bookBiz.getBookMapper());
- System.out.println(bookBiz.selectByPrimaryKey(44));
- }
-
- @Test
- public void test3() {
- int[] ints = {};
- //将数组变成字符串
- StringBuffer sb = new StringBuffer();
- for (int i:ints){
- sb.append(",").append(i);
- }
- String s = sb.toString();
- System.out.println(s.substring(1));
- }
-
- @Test
- public void selectByIn(){
- bookBiz.selectByIn(Arrays.asList(new Integer[]{31,32,33,34})).forEach(System.out::println);
-
- }
-
-
- }
结果:
#{...}
${...}
Concat
注意:#{...}自带引号,${...}有sql注入的风险
BookMapper.xml
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.cdl.mapper.BookMapper" >
- <resultMap id="BaseResultMap" type="com.cdl.model.Book" >
- <constructor >
- <idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
- constructor>
- resultMap>
- <sql id="Base_Column_List" >
- bid, bname, price
- sql>
-
-
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from t_mvc_book
- where bid = #{bid,jdbcType=INTEGER}
- select>
-
- <select id="selectByIn" resultMap="BaseResultMap" parameterType="java.util.List" >
- select
- <include refid="Base_Column_List" />
- from t_mvc_book
- where bid in
- <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
- #{bid}
- foreach>
- select>
-
-
-
-
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from t_mvc_book
- where bid = #{bid,jdbcType=INTEGER}
- delete>
- <insert id="insert" parameterType="com.cdl.model.Book" >
- insert into t_mvc_book (bid, bname, price
- )
- values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
- )
- insert>
- <insert id="insertSelective" parameterType="com.cdl.model.Book" >
- insert into t_mvc_book
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="bid != null" >
- bid,
- if>
- <if test="bname != null" >
- bname,
- if>
- <if test="price != null" >
- price,
- if>
- trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="bid != null" >
- #{bid,jdbcType=INTEGER},
- if>
- <if test="bname != null" >
- #{bname,jdbcType=VARCHAR},
- if>
- <if test="price != null" >
- #{price,jdbcType=REAL},
- if>
- trim>
- insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.cdl.model.Book" >
- update t_mvc_book
- <set >
- <if test="bname != null" >
- bname = #{bname,jdbcType=VARCHAR},
- if>
- <if test="price != null" >
- price = #{price,jdbcType=REAL},
- if>
- set>
- where bid = #{bid,jdbcType=INTEGER}
- update>
- <update id="updateByPrimaryKey" parameterType="com.cdl.model.Book" >
- update t_mvc_book
- set bname = #{bname,jdbcType=VARCHAR},
- price = #{price,jdbcType=REAL}
- where bid = #{bid,jdbcType=INTEGER}
- update>
-
-
- <select id="selectBooksLike1" resultType="com.cdl.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like #{bname}
- select>
- <select id="selectBooksLike2" resultType="com.cdl.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like '${bname}'
- select>
- <select id="selectBooksLike3" resultType="com.cdl.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like concat('%',#{bname},'%')
- select>
-
-
-
-
- mapper>
interface BookMapper
- package com.cdl.mapper;
-
- import com.cdl.model.Book;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- public interface BookMapper {
- int deleteByPrimaryKey(Integer bid);
-
- int insert(Book record);
-
- int insertSelective(Book record);
-
- Book selectByPrimaryKey(Integer bid);
-
- int updateByPrimaryKeySelective(Book record);
-
- int updateByPrimaryKey(Book record);
-
- //通过in关键字进行查询 熟悉foreach标签的使用
- //如果说参数是非实体类 那么记得加上注解 @param,bookIds是对应collection="bookIds"
- List
selectByIn(@Param("bookIds") List bookIds); -
-
- List
selectBooksLike1(@Param("bname") String bname); - List
selectBooksLike2(@Param("bname") String bname); - List
selectBooksLike3(@Param("bname") String bname); - }
interface BookBiz
- package com.cdl.biz;
-
- import com.cdl.model.Book;
-
- import java.util.List;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:00
- */
- public interface BookBiz {
- int deleteByPrimaryKey(Integer bid);
-
-
- Book selectByPrimaryKey(Integer bid);
-
- List
selectByIn( List bookIds); -
- List
selectBooksLike1(String bname); - List
selectBooksLike2(String bname); - List
selectBooksLike3(String bname); - }
BookBizImpl
- package com.cdl.biz.impl;
-
- import com.cdl.biz.BookBiz;
- import com.cdl.mapper.BookMapper;
- import com.cdl.model.Book;
-
- import java.util.List;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:49
- */
- public class BookBizImpl implements BookBiz {
-
- private BookMapper bookMapper;
-
- //alt + insert 快速提供set/get/tostring/构造函数
- //alt +enter 快速构建实现类 能够自动补全
-
- public BookMapper getBookMapper() {
- return bookMapper;
- }
-
- public void setBookMapper(BookMapper bookMapper) {
- this.bookMapper = bookMapper;
- }
-
- @Override
- public int deleteByPrimaryKey(Integer bid) {
- return bookMapper.deleteByPrimaryKey(bid);
- }
-
- @Override
- public Book selectByPrimaryKey(Integer bid) {
- return bookMapper.selectByPrimaryKey(bid);
- }
-
- @Override
- public List
selectByIn(List bookIds) { - return bookMapper.selectByIn(bookIds);
- }
-
-
- public List
selectBooksLike1(String bname){ - return bookMapper.selectBooksLike1(bname);
- }
- public List
selectBooksLike2(String bname){ - return bookMapper.selectBooksLike2(bname);
- }
- public List
selectBooksLike3(String bname){ - return bookMapper.selectBooksLike3(bname);
- }
-
-
- }
测试类 测试:
- package com.cdl.biz.impl;
-
- import com.cdl.mapper.BookMapper;
- import com.cdl.util.SessionUtil;
- import org.apache.ibatis.session.SqlSession;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
-
- import java.util.Arrays;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:55
- */
- public class BookBizImplTest {
-
- private BookBizImpl bookBiz;
- SqlSession sqlSession;
-
- @Before
- public void setUp() throws Exception {
- System.out.println("初始换方法。。。");
- BookBizImpl bookBiz = new BookBizImpl();
- //工具类中获取session对象
- sqlSession = SessionUtil.openSession();
- //从session对象中获取mapper对象
- BookMapper mapper = sqlSession.getMapper(BookMapper.class);
- bookBiz.setBookMapper(mapper);
- this.bookBiz = bookBiz;
- }
-
- @After
- public void tearDown() throws Exception {
- System.out.println("方法测试结束。。");
- }
-
- @Test
- public void deleteByPrimaryKey() {
-
- }
-
- @Test
- public void selectByPrimaryKey() {
-
- System.out.println("测试的业务方法。。。");
- //System.out.println(bookBiz.getBookMapper());
- System.out.println(bookBiz.selectByPrimaryKey(44));
- }
-
- @Test
- public void test3() {
- int[] ints = {1,2,3,4,5};
- //将数组变成字符串
- StringBuffer sb = new StringBuffer();
- for (int i:ints){
- sb.append(",").append(i);
- }
- String s = sb.toString();
- System.out.println(s.substring(1));
- }
-
- @Test
- public void selectByIn(){
- bookBiz.selectByIn(Arrays.asList(new Integer[]{31,32,33,34})).forEach(System.out::println);
- }
-
-
- @Test
- public void selectBooksLike1() {
- //bookBiz.selectBooksLike1(selectBooksLike1:"%圣墟%").forEach(System.out::println);
- bookBiz.selectBooksLike1("%圣墟%").forEach(System.out::println);
- }
-
- @Test
- public void selectBooksLike2() {
- bookBiz.selectBooksLike2("%圣墟%").forEach(System.out::println);
- }
-
- @Test
- public void selectBooksLike3() {
- bookBiz.selectBooksLike3("圣墟").forEach(System.out::println);
- }
- }
结果都一样:
注意
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
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.cdl.mapper.BookMapper" >
- <resultMap id="BaseResultMap" type="com.cdl.model.Book" >
- <constructor >
- <idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
- constructor>
- resultMap>
- <sql id="Base_Column_List" >
- bid, bname, price
- sql>
-
-
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from t_mvc_book
- where bid = #{bid,jdbcType=INTEGER}
- select>
-
- <select id="selectByIn" resultType="com.cdl.model.Book" parameterType="java.util.List" >
- select
- <include refid="Base_Column_List" />
- from t_mvc_book
- where bid in
- <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
- #{bid}
- foreach>
- select>
-
-
-
-
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from t_mvc_book
- where bid = #{bid,jdbcType=INTEGER}
- delete>
- <insert id="insert" parameterType="com.cdl.model.Book" >
- insert into t_mvc_book (bid, bname, price
- )
- values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
- )
- insert>
- <insert id="insertSelective" parameterType="com.cdl.model.Book" >
- insert into t_mvc_book
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="bid != null" >
- bid,
- if>
- <if test="bname != null" >
- bname,
- if>
- <if test="price != null" >
- price,
- if>
- trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="bid != null" >
- #{bid,jdbcType=INTEGER},
- if>
- <if test="bname != null" >
- #{bname,jdbcType=VARCHAR},
- if>
- <if test="price != null" >
- #{price,jdbcType=REAL},
- if>
- trim>
- insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.cdl.model.Book" >
- update t_mvc_book
- <set >
- <if test="bname != null" >
- bname = #{bname,jdbcType=VARCHAR},
- if>
- <if test="price != null" >
- price = #{price,jdbcType=REAL},
- if>
- set>
- where bid = #{bid,jdbcType=INTEGER}
- update>
- <update id="updateByPrimaryKey" parameterType="com.cdl.model.Book" >
- update t_mvc_book
- set bname = #{bname,jdbcType=VARCHAR},
- price = #{price,jdbcType=REAL}
- where bid = #{bid,jdbcType=INTEGER}
- update>
-
-
- <select id="selectBooksLike1" resultType="com.cdl.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like #{bname}
- select>
- <select id="selectBooksLike2" resultType="com.cdl.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like '${bname}'
- select>
- <select id="selectBooksLike3" resultType="com.cdl.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like concat('%',#{bname},'%')
- select>
-
-
- <select id="list1" resultMap="BaseResultMap">
- select * from t_mvc_book
- select>
- <select id="list2" resultType="com.cdl.model.Book">
- select * from t_mvc_book
- select>
- <select id="list3" resultType="com.cdl.model.Book" parameterType="com.cdl.model.BookVo">
- select * from t_mvc_book where bid in
- <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
- #{bid}
- foreach>
- select>
- <select id="list4" resultType="java.util.Map">
- select * from t_mvc_book
- select>
- <select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
- select * from t_mvc_book where bid = #{bid}
- select>
-
-
-
-
-
-
-
-
-
- mapper>
BookVo
- package com.cdl.model;
-
- import java.util.List;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-12 8:48
- */
- public class BookVo extends Book{
-
- private List bookIds;
-
- public List getBookIds() {
- return bookIds;
- }
-
- public void setBookIds(List bookIds) {
- this.bookIds = bookIds;
- }
- }
interface BookMapper
- package com.cdl.mapper;
-
- import com.cdl.model.Book;
- import com.cdl.model.BookVo;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
- import java.util.Map;
-
- public interface BookMapper {
- int deleteByPrimaryKey(Integer bid);
-
- int insert(Book record);
-
- int insertSelective(Book record);
-
- Book selectByPrimaryKey(Integer bid);
-
- int updateByPrimaryKeySelective(Book record);
-
- int updateByPrimaryKey(Book record);
-
- //通过in关键字进行查询 熟悉foreach标签的使用
- //如果说参数是非实体类 那么记得加上注解 @param,bookIds是对应collection="bookIds"
- List
selectByIn(@Param("bookIds") List bookIds); -
-
- List
selectBooksLike1(@Param("bname") String bname); - List
selectBooksLike2(@Param("bname") String bname); - List
selectBooksLike3(@Param("bname") String bname); -
- //list1和list2的结论是:对于单表查询而言,可以用它Resultmap/ReaultType接收 但是多表必须用Resultmap
- List
list1(); - List
list2(); - //如果要传入多个查询参数,必须以对象的方式进行传递
- List
list3(BookVo vo); -
- //说明了返回一条数据还是多条数据 都应该用java.util.Map" 接收
- // 如果是多条数据,那么返回的是list
- List
- Map list5(Map map);
- }
BookBizImpl
- package com.cdl.biz.impl;
-
- import com.cdl.biz.BookBiz;
- import com.cdl.mapper.BookMapper;
- import com.cdl.model.Book;
- import com.cdl.model.BookVo;
-
- import java.util.List;
- import java.util.Map;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:49
- */
- public class BookBizImpl implements BookBiz {
-
- private BookMapper bookMapper;
-
- //alt + insert 快速提供set/get/tostring/构造函数
- //alt +enter 快速构建实现类 能够自动补全
-
- public BookMapper getBookMapper() {
- return bookMapper;
- }
-
- public void setBookMapper(BookMapper bookMapper) {
- this.bookMapper = bookMapper;
- }
-
- @Override
- public int deleteByPrimaryKey(Integer bid) {
- return bookMapper.deleteByPrimaryKey(bid);
- }
-
- @Override
- public Book selectByPrimaryKey(Integer bid) {
- return bookMapper.selectByPrimaryKey(bid);
- }
-
- @Override
- public List
selectByIn(List bookIds) { - return bookMapper.selectByIn(bookIds);
- }
-
-
- public List
selectBooksLike1(String bname){ - return bookMapper.selectBooksLike1(bname);
- }
- public List
selectBooksLike2(String bname){ - return bookMapper.selectBooksLike2(bname);
- }
- public List
selectBooksLike3(String bname){ - return bookMapper.selectBooksLike3(bname);
- }
-
- @Override
- public List
list1() { - return bookMapper.list1();
- }
-
- @Override
- public List
list2() { - return bookMapper.list2();
- }
-
- @Override
- public List
list3(BookVo vo) { - return bookMapper.list3(vo);
- }
-
- @Override
- public List
- return bookMapper.list4();
- }
-
- @Override
- public Map list5(Map map) {
- return bookMapper.list5(map);
- }
-
-
- }
//list1和list2的结论是:对于单表查询而言,可以用Resultmap/ReaultType接收 但是多表必须用Resultmap
BookBizImplTest
- @Test
- public void list1() {
- bookBiz.list1().forEach(System.out::println);
- }
-
- @Test
- public void list2() {
- bookBiz.list2().forEach(System.out::println);
- }
list3
//如果要传入多个查询参数,必须以对象的方式进行传递
- @Test
- public void list3() {
- BookVo vo = new BookVo();
- vo.setBookIds(Arrays.asList(new Integer[]{31,32,33,34}));
- bookBiz.list3(vo).forEach(System.out::println);
- }
list4和list5
//说明了返回一条数据还是多条数据 都应该用java.util.Map" 接收 // 如果是多条数据,那么返回的是list
- @Test
- public void list4() {
- bookBiz.list4().forEach(System.out::println);
- }
- @Test
- public void list5() {
- Map map = new HashMap();
- map.put("bid",32);
- System.out.println(bookBiz.list5(map));
- }
添加pom.xml的文件
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>5.1.2version>
- dependency>
Mybatis.cfg.xml配置拦截器
- <plugins>
-
- <plugin interceptor="com.github.pagehelper.PageInterceptor">
- plugin>
- plugins>
添加进BookMapper.xml
- <select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
- select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
- select>
BookMapper
- //利用第三方插件进行分页
- List
BookBiz
List
BookBizImpl
- @Override
- public List
- return bookMapper.listPager(map);
- }
分析:此时还不能分页 ,因为没有使用MySQL的limit分页 就要使用代码进行分页
PageBean
- package com.cdl.util;
-
- import javax.servlet.http.HttpServletRequest;
- import java.io.Serializable;
- import java.util.Map;
-
- public class PageBean implements Serializable {
-
- private static final long serialVersionUID = 2422581023658455731L;
-
- //页码
- private int page=1;
- //每页显示记录数
- private int rows=10;
- //总记录数
- private int total=0;
- //是否分页
- private boolean isPagination=true;
- //上一次的请求路径
- private String url;
- //获取所有的请求参数
- private Map
map; -
- public PageBean() {
- super();
- }
-
- //设置请求参数
- public void setRequest(HttpServletRequest req) {
- String page=req.getParameter("page");
- String rows=req.getParameter("rows");
- String pagination=req.getParameter("pagination");
- this.setPage(page);
- this.setRows(rows);
- this.setPagination(pagination);
- this.url=req.getContextPath()+req.getServletPath();
- this.map=req.getParameterMap();
- }
- public String getUrl() {
- return url;
- }
-
- public void setUrl(String url) {
- this.url = url;
- }
-
- public Map
getMap() { - return map;
- }
-
- public void setMap(Map
map) { - this.map = map;
- }
-
- public int getPage() {
- return page;
- }
-
- public void setPage(int page) {
- this.page = page;
- }
-
- public void setPage(String page) {
- if(null!=page&&!"".equals(page.trim()))
- this.page = Integer.parseInt(page);
- }
-
- public int getRows() {
- return rows;
- }
-
- public void setRows(int rows) {
- this.rows = rows;
- }
-
- public void setRows(String rows) {
- if(null!=rows&&!"".equals(rows.trim()))
- this.rows = Integer.parseInt(rows);
- }
-
- public int getTotal() {
- return total;
- }
-
- public void setTotal(int total) {
- this.total = total;
- }
-
- public void setTotal(String total) {
- this.total = Integer.parseInt(total);
- }
-
- public boolean isPagination() {
- return isPagination;
- }
-
- public void setPagination(boolean isPagination) {
- this.isPagination = isPagination;
- }
-
- public void setPagination(String isPagination) {
- if(null!=isPagination&&!"".equals(isPagination.trim()))
- this.isPagination = Boolean.parseBoolean(isPagination);
- }
-
- /**
- * 获取分页起始标记位置
- * @return
- */
- public int getStartIndex() {
- //(当前页码-1)*显示记录数
- return (this.getPage()-1)*this.rows;
- }
-
- /**
- * 末页
- * @return
- */
- public int getMaxPage() {
- int totalpage=this.total/this.rows;
- if(this.total%this.rows!=0)
- totalpage++;
- return totalpage;
- }
-
- /**
- * 下一页
- * @return
- */
- public int getNextPage() {
- int nextPage=this.page+1;
- if(this.page>=this.getMaxPage())
- nextPage=this.getMaxPage();
- return nextPage;
- }
-
- /**
- * 上一页
- * @return
- */
- public int getPreivousPage() {
- int previousPage=this.page-1;
- if(previousPage<1)
- previousPage=1;
- return previousPage;
- }
-
- @Override
- public String toString() {
- return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination
- + "]";
- }
- }
BookBiz
List
BookBizImpl
- @Override
- public List
- //分页插件相关代码
- if(pageBean != null && pageBean.isPagination()){
- PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
- }
-
- List
- if(pageBean != null && pageBean.isPagination()){
- //处理结果的前提 是需要分页
- PageInfo info = new PageInfo(maps);
- pageBean.setTotal(info.getTotal()+"");
- }
-
- return maps;
- }
- @Test
- public void listPager() {
- Map map = new HashMap();
- map.put("bname","圣墟");
- // bookBiz.listPager(map).forEach(System.out::println);
-
- //查询出第二页的20条数据
- PageBean pageBean = new PageBean();
- pageBean.setPage(2);
- pageBean.setRows(20);
- bookBiz.listPager(map,pageBean).forEach(System.out::println);
- }
- }
45-65
>(>)
<(<)
&(&)
空格( )
BookMapper.xml
- <select id="list6" resultType="com.cdl.model.Book" parameterType="com.cdl.model.BookVo">
- select * from t_mvc_book
- <where>
- <if test="null != min and min != ''">
-
- if>
- <if test="null != max and max != ''">
- price ]]>
- if>
- where>
- select>
-
- <select id="list7" resultType="com.cdl.model.Book" parameterType="com.cdl.model.BookVo">
- select * from t_mvc_book
- <where>
- <if test="null != min and min != ''">
- and #{min} < price
- if>
- <if test="null != max and max != ''">
- and #{max} > price
- if>
- where>
- select>
BookMapper
- /**
- * 处理特殊字符
- * @param bookVo
- * @return
- */
- List
list6(BookVo bookVo); - List
list7(BookVo bookVo);
BookVo
- package com.cdl.model;
-
- import java.util.List;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-12 8:48
- */
- public class BookVo extends Book{
-
- private List bookIds;
- private int min;
- private int max;
-
- public int getMin() {
- return min;
- }
-
- public void setMin(int min) {
- this.min = min;
- }
-
- public int getMax() {
- return max;
- }
-
- public void setMax(int max) {
- this.max = max;
- }
-
- public List getBookIds() {
- return bookIds;
- }
-
- public void setBookIds(List bookIds) {
- this.bookIds = bookIds;
- }
- }
BookBiz
- /**
- * 特殊字符处理
- * @param bookVo
- * @return
- */
- List
list6(BookVo bookVo); - List
list7(BookVo bookVo);
BookBizImpl
- @Override
- public List
list6(BookVo bookVo) { - return bookMapper.list6(bookVo);
- }
-
- @Override
- public List
list7(BookVo bookVo) { - return bookMapper.list7(bookVo);
- }
- @Test
- public void list6() {
- BookVo bookVo = new BookVo();
- bookVo.setMax(45);
- bookVo.setMax(35);
- bookBiz.list6(bookVo).forEach(System.out::println);
- }
-
- @Test
- public void list7() {
- BookVo bookVo = new BookVo();
- bookVo.setMax(45);
- bookVo.setMax(35);
- bookBiz.list6(bookVo).forEach(System.out::println);
- }
- }