Mybatis靜態SQL之if、choose、where、set、trim、foreach標志實例詳解。本站提示廣大學習愛好者:(Mybatis靜態SQL之if、choose、where、set、trim、foreach標志實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Mybatis靜態SQL之if、choose、where、set、trim、foreach標志實例詳解正文
靜態SQL就是靜態的生成SQL。
if標志
假定有如許一種需求:查詢用戶,當用戶名不等於“admin”的時刻,我們還須要暗碼為123456。
數據庫中的數據為:
MyBatisConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--界說別號 留意typeAliases必定要在environments之前--> <typeAliases> <typeAlias type="jike.book.pojo.JiKeUser" alias="JiKeUser"/> <typeAlias type="jike.book.pojo.Author" alias="Author"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> </transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/jikebook"/> <property name="username" value="root"/> <property name="password" value="*****"/> </dataSource> </environment> </environments> <mappers> <mapper resource="jike/book/map/jikeUser.xml"/> </mappers> </configuration>
JiKeUser.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="/"> <select id="selectSQL" resultType="JiKeUser" parameterType="JiKeUser"> SELECT * FROM jikebook.jikeuser WHERE 1=1 <if test="userName!='admin'"> AND password=#{password} </if> </select> </mapper>
測試類:
package jike.book.test; import jike.book.pojo.JiKeUser; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.Reader; import java.util.List; /** * DateTime: 2016/9/6 13:36 * 功效: * 思緒: */ public class TestSQL { public static void main(String[] args) { // 資本途徑 String resource="jike/book/map/MyBatisConfig.xml"; Reader reader=null; SqlSession session; try { reader= Resources.getResourceAsReader(resource); } catch ( IOException e ) { e.printStackTrace(); } SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader); session=sqlMapper.openSession(); JiKeUser jiKeUser=new JiKeUser(); jiKeUser.setPassword("123456"); List<JiKeUser> userList=session.selectList("selectSQL",jiKeUser); for ( JiKeUser user:userList ) { System.out.println("userName:"+user.getUserName()); } session.close(); } }
運轉成果為:
choose標志
假定我們以後有這麼一個需求:查詢用戶,假如用戶名不為空就加上用戶名這個前提,假如id不為空也加上id這個前提,不然的話就是設置暗碼不為空,這就是一個多路選擇。
MyBatisConfig.xml不轉變,在JikeUser.xml中加上:
<select id="selectJiKeUserChoose" resultType="JiKeUser" parameterType="JiKeUser"> select * from jikeuser where 1=1 <choose> <when test="userName!=null"> and userName like #{userName} </when> <when test="id!=0"> and id =#{id} </when> <otherwise> and password is not null </otherwise> </choose> </select>
測試類:假定用戶名不為空:
package jike.book.test; import jike.book.pojo.JiKeUser; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.Reader; import java.util.List; /** * DateTime: 2016/9/6 13:36 * 功效: * 思緒: */ public class TestSQL { public static void main(String[] args) { // 資本途徑 String resource="jike/book/map/MyBatisConfig.xml"; Reader reader=null; SqlSession session; try { reader= Resources.getResourceAsReader(resource); } catch ( IOException e ) { e.printStackTrace(); } SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader); session=sqlMapper.openSession(); JiKeUser jiKeUser=new JiKeUser(); jiKeUser.setUserName("YEN"); List<JiKeUser> userList=session.selectList("selectJiKeUserChoose",jiKeUser); for ( JiKeUser user:userList ) { System.out.println("userName:"+user.getUserName()); } session.close(); } }
成果為:
假定不設置用戶名這個前提,即正文失落jiKeUser.setUserName("YEN");:
where標志、set標志
下面我們在choose中查詢是不克不及肯定子銜接前提中的and是寫照樣不寫,是以加了一個1=1.而where會只能的去斷定該不應加。
<select id="selectJiKeUserWhere" resultType="JiKeUser" parameterType="JiKeUser"> select * from jikeuser <where> <if test="userName!=null"> and userName like #{userName} </if> <if test="id!=null"> and id =#{id} </if> </where> </select>
set標志智能賦值,會主動去失落過剩的”,”。
<update id="updateJiKeUserSet" parameterType="JiKeUser"> update JiKeUser <set> <if test="userName != null">userName=#{userName},</if> <if test="password != null">password=#{password},</if> </set> where id=#{id} </update>
操作之前的數據:
操作:
操作成果:
<update id="updateUserTrim" parameterType="JiKeUser"> UPDATE JiKeUser <trim prefix="SET" suffixOverrides="," suffix="WHERE id = #{id}" > <if test="userName != null and userName != '' "> userName = #{userName}, </if> <if test="password != null and password != '' "> password=#{password}, </if> </trim> </update>
foreach標志
平日用於輪回查詢或輪回賦值
<select id="selectJiKeUserForeach" resultType="JiKeUser" parameterType="list"> select * from jikeuser <where> id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </where> </select>
測試:
以上所述是小編給年夜家引見的Mybatis靜態SQL之if、choose、where、set、trim、foreach標志實例詳解,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!