1.
<!--
insert into t_mvc_book(bid,bname,price) values(#{bid},#{bname},#{price})
insert into t_mvc_book(bid,price) values(#{bid},#{price})
trim:格式化标签 simpleDateFormart new date->是一个长整形的数字
作用:构造课运行的的sql语句
prifix:前缀
subfix:后缀
subffixOverrides:减除最后一个逗号
-->
2.模糊查询的三种方式
2.1.#{... }: bname like #{bname },
2.2.${...}:bname like '${bname}':传过去的字符串自带一个引号。
2.3.concat:bname like concat(concat('%',#{bname}),'%')('%圣墟%')(作用:拼接两个字符串)
2.4. 使用${...}代替#{...}(不建议使用该方式,有SQL注入风险)
2.5. 关键:#{...}与${...}区别?
参数类型为字符串,#会在前后加单引号['],$则直接插入值
注:
1) mybatis中使用OGNL表达式传递参数
2) 优先使用#{...}
3) ${...}方式存在SQL注入风险
3.查询返回结果集
resultMap:适合使用返回值是自定义实体类的情况,不是jdk等提供的类
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型
3.1 使用resultMap返回自定义类型集合
3.2 使用resultType返回List<T>
3.3 使用resultType返回单个对象
3.4 使用resultType返回List<Map>,适用于多表查询返回结果集
3.5 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
3.6 如果说多张表查询,建议使用resultmap,这样的话不许额外创建vo类
4.分页查询
4.1 为什么要重写mybatis的分页?
Mybatis的分页功能很弱,它是基于内存的分页(查出所有记录再按偏移量offset和边界limit取结果),在大数据量的情况下这样的分页基本上是没有用的
4.2 导入分页插件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
4.3 将pagehelper插件配置到mybatis中
<!-- 配置分页插件PageHelper, 4.0.0以后的版本支持自动识别使用的数据库 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
4.4
public List<Map> list7(Map map,PageBean pageBean) { /** * jdbc:map->来自于jsp * baseDao * 1.getCountsql->total->pageBean.setTotal * 2.setPageSql->list<T>->返回 * * * hibernate: * map->来自于jsp * baseDao * 1.getCountHql->total->pageBean.setTotal * 2.不需要拼接出pageHql->hibernate.setFirst.../setMaxRes... * * mybatis中借助github提供的pageHelper * pageHelper.start * * 业务流程 * pageBean.setTotal */ if(pageBean!=null && pageBean.isPagination()) PageHelper.startPage(pageBean.getPage(),pageBean.getRows()); List<Map> maps = this.bookMapper.list7(map); if(pageBean!=null && pageBean.isPagination()){ PageInfo pageInfo = new PageInfo(maps); pageBean.setTotal(pageInfo.getTotal()+""); } return maps; }
5.特殊字符处理
>:(>)
<:(<)
&:(&)
空格( )
where 1=1 and price > #{min} and price < #{max}
<![CDATA[ <= ]]> :转译特殊字符
<![CDATA[ and price > #{min} and price < #{max}]]>