目录
同一个project,尽量避免多个module中取同样的类名
现在if可以不用在代码中拼接,在配置文件中进行配置
图例:

假设将一个数组变成字符串,也就是和以下方法相同
- public void test3(){
- int [] ints ={1,2,3,4,5,6};
- // 将数组变成字符串 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));
- }
效果:

可以看到效果和想要的效果相同,那如果数组为空呢? 会提示越界了,然而这个时候Mybatis提供了一个foreach标签
先在BookMapper.xml中将sql语句写出来
- <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>

l 再去BookMapper接口中将方法写出
- // 通过in关键字进行查询,讲解 foreach标签的使用
- // 如果是参数 是非实体类(book,Order,....),那么记得加上注解 @param,bookIds是对应collection属性的
- List
selectByIn(@Param("bookIds") List bookIds);

加入到BookBiz和实现类中,然后进行测试
BookBiz:
List selectByIn(List bookIds);

BookBizImpl:
- @Override
- public List
selectByIn(List bookIds) { - return bookMapper.selectByIn(bookIds);
- }

测试类中:
- @Test
- public void selectByIn() {
- List
bookIds = Arrays.asList(new Integer[]{31,32,33,34}); - bookBiz.selectByIn(bookIds).forEach(System.out::println);
- }

效果:

注意:#{...}自带引号,${...}有sql注入的风险
将以下代码段加入到BookMapper.xml中
- <select id="selectBooksLike1" resultType="com.mgy.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like #{bname}
- select>
- <select id="selectBooksLike2" resultType="com.mgy.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like '${bname}'
- select>
- <select id="selectBooksLike3" resultType="com.mgy.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
- select>
再将以下方法加入到BookMapper接口中
- List
selectBooksLike1(@Param("bname") String bname); - List
selectBooksLike2(@Param("bname") String bname); - List
selectBooksLike3(@Param("bname") String bname);
将以下方法加入到BookBiz:
- List
selectBooksLike1(String bname); -
- List
selectBooksLike2(String bname); -
- List
selectBooksLike3(String bname);
实现类:
- 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);
- }
测试类:
- @Test
- public void selectBooksLike1() {
- bookBiz.selectBooksLike1("%圣墟%").forEach(System.out::println);
- }
-
- @Test
- public void selectBooksLike2() {
- bookBiz.selectBooksLike2("%圣墟%").forEach(System.out::println);
- }
-
- @Test
- public void selectBooksLike3() {
- bookBiz.selectBooksLike2("圣墟").forEach(System.out::println);
- }
selectctBooksLike1方法的效果:

selectctBooksLike2方法的效果:

selectctBooksLike3方法的效果:

方法三才是模糊查询的代码块,而方法一和方法二是为了做一个对比。
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中
-
- <select id="list1" resultMap="BaseResultMap">
- select * from t_mvc_book
- select>
-
- <select id="list2" resultType="com.mgy.model.Book">
- select * from t_mvc_book
- select>
- <select id="list3" resultType="com.mgy.model.Book" parameterType="com.mgy.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>
自定义mvc中的
baseDao
executeQuery-->List
-->单表查询--->Mybaits中的ResultType executeQuery-->List
如果是单表的情况下,ResultType和ResultMap都可以用
而list4和list5说明了不管是返回一条数据,还是多条数据,都应该用java.util.Map进行接受,如果是一条数据,那么返回值是Map,如果是多条数据,那么返回值是List
BookMapper中加入以下代码块:
- // list1 list2的结论是:对于单表查询而言,可以用它Resultmap/resultType接受,但是多表必须用Resultmap接受
- List
list1(); - List
list2(); - // 如果要传入多个查询参数,必须以对象的方式进行传递
- // 举例:select * from t_mvc_book where
- // bid in(1,2,3,4,5,6) and banme in("圣墟","不死不灭");
- List
list3(BookVo vo); - // 说明了不管是返回一条数据,还是多条数据,都应该用java.util.Map进行接受,
- // 如果是一条数据,那么返回值是Map,如果是多条数据,那么返回值是List
- List
- Map list5(Map map);
加入到BookBiz中:
- List
list1(); - List
list2(); - List
list3(BookVo vo); - List
- Map list5(Map map);
BookVo:
- package com.mgy.model;
-
- import java.util.List;
-
- /**
- * @author 云鹤衫
- * @site www.yunheshan.com
- * @company xxx公司
- * @create 2022-08-11 22:30
- */
- public class BookVo extends Book{
- private List bookIds;
-
- public List getBookIds() {
- return bookIds;
- }
-
- public void setBookId(List bookIds) {
- this.bookIds = bookIds;
- }
- }
加入到实现类中:
- @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);
- }
生成到测试类中
- @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.setBookId(Arrays.asList(new Integer[]{31,32,33,34}));
- bookBiz.list3(vo).forEach(System.out::print);
- }
-
- @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));
- }
list1的效果:

list2的效果:

根据list1和list2的效果对比可以知道list1和list2的结论是为正确的
list3的效果:

可以看到list3的效果和之前写的selectByIn该方法的效果一样,如果要传多个参数,只要BookVo中添加属性即可
list4的效果:

list5的效果:
总结:
结论1:对于单表查询而言,Resultmap和ResultType是没有区别的,但是对于多表而言是有区别的
结论2:如果要传递多个参数到后台,就把这些参数封装到一个对象中
结论3:不管返回集合还是单条数据,xml文件配置的都是java.util.map而不是Java.util.list
将以下依赖加入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>
切记一定要加入到typeAliases标签之后

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
PageBean:
- package com.mgy.util;
-
- import java.io.Serializable;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
-
- 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
实现类:
- @Override
- public List<Map> listPager(Map map, PageBean pageBean) {
- // pageHelper分页插件相关代码
- if(pageBean !=null && pageBean.isPagination()){
- PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
- }
-
- List<Map> maps = bookMapper.listPager(map);
-
- 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","圣墟");
-
- // 查询出第二页的20条数据
- PageBean pageBean=new PageBean();
- pageBean.setPage(2);
- pageBean.setRows(20);
-
- bookBiz.listPager(map,pageBean).forEach(System.out::println);
- }
效果:

被CDATA所包裹的特殊字符,都会被转译成sql语句中的字符
BookMapper.xml:
- <select id="list6" resultType="com.javaxl.model.Book" parameterType="com.javaxl.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.javaxl.model.Book" parameterType="com.javaxl.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); -
-
- /**
- * 处理特殊字符
- * @param bookVo
- * @return
- */
- List
list7(BookVo bookVo);
BookVo
- package com.mgy.model;
-
- import java.util.List;
-
- /**
- * @author 云鹤衫
- * @site www.yunheshan.com
- * @company xxx公司
- * @create 2022-08-11 22:30
- */
- public class BookVo extends Book{
- private List bookIds;
- private int min;
- private int max;
-
- public int getMax() {
- return max;
- }
-
- public void setMax(int max) {
- this.max = max;
- }
-
- public int getMin() {
- return min;
- }
-
- public void setMin(int min) {
- this.min = min;
- }
-
- public void setBookIds(List bookIds) {
- this.bookIds = bookIds;
- }
-
- public List getBookIds() {
- return bookIds;
- }
-
- public void setBookId(List bookIds) {
- this.bookIds = bookIds;
- }
- }
BookBiz
- List
list6(BookVo bookVo); -
- List
list7(BookVo bookVo);
实现类:
- @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 vo=new BookVo();
- vo.setMax(45);
- vo.setMin(35);
- bookBiz.list6(vo).forEach(System.out::println);
- }
-
- @Test
- public void list7() {
- BookVo vo=new BookVo();
- vo.setMax(45);
- vo.setMin(35);
- bookBiz.list7(vo).forEach(System.out::println);
- }
list6效果:

list7效果:
