視頻觀看地址:http://edu.51cto.com/course/14674.html?source=sohtml
需求:查詢所有用戶,若是用戶填寫了姓名,按照姓名的模糊查詢,若是用戶填寫了姓名和年齡,則按姓名和年齡一塊兒查找java
一、接口定義sql
public List<User> querybyUser(User vo) throws Exception;
二、mapper.xml配置數組
<select id = "querybyUser" resultMap="UserMap"> select * from tb_user where 1=1 <if test="userName!=null and userName!=''"> and user_Name like #{userName} </if> <if test = "age!=null"> and age =#{age} </if> </select>
三、測試方法mybatis
@Test public void testIf()throws Exception{ User user = new User(); user.setUserName(null); user.setAge(17); List<User> list = userMapper.querybyUser(user); for (User u : list) { System.out.println(u); } }
四、日誌oracle
DEBUG - Opening JDBC Connection DEBUG - Created connection 276327391. DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@10786bdf] DEBUG - ==> Preparing: select * from tb_user where 1=1 and age =? DEBUG - ==> Parameters: 17(Integer) DEBUG - <== Total: 1 User [userid=10, userName=阿珂3, pwd=123456, age=17, sex=女, birthday=Mon Aug 13 14:57:50 CST 2018] DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@10786bdf] DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@10786bdf] DEBUG - Returned connection 276327391 to pool.
在前面的sql中的咱們認爲的定義了一個where 1=1這樣的東西,此時咱們能夠經過mybatis的where標籤來搞定app
<select id = "querybyUser" resultMap="UserMap"> select * from tb_user <where> <if test="userName!=null and userName!=''"> and user_Name like #{userName} </if> <if test = "age!=null"> and age =#{age} </if> </where> </select>
where :自動根據需求生成,而且能夠剔除不須要andide
直接測試便可測試
另一個動態 SQL 通用的必要操做是迭代一個集合, 一般是構建在 IN 條件中的。ui
接口方法定義
public List<User> querybyIn(QueryUser vo) throws Exception;
mapper.xml
<select id = "querybyIn" resultMap="UserMap"> select * from tb_user <where> <if test="userids!=null"> <!-- collection:要迭代集合或者數組 --> <foreach collection="userids" open="userid in (" close=")" separator="," item="userid"> #{userid} </foreach> </if> </where> </select>
測試方法
@Test public void testforeach()throws Exception{ QueryUser queryUser= new QueryUser(); queryUser.setUserids(new int[]{8,9,2,3}); List<User> list = userMapper.querybyIn(queryUser); for (User u : list) { System.out.println(u); } }
日誌
DEBUG - Opening JDBC Connection DEBUG - Created connection 1060925979. DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@3f3c721b] DEBUG - ==> Preparing: select * from tb_user WHERE userid in ( ? , ? , ? , ? ) DEBUG - ==> Parameters: 8(Integer), 9(Integer), 2(Integer), 3(Integer) DEBUG - <== Total: 4 User [userid=8, userName=阿珂, pwd=123456, age=18, sex=女, birthday=Mon Aug 13 14:06:14 CST 2018] User [userid=9, userName=阿珂2, pwd=123456, age=18, sex=女, birthday=Mon Aug 13 14:56:45 CST 2018] User [userid=2, userName=張三, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018] User [userid=3, userName=李四, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018] DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@3f3c721b] DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@3f3c721b] DEBUG - Returned connection 1060925979 to pool.
set 元素能夠被用於動態包含更新的列,而不包含不需更新的(動態更新)
接口定義
public int dycUpdate(User vo) throws Exception;
mapper.xml定義
<update id="dycUpdate"> update tb_user <set> <if test="userName!=null and userName!=''"> user_name=#{userName}, </if> <if test="pwd!=null and pwd!=''"> pwd=#{pwd}, </if> <if test="age!=null"> age=#{age}, </if> <if test="sex!=null"> sex=#{sex}, </if> <if test="birthday!=null"> birthday=#{birthday}, </if> </set> where userid=#{userid} </update>
測試方法
@Test public void testdycUpdate()throws Exception{ User user = new User(); user.setUserName("hello"); user.setSex("男"); user.setBirthday(new Date()); user.setUserid(8); userMapper.dycUpdate(user); sqlSession.commit(); }
日誌
DEBUG - Opening JDBC Connection DEBUG - Created connection 340839523. DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63] DEBUG - ==> Preparing: update tb_user SET user_name=?, sex=?, birthday=? where userid=? DEBUG - ==> Parameters: hello(String), 男(String), 2018-08-14 10:19:19.311(Timestamp), 8(Integer) DEBUG - <== Updates: 1 DEBUG - Committing JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63] DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63] DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63] DEBUG - Returned connection 340839523 to pool.
接口定義
public int dycInsert(User vo) throws Exception;
mapper.xml文件
<insert id="dycInsert"> insert into tb_user ( userid, <trim suffixOverrides=","> <if test="userName!=null and userName!=''"> user_name, </if> <if test="pwd!=null and pwd!=''"> pwd, </if> <if test="age!=null"> age, </if> <if test="sex!=null"> sex, </if> <if test="birthday!=null"> birthday, </if> </trim> ) values( seq_user.nextval, <trim suffixOverrides=","> <if test="userName!=null and userName!=''"> #{userName}, </if> <if test="pwd!=null and pwd!=''"> #{pwd}, </if> <if test="age!=null"> #{age}, </if> <if test="sex!=null"> #{sex}, </if> <if test="birthday!=null"> #{birthday}, </if> </trim> ) </insert>
測試方法
@Test public void testdycInsert()throws Exception{ User user = new User(); user.setUserName("yui"); user.setSex("男"); user.setBirthday(new Date()); user.setUserid(8); userMapper.dycInsert(user); sqlSession.commit(); }
日誌
DEBUG - Opening JDBC Connection DEBUG - Created connection 1341177488. DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90] DEBUG - ==> Preparing: insert into tb_user ( userid, user_name, sex, birthday ) values( seq_user.nextval, ?, ?, ? ) DEBUG - ==> Parameters: yui(String), 男(String), 2018-08-14 10:31:06.973(Timestamp) DEBUG - <== Updates: 1 DEBUG - Committing JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90] DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90] DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90] DEBUG - Returned connection 1341177488 to pool.
有時咱們不想應用全部的條件, 相反咱們想選擇不少狀況下的一種。 Java 中的 switch 和 語句類似,MyBatis 提供 choose 元素。
需求:查詢全部用戶
1.若是用戶填寫了性別和姓名,則按姓名查找
2.若是用戶填寫了姓名,按姓名查找
3.若是用戶未填寫姓名,可是填寫了性別,則按性別查找
接口聲明
public List<User> queryDyc(QueryUser vo) throws Exception;
mapper.xml文件
<select id="queryDyc" resultMap="UserMap"> select * from tb_user <where> <choose> <when test="name!=null and name!=''"> user_name=#{name} </when> <when test="sex!=null and sex!=''"> sex=#{sex} </when> <otherwise> age>5 </otherwise> </choose> </where> </select>
測試方法
@Test public void testIfelse()throws Exception{ QueryUser queryUser= new QueryUser(); queryUser.setName(null); queryUser.setSex(null); List<User> list = userMapper.queryDyc(queryUser); for (User u : list) { System.out.println(u); } }
日誌
DEBUG - Opening JDBC Connection DEBUG - Created connection 480779844. DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@1ca81e44] DEBUG - ==> Preparing: select * from tb_user WHERE age>5 DEBUG - ==> Parameters: DEBUG - <== Total: 7 User [userid=8, userName=hello, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018] User [userid=9, userName=阿珂2, pwd=123456, age=18, sex=女, birthday=Mon Aug 13 14:56:45 CST 2018] User [userid=10, userName=阿珂3, pwd=123456, age=17, sex=女, birthday=Mon Aug 13 14:57:50 CST 2018] User [userid=2, userName=張三, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018] User [userid=3, userName=李四, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018] User [userid=4, userName=王五, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018] User [userid=5, userName=趙六, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018] DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@1ca81e44] DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@1ca81e44] DEBUG - Returned connection 480779844 to pool.
sql片斷是爲了將一些經常使用的sql語句保存起來,在須要的時候進行引用便可
若是sql片斷和statement在同一個命名空間下,則直接引用
<sql id="basesql"> select * from tb_user </sql> <select id="queryDyc" resultMap="UserMap"> <include refid="basesql"/> <where> <choose> <when test="name!=null and name!=''"> user_name=#{name} </when> <when test="sex!=null and sex!=''"> sex=#{sex} </when> <otherwise> age>5 </otherwise> </choose> </where> </select>
若是sql片斷和statement不在同一個命名空間下,則直接則使用命名空間.sqlid的形式進行引用
<select id="queryDyc" resultMap="UserMap"> <include refid="mycomm.baseselect"/> <where> <choose> <when test="name!=null and name!=''"> user_name=#{name} </when> <when test="sex!=null and sex!=''"> sex=#{sex} </when> <otherwise> age>5 </otherwise> </choose> </where> </select>