MyBatis學習(五)
阿新 • • 發佈:2018-09-30
形式 string rom tag pda throws when reac 剔除 視頻觀看地址:http://edu.51cto.com/course/14674.html?source=so
1、動態sql
1.1、if
需求:查詢全部用戶,如果用戶填寫了姓名,按照姓名的模糊查詢,如果用戶填寫了姓名和年齡,則按姓名和年齡一起查找
1、接口定義
public List<User> querybyUser(User vo) throws Exception;
2、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>
3、測試方法
@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);
}
}
4、日誌
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.
1.2、where
在前面的sql中的我們認為的定義了一個where 1=1這樣的東西,此時我們可以通過mybatis的where標簽來搞定
<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 :自動根據需求生成,並且可以剔除不需要and
直接測試即可
1.3、foreach
另外一個動態 SQL 通用的必要操作是叠代一個集合, 通常是構建在 IN 條件中的。
接口方法定義
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.
1.4、Set
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.
1.5、動態添加
接口定義
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.
1.6、choose、when、otherwise
有時我們不想應用所有的條件, 相反我們想選擇很多情況下的一種。 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.
1.7、sql片段
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>
MyBatis學習(五)