目录
1.if:条件语句


imp代码层: 
与xml代码对应mapper:

在biz层写入相应的代码:

进行测试:

结果
完整代码xml:
- <select id="selectByIn" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from t_mvc_book
- where bid in
- <foreach collection="bookIds" open="(" close= ")" separator="," item="bid">
- #{bid}
- foreach>
- select>
1.#{...}

mapper:

biz:
impl:
测试:

结果
2.${...}
3.Concat
接下来下面这两个亦是如此





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



1.resultMap
适合使用返回值是自定义实体类的情况
2.resultType
适合使用返回值的数据类型是非自定义的,即jdk的提供的类型
注意事项:使用resultMap返回自定义类型集合
使用resultType返回List
使用resultType返回单个对象
使用resultType返回List
使用resultType返回Map
xml:
- <select id="list1" resultMap="BaseResultMap">
- select * from t_mvc_book
- select>
- <select id="list2" resultType="com.hmj.model.Book">
- select * from t_mvc_book
- select>
- <select id="list3" resultType="com.hmj.model.Book" parameterType="com.hmj.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:
- // list1/list2的结论是,对于单表查询而言,可以用resultmap/resultType接收,但是多表必须用ResultMap接收
- List
list1(); - List
list2(); - // 如果要传入多个查询参数,必须以对象的方式进行传递
- List
list3(BookVo vo); - List
- Map list5(Map map);
biz:
- List
list1(); - List
list2(); - List
list3(BookVo vo); - List
- Map list5(Map map);
impl:
-
- @Override
- public List<Book> list1() {
- return bookMapper.list1();
- }
-
- @Override
- public List<Book> list2() {
- return bookMapper.list2();
- }
-
- @Override
- public List<Book> list3(BookVo vo) {
- return bookMapper.list3(vo);
- }
-
- @Override
- public List<Map> list4() {
- return bookMapper.list4();
- }
-
- @Override
- public Map list5(Map map) {
- return bookMapper.list5(map);
- }
test测试类:
- @Test
- public void list1() {
- bookBiz.list1().forEach(System.out::println);
- }
-
- @Test
- public void list2() {
- bookBiz.list2().forEach(System.out::println);
- }
-
- @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);
- }
-
- @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));
- }
1.为什么要重写mybatis的分页
Mybatis的分页功能很弱,它是基于内存的分页(查出所有记录再按偏移量offset和边界limit取结果),在大数据量的情况下这样的分页基本上是没有用的
2.使用分页插件的步骤
导入pom依赖
Mybatis.cfg.xml配置拦截器
使用PageHelper进行分页
处理分页结果
Pom依赖
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>5.1.2version>
- dependency>
Mybatis.cfg.xml配置拦截器
- <plugins>
-
- <plugin interceptor="com.github.pagehelper.PageInterceptor">
- plugin>
- plugins>
使用分页插件
- <select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
- select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
- select>
Mapper层
List
Service层
- List
-
- @Override
- public List
- if(pageBean != null && pageBean.isPagination()){
- PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
- }
- List
- if(pageBean != null && pageBean.isPagination()){
- PageInfo pageInfo = new PageInfo(list);
- System.out.println("页码:"+pageInfo.getPageNum());
- System.out.println("页大小:"+pageInfo.getPageSize());
- System.out.println("总记录:"+pageInfo.getTotal());
- pageBean.setTotal(pageInfo.getTotal()+"");
- }
- return list;
- }

>(>)
<(<)
&(&)
空格( )
相关代码配置
- <select id="list6" resultType="com.hmj.model.Book" parameterType="com.hmj.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.hmj.model.Book" parameterType="com.hmj.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>
Mapper
- /**
- * 处理特殊字符
- * @param bookVo
- * @return
- */
- List
list6(BookVo bookVo); -
-
- /**
- * 处理特殊字符
- * @param bookVo
- * @return
- */
- List
list7(BookVo bookVo);


结果
list6
list 7
