Mybatis传参总结

mybatis中手动写sql,步骤是先在navicate中执行通过的sql,

如果是对象类型传参,需要注意jdbcType转换,比如:name = #{record.name,jdbcType=VARCHAR}


以下几种方法比较常用,欢迎补充。


1.pom.xml中mybatis依赖1.2.0


<dependencies>
 <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>

</build>


2.基本类型和对象参数,可以是这样:

ServerMapper.java:

int updateByExample(@Param("record") Server record, @Param("example") ServerExample example);


ServerMapper.xml:

<update id="updateByExample" parameterType="map">
update server
set id = #{record.id,jdbcType=VARCHAR},
name = #{record.name,jdbcType=VARCHAR},
operatingsystem = #{record.operatingsystem,jdbcType=VARCHAR},
cpu = #{record.cpu,jdbcType=INTEGER},
applicant = #{record.applicant,jdbcType=VARCHAR},
status = #{record.status,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>

3.仅有一个list参数,可以这样:

ServerMapper.java:

List<ServerDetails> selectByServerIds(List<String> serverIds);


ServerMapper.xml,注意collection为list:

<select id="selectByServerIds" resultMap="ServerDetailsMap">

SELECT s.id, s.`name`,s.operatingsystem,s.cpu,s.disksize,s.memorysize,s.intranetip,s.vcluster,s.`owner`,s.environment,s.applicant,s.`status`,
ip.ip,ip.`status` as ipstatus, ip.segment,ip.remark,ip.line
from `server` as s , ippool as ip
where s.intranetip = ip.id
and s.id in
<foreach item="item" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>

可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。

当你这么做的时,MyBatis 会自动将它包装在一个 Map 中,用名称作为key。List 实例将会以“list” 作为key,而数组实例将会以“array”作为key。


4.多个参数中包含list的情况,是这样的(excRelatedServerIds是list类型):

ServerMapper.java文件中传入的是包含list的map,ServerMapper.xml中的collection为指定的excRelatedServerIds


Map<String, Object> map = Maps.newHashMap();
map.put("env", env);
map.put("userId", userId);
map.put("excRelatedServerIds", excRelatedServerIds);
PageInfo<UserServerDetails> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(()-> serverMapper.selectPageByEnvAndUserExcRelated(map));

ServerMapper.xml:

 select us.id, us.user_id, us.server_id,
s.`name`, s.operatingsystem, s.cpu,s.disksize,s.memorysize, s.intranetip, s.vcluster,s.`owner`,s.environment,s.applicant,s.`status`,
ip.ip,ip.`status` as ipstatus,ip.segment,ip.remark , ip.line
from user_server as us,`server` as s, ippool as ip
where us.server_id = s.id and s.intranetip = ip.id
<if test="env != null">
and s.environment=#{env,jdbcType=INTEGER}
</if>

<if test="userId != null">
and us.user_id=#{userId,jdbcType=VARCHAR}
</if>

and s.id not in
<foreach item="item" index="index" collection="excRelatedServerIds" open="(" separator="," close=")">
#{item}
</foreach>

</select>





5.还有一种是按照顺序传参数,这种不推荐:


Mapper.java:

Public User selectUser(String name,String area);

Mapper.xml :

<select id="selectUser" resultMap="BaseResultMap">

select * from user_user_t where user_name = #{0} and user_area=#{1}

</select> 其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。



6.如果要封装自己的返回类型,需要xml文件中定义并且去引用bean对象:

<resultMap id="ServerDetailsMap" type="com.ServerDetails">
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="operatingsystem" jdbcType="VARCHAR" property="operatingsystem" />
<result column="cpu" jdbcType="INTEGER" property="cpu" />
<result column="disksize" jdbcType="INTEGER" property="disksize" />
<result column="memorysize" jdbcType="INTEGER" property="memorysize" />
<result column="intranetip" jdbcType="VARCHAR" property="intranetip" />
<result column="vcluster" jdbcType="VARCHAR" property="vcluster" />
<result column="owner" jdbcType="VARCHAR" property="owner" />
<result column="environment" jdbcType="INTEGER" property="environment" />
<result column="applicant" jdbcType="VARCHAR" property="applicant" />
<result column="status" jdbcType="VARCHAR" property="status" />
</resultMap>


mybatis ${}与#{}的区别(来源网络)


#{} 解析的是占位符? 可以防止SQL注入,比如打印出来的语句 select * from table where id=?

然而${} 则是不能防止SQL注入打印出来的语句 select * from table where id=2 实实在在的参数。


最简单的区别就是${}解析传过来的参数值不带单引号,#{}解析传过来参数带单引号。


最后总结一下必须使用$引用参数的情况,那就是参数的int型的时候,必须使用$引用。





参考:http://blog.csdn.net/u014687389/article/details/72778664?locationNum=8&fps=1
上一篇:24款非常实用的CSS3工具终极收藏


下一篇:mvc 注解相应配置