mybatis葂和${}の違い、参、基本文法を詳しく説明します。
1(#{}と${}の違いと注入問題
(1) :
, SQL mybatis , mapper xml , mybatis ,#{} ${} :
:select * from t_user where userName = #{name};
#{} : ? :select * from t_user where userName = ?
#{} : :select * from t_user where userName = 'zhangsan'
(2) :
#{}, sql ; 、 , ${}
:select * from ${tableName} where id > #{id};
(3) SQL :
, ${} :
select * from ${tableName};
t_user;delete from t_user, sql , :
select * from t_user;delete from t_user;
(4) like :
concat :
select * from t_user where name like concat('%', #{name}, '%')
2 mybatis何種類かの伝参方式
:
(1) :
public User getUserByUuid(String uuid);
<select id="getUserByUuid" resultMap="BaseResultMap" parameterType="Object">
SELECT * FROM t_user WHERE uuid = #{uuid}
</select>
(2)
public User getUserByNameAndPass(String name,String pass);
<select id="getUserByNameAndPass" resultMap="BaseResultMap" parameterType="Object">
SELECT * FROM t_user WHERE t_name = #{0} and t_pass = #{1}
</select>
(3)Map
public User getUserByMap(Map<String,Object> map);
<select id="getUserByMap" resultMap="BaseResultMap" parameterType="java.util.Map">
SELECT * FROM t_user WHERE t_name = #{name} and t_pass = #{pass}
</select>
(4)
public int updateUser(User user);
<select id="updateUser" resultMap="BaseResultMap" parameterType="Object">
update t_user set t_name = #{name}, t_pass = #{pass} where uuid=#{uuid}
</select>
(4)List
public int batchDelUser(List<String> uuidList);
<delete id="batchDelUser" parameterType="java.util.List">
DELETE FROM t_user WHERE uuid IN
<foreach collection="list" index="index" item="uuid" open="(" separator="," close=")">
#{uuid}
</foreach>
</delete>
:
public List<User> getUserByTime(@Param("startTime")String startTime, @Param("endTime")String endTime);
<select id="getUserByTime" resultMap="BaseResultMap" parameterType="Object">
SELECT * from t_user where createTime >= #{startTime} and createTime <= #{endTime}
</select>
2 chose when otherswise
//JAVA
public List<Group> getUserRoleRelByUserUuid(@Param("groupUuid") String userUuid,@Param("roleList")List<String> roleUuidList);
//SQL
SELECT * from user_role where groupUuid=#{groupUuid}
<choose>
<when test="roleList!=null&&roleList.size()>0">
AND roleUuid IN
<foreach collection="roleList" index="index" item="roleUuid" open="(" separator="," close=")">
#{roleUuid}
</foreach>
</when>
<otherwise>
AND roleUuid IN ('')
</otherwise>
</choose>
3文字列が等しいと判断する
//JAVA
public int getOrderCountByParams(Map<String, Object> params);
//SQL
<select id="getOrderCountByParams" resultType="java.lang.Integer" parameterType="Object">
SELECT count(*) FROM itil_publish_order where 1=1
<if test="timeType == '1'.toString()" >
AND create_time >= #{timeStr}
</if>
<if test="timeType == '2'.toString()" >
AND end_time <= #{timeStr}
</if>
</select>
<if test = 'timeType== "1"'> </if>
4 CONCAT関数はあいまいなマッチングを実現する。
<select id="getMaxSerialCode" resultType="java.lang.String" parameterType="Object">
SELECT count(*) FROM
itil_publish_order
WHERE serial_code LIKE CONCAT('%',#{codeStr},'%')
ORDER BY serial_code DESC LIMIT 1
</select>
5大なりイコール、小なり
//JAVA
public List<PublishOrder> getOrderCount(@Param("startTime") String startTime,@Param("startTime")List<String> startTime);
//SQL
<select id="getOrderCount" resultType="java.lang.String" parameterType="Object">
SELECT * FROM itil_publish_order
WHERE createTime >= #{startTime} and <= #{startTime}
</select>
ここで、mybatis菗と米ドル{}の違い、伝来、基本文法についての文章を紹介します。これに関連して、MyBatisの中で、もっと多いのです。