目录
id="updateByPrimaryKeySelective" parameterType="com.zking.model.Book" > - update t_mvc_book
- <set >
- <if test="bname != null" >
- bname = #{bname,jdbcType=VARCHAR},
-
- <if test="price != null" >
- price = #{price,jdbcType=REAL},
-
-
- where bid = #{bid,jdbcType=INTEGER}
-
自定义mvc
update aa表 set a=?,b=?,c=?,d=? where id=?
弊端:
意味着后台
实体类只有a,b属性值不为空,其他为空
进一步 update aa set a=颠颠,b=老六,c=null,d=null where id=?
mybatis动态sql
update t_mvc_book
title=颠颠,
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
没有赋值的原来有值依然是原来的值
- <select id="selectBooksIn" resultType="com.javaxl.model.Book" parameterType="java.util.List">
- select * from t_mvc_book where bid in
- <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
- #{bid}
- foreach>
- select>
-
-
- List
selectBooksIn(@Param("bookIds") List bookIds) ;
- <select id="selectBooksLike1" resultType="com.zking.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like #{bname}
- select>

- <select id="selectBooksLike2" resultType="com.zking.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like '${bname}'
- </select>

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. 一般能用#的就别用$.
- <select id="selectBooksLike3" resultType="com.zking.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.zking.model.Book">
- select * from t_mvc_book
- select>
list1、list2的结论是:对于单表查询而言,可以用Resutmap/resultType接收,但是多表必须用Resutmap接收
- <select id="list3" resultType="com.zking.model.Book" parameterType="com.javaxl.model.BookVo">
- select * from t_mvc_book where bid in
- <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
- #{bid}
- foreach>
- select>
如果要传入多个查询参数,必须以对象的方式进行传递
不管返回1条数据,还是多条数据,都应该用java.util.Map进行接受 如果是1条数据,那么返回值是Map 如果返回是多条数据,那么返回值是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>
添加进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<Map> listPager(Map map);
BookBiz
List<Map> listPager(Map map, PageBean pageBean);
BookBizImpl
- @Override
- public List<Map> listPager(Map map, PageBean pageBean) {
- //分页插件相关代码
- 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","圣墟");
- // 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);
- }
- }
>(>)
<(<)
&(&)
空格( )
BookMapper.xml
- <select id="list6" resultType="com.zking.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.zking.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.zking.model;
-
- import java.util.List;
-
- 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
- @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);
- }
- }