Parameter
1. 传入简单类型
JAVA代码:
public User get(Long id) { return (User) getSqlSession().selectOne("com.liulanghan.get" , id); }
MAPPER :
<select id="findUserListByIdList" parameterType="java.lang.Long" resultType="User"> select * from user where id = #{id}; </select>
2. 传入List
JAVA代码:
public List<Area> findUserListByIdList(List<Long> idList) { return getSqlSession().selectList("com.liulanghan.findUserListByIdList", idList); }
MAPPER :
<select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User"> select * from user user <where> user.ID in ( <foreach item="guard" index="index" collection="list" separator=","> #{guard} </foreach> ) </where> </select>
单独传入list时,foreach中的collection必须是list,不不管变量的具体名称是什么。比如这里变量名为idList,
collection却是是list。
3. 传入数组
JAVA代码:
public List<Area> findUserListByIdList(int[] ids) { return getSqlSession().selectList("com.liulanghan.findUserListByIdList", ids); }
MAPPER :
<select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User"> select * from user user <where> user.ID in ( <foreach item="guard" index="index" collection="array" separator=","> #{guard} </foreach> ) </where> </select>
单独传入数组时,foreach中的collection必须是array,不不管变量的具体名称是什么。比如这里变量名为ids,
collection却是是array
4. 传入map
JAVA代码:
public boolean exists(Map<String, Object> map){ Object count = getSqlSession().selectOne("com.liulanghan.exists", map); int totalCount = Integer.parseInt(count.toString()); return totalCount > 0 ? true : false; }
MAPPER :
<select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer"> SELECT COUNT(*) FROM USER user <where> <if test="code != null"> and user.CODE = #{code} </if> <if test="id != null"> and user.ID = #{id} </if> <if test="idList !=null "> and user.ID in ( <foreach item="guard" index="index" collection="idList" separator=","> #{guard} </foreach> ) </if> </where> </select>
MAP中有list或array时,foreach中的collection必须是具体list或array的变量名。比如这里MAP含有一个
名为idList的list,所以MAP中用idList取值,这点和单独传list或array时不太一样。
5. 传入JAVA对象
JAVA代码:
public boolean findUserListByDTO(UserDTO userDTO){ Object count = getSqlSession().selectOne("com.liulanghan.exists", userDTO); int totalCount = Integer.parseInt(count.toString()); return totalCount > 0 ? true : false; }
MAPPER :
<select id="findUserListByDTO" parameterType="UserDTO" resultType="java.lang.Integer"> SELECT COUNT(*) FROM USER user <where> <if test="code != null"> and user.CODE = #{code} </if> <if test="id != null"> and user.ID = #{id} </if> <if test="idList !=null "> and user.ID in ( <foreach item="guard" index="index" collection="idList" separator=","> #{guard} </foreach> ) </if> </where> </select>
JAVA对象中有list或array时,foreach中的collection必须是具体list或array的变量名。比如这里UserDTO含有一个
名为idList的list,所以UserDTO中用idList取值,这点和单独传list或array时不太一样。
6.取值
由上面可以看出,取值的时候用的是#{}。它具体的意思是告诉MyBatis创建一个预处理语句参数。
使用JDBC,这样的一个参数在SQL中会由一个“?”来标识,并被传递到一个新的预处理语句中,就像这样:
// Similar JDBC code, NOT MyBatis… String selectPerson = “SELECT * FROM PERSON WHERE ID=?”; PreparedStatement ps = conn.prepareStatement(selectPerson); ps.setInt(1,id);
可以看到这个写法比较简单,MyBatis为我们做了很多默认的事情,具体的写法应该如下:
#{property,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler,mode=OUT,resultMap=User}
property:属性名,即代码传入的变量名。
javaType:该字段在JAVA中的类型,比如int。
jdbcType:该字段在JDBC中的类型,比如NUMERIC。
typeHandler:类型处理器
mode:参数类型为IN,OUT或INOUT参数
resultMap:结果。
还好,MyBatis比较体谅我们,一般我们只需写一个属性名即可,如#{id},其他的如javaType和typeHandlerMybatis
会自动帮我们填好。可是这样有时也会出问题,比如出现CLOB字段时。
由于JAVA代码中的String类型对应的默认typeHandler为StringTypeHandler,当用String类型处理时,如果String长度超过一定长度,就会报如下错误:
setString can only process strings of less than 32766 chararacters
解决办法是指定该属性的typeHandler,如下:
#{message,typeHandler=org.apache.ibatis.type.ClobTypeHandler}
我们也可以自定义typeHandler来处理需要的数据,具体这里详述。
JDBC类型是仅仅需要对插入,更新和删除操作可能为空的列进行处理。这是JDBC的需要,而不是MyBatis的。一般不需要配置
mode、resultMap一般不需要,在写存储过程时会用到,这里不详述。
7.字符串替换
一般情况下,我们采用#{}取值,产生预处理语句,但是有时我们可能不希望Mybatis来帮我们预处理,比如ORDER BY时,可以
采用如下写法:
ORDER BY ${columnName}
这里MyBatis不会修改或转义字符串。而是直接拼接到SQL字符串后面。
重要:接受从用户输出的内容并提供给语句中不变的字符串,这样做是不安全的。这会导致潜在的SQL注入攻击,因此你
不应该允许用户输入这些字段,或者通常自行转义并检查。
相关推荐
Introduce custom 'Mybatis Parameter' language to support Mybatis parameter expression. Generate Mapper XML, SQL statement, statement declaration on the fly. Integrate with DataSource in IDEA. Many ...
mybatis源码是mybatis-3.2.6的源码,用户使用mybatis时可以进行参考
mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis练习3mybatis...
mybatis入门案例mybatis入门案例mybatis入门案例mybatis入门案例mybatis入门案例mybatis入门案例mybatis入门案例mybatis入门案例mybatis入门案例mybatis入门案例mybatis入门案例mybatis入门案例mybatis入门案例...
mybatis在持久层框架中还是比较火的,一般项目都是基于ssm。虽然mybatis可以直接在xml中通过SQL语句操作数据库,很是灵活。但正其操作都要通过SQL语句进行,就必须写大量的xml文件,很是麻烦。mybatis-plus就很好的...
解决mybatis头文件报错 下载好压缩包 解压将文件放到本地文件夹 例如 D盘的哪个文件夹 D:\mybatis\ ;然后打开eclipse ->Window->prefenrence->XML->XML Catalog->User Specifiled Entreis->Add->Location(此处是你...
开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2...
mybatis deom.mybatis 的简单入门实例。基于maven开发的。
生成已tk.mybatis封装的mybatis代码
mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip ...
mybatis_plugin2.9.2
mybatis、mybatis详细设计、mybatis配置
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 ...
基于mybatis的慢SQL小插件,原理是mybatis拦截器。只需要在springboot的配置文件做简单的配置,mybatis拦截器将SQL中所有参数自动做了填充。拦截器监控慢SQL并将完整的可执行的SQL语句打印在日志文件中,复制该SQL...
MyBatis学习代码: mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程mybatis in action之三实现数据的增删改查 mybatis实战教程mybatis in...
mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架mybatis框架...
该jar包是Mybatis和Spring整合必备jar包,适配Mybatis-3.4.0及以上版本。
free-idea-mybatis is an enchange plugin for idea to supoort mybatis,here is the main functions: generate mapper xml files navigate from the code to mapper and from the mapper back to code auto code...
本工程为 MyBatis-Plus 的官方示例,项目结构如下: mybatis-plus-sample-quickstart: 快速开始示例 mybatis-plus-sample-quickstart-springmvc: 快速开始示例(Spring MVC版本) mybatis-plus-sample-reduce-...
mybatis中文离线文档