[一步是咫尺,一步即天涯]
首先,请大家先回顾一下,我们前文在讲述
准备工作:
a.操作系统 :win7 x64
b.基本软件:MySQL,Mybatis,Spring,SQLyog
-------------------------------------------------------------------------------------------------------------------------------------
2.修改UserInfoMapper.xml,具体内容如下:
【解释】b.按照标准写法,第一个
c.如果没有一个条件符合,则返回所有条目。
d.
3.修改单元测试方法,如下:
@Test
public void testSeletOne() {
try {
Map map = new HashMap();
map.put("department", "1");
map.put("gender", "1");
map.put("position", "工程师");
Departments d = new Departments("2", "%售%");
map.put("d", d);
UserInfoDao userInfo = sqlSession.getMapper(UserInfoDao.class);
List UIList = userInfo.findUserInfoByUnoQuantity(map);
for (UserInfo ui : UIList) {
System.out.println(ui.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
4.运行单元测试方法,观察输出即可。
5.结论:where 元素知道只有在一个以上的
--------------------------------------------------------------------------------------------------------------------------------------------------------
1.该标签的功能与
2.修改Mapper文件,具体内容如下:
【解释】
a.我们使用
b.属性“prefix”表示:加入前缀where
c.属性“prefixOverrides”表示:自动覆盖第一个“and”或者“or”
d.后缀的用法类似;
3.增加对应接口,修改单元测试方法调用接口,如下:
@Test
public void testSeletOne() {
try {
Map map = new HashMap();
map.put("department", "1");
map.put("gender", "1");
map.put("position", "工程师");
Departments d = new Departments("2", "%售%");
map.put("d", d);
UserInfoDao userInfo = sqlSession.getMapper(UserInfoDao.class);
List UIList = userInfo.findUserInfoByTrim(map);
for (UserInfo ui : UIList) {
System.out.println(ui.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
4.运行单元测试方法,观察控制台输出即可。1.注意:此标签用于update语句。请各位看官注意观察书写方法。
2.修改Mapper文件,具体内容如下:
update userInfo
mobile=#{mobile},
gender=#{gender},
position = #{position},
where userid=#{userid}
【解释】
a.SQL语句的set被
b.每个
c.
update userInfo
mobile=#{mobile},
gender=#{gender},
position = #{position},
where userid=#{userid}
@Test
public void testUpdate() {
try {
UserInfo ui = new UserInfo("admin", "3", "经理", "77778888","0", "[email protected]", null);
UserInfoDao userInfo = sqlSession.getMapper(UserInfoDao.class);
int re = userInfo.updateUserInfoBySet(ui);
if(re==1){
System.out.println("更新成功");
}
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
4.运行单元测试方法,观察控制台输出即可
--------------------------------------------------------------------------------------------------------------------------------------------------------
至此,Mybatis最入门---动态查询(choose,when,otherwise)结束
经过上面的叙述与使用,发现