从化哪里做网站好,邢台最近发生的新闻,谷歌三件套,旅社网站怎么建立1.1 动态sql语句概述
Mybatis 的映射文件中#xff0c;业务逻辑复杂时#xff0c; SQL是动态变化的#xff0c;此时在前面的学习中 SQL 就不能满足要求了。
参考的官方文档#xff1a;
1.2 动态 SQL 之if
根据实体类的不同取值#xff0c;使用不同的 SQL语句…1.1 动态sql语句概述
Mybatis 的映射文件中业务逻辑复杂时 SQL是动态变化的此时在前面的学习中 SQL 就不能满足要求了。
参考的官方文档
1.2 动态 SQL 之if
根据实体类的不同取值使用不同的 SQL语句来进行查询 比如在 id 如果不为空时可以根据id查询如果username 不为空时还要加入用户名作为条件。 这种情况在多条件组合查询中经常会碰到 select idfindColl parameterTypeuser resultTypeuserselect * from T_userwhere !--表示where--if testid!0id#{id}/ifif testusername!null and username!and username#{username}/if/where编写dao接口
//多条件查询
public ListUser findColl(User user);当查询条件id和username都存在时控制台打印的sql语句如下 Testpublic void findCollT(){SqlSession sqlSession MybatisUtils.getSqlSession(true);UserDao mapper sqlSession.getMapper(UserDao.class);User usernew User();user.setId(1);user.setUsername(张三);ListUser coll mapper.findColl(user);System.out.println(coll.toString());MybatisUtils.closeSqlSession(sqlSession);}当查询条件只有id存在时控制台打印的sql语句如下 //获得MyBatis框架生成的UserDao接口的实现类
Testpublic void findCollT(){SqlSession sqlSession MybatisUtils.getSqlSession(true);UserDao mapper sqlSession.getMapper(UserDao.class);User usernew User();user.setId(1);ListUser coll mapper.findColl(user);System.out.println(coll.toString());
MybatisUtils.closeSqlSession(sqlSession);}
总结语法:
where条件标签。如果有动态条件则使用该标签代替 where 关键字。
if条件判断标签。
if test“条件判断”查询条件拼接
/if1.3 动态sql之set (update insert) !-- 动态语句 set 更新语句 update 表名 set name#{name} --update idupdateMap parameterTypemapupdate T_user setif testusername!nullusername#{username},/ifif testsex!nullsex#{sex}/if/setwhereif testid!nullid#{id}/if/where/update!-- 动态语句 set 插入语句 insert into 表名 set name#{name},age#{age} --insert idsaveMap parameterTypemapinsert into T_usersetif testid!nullid#{id},/ifif testusername!nullusername#{username},/ifif testsex!nullsex#{sex},/ifif testaddress!nulladdress#{address},/ifif testbirthday!nullbirthday#{birthday}/if/set/insert编写dao接口添加方法
public int updateMap(MapString ,Object map);
public int saveMap(MapString ,Object map);测试类编写
修改的测试方法
Testpublic void updatemap(){SqlSession sqlSession MybatisUtils.getSqlSession(true);UserDao mapper sqlSession.getMapper(UserDao.class);
MapString,Object mapnew HashMap();map.put(id,1);map.put(username,老六);map.put(sex,女);int i mapper.updateMap(map);System.out.println(i);}
添加的测试方法Testpublic void savemapT(){SqlSession sqlSession MybatisUtils.getSqlSession(true);UserDao mapper sqlSession.getMapper(UserDao.class);
MapString,Object mapnew HashMap();map.put(username,老六);map.put(sex,女);map.put(address,郑州);map.put(birthday,Date.valueOf(2010-03-09));int i mapper.saveMap(map);System.out.println(i);}总结语法
set条件标签。set 元素可以用于动态包含需要更新的列
if条件判断标签。
if test条件判断查询条件拼接
/if1.4 动态sql之choose (when, otherwise)
相当于Java中的switch语句
当when有条件满足的时候就跳出choose choosewhen test条件1.../whenwhen test条件2.../whenwhen test条件3.../whenotherwise其他条件/otherwise
/choose代码演示步骤
1、编写Dao接口代码
2、编写mapper文件
3、测试运行
代码实现
编写Dao接口代码
public ListUser showUserfindAll(Param(username) String username,Param(sex) String sex,Param(address) String address,Param(birthday) Date birthday;编写mapper文件
!-- 动态语句choose when when otherwise --
!-- if elseif elseif elsechoosewhen test/whenwhen test/whenwhen test/whenotherwise.../otherwise/choose--
select idshowUserfindAll resultTypecn.zxy.pojo.Userselect * from user where choosewhen testusername!null and username!username like concat(%,#{username},%)/whenwhen testsex!null and sex!sex #{sex}/whenwhen testaddress!null and address!address like concat(%,#{address},%)/whenotherwiseyear(birthday)year(#{birthday})/otherwise
/choose另一种写法
select idshowUserfindAll resultTypecn.zxy.pojo.Userselect * from user wherechoosewhen testusername!nullchoosewhen testusername.indexOf(%)!-1 username like #{username}/whenwhen testusername.indexOf(_)!-1username like #{username}/whenotherwiseusername #{username}/otherwise/choose/whenwhen testsex!null and sex!sex #{sex}/whenwhen testaddress!null and address!address like concat(%,#{address},%)/whenotherwisebirthday#{birthday}/otherwise/choose/where/select实体和数据库名字不匹配
!-- 查询中如果表字段名和实体类名不一致不想定义ResultMap,可以使用查询的列名 --
sql ids1pid,user_name username,birthday birth,address addr,sex sex/sql
select idshowUserfindAll resultTypecn.zxy.pojo.Userselect include refids1p/ from user where 11choosewhen testusername!nullchoosewhen testusername.indexOf(%)!-1user_name like #{username}/whenwhen testusername.indexOf(_)!-1user_name like #{username}/whenotherwiseuser_name #{username}/otherwise/choose/whenwhen testsex!null and sex!and sex #{sex}/whenwhen testaddress!null and address!and address like concat(%,#{addr},%)/whenotherwiseand year(birthday)year(#{birth})/otherwise/choose
/select测试运行 Testpublic void test() throws IOException {InputStream is Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory factorynew SqlSessionFactoryBuilder().build(is);SqlSession session factory.openSession();UserDao userDao session.getMapper(UserDao.class);ListUser users userDao.showUserfindAll(小_, 男, 河, null);System.out.println(users.toString());
}1.5 动态 SQL 之foreach
循环执行sql的拼接操作例如SELECT * FROM user WHERE id IN (1,2,5)。
foreach 迭代一个集合通常用于in条件
属性 item 查询条件 collection必须指定 list array map—key open 括号 ( separator 分割符 close :括号
例子如下 !--循环迭代--
select idfindByIds parameterTypelist resultTypeuser!--collection: 表示集合存储类型 分别为 list array数组open : id in ( 是指代 where id in(close 闭合括号item : 查询条件separator 分割符--select * from T_userwhereforeach collectionlist openid in ( close) itemid separator,#{id}/foreach/where
/select编写dao接口
//根据多个id查询
public ListUser findByIds(ListInteger ids);测试代码片段如下 Testpublic void findByidst() {//获取sqlssession给定的对象SqlSession sqlSession MybatisUtils.getSqlSession(true);UserDao mapper sqlSession.getMapper(UserDao.class);//创建list集合 并赋值ListInteger listnew ArrayList();list.add(1);list.add(3);list.add(5);//把集合里面的值赋给mapperListUser byIds mapper.findByIds(list);for (User byId : byIds) {System.out.println(byId);}MybatisUtils.closeSqlSession(sqlSession);}批量插入
Mapper接口编写
public void insert(ListUser list);映射文件编写 insert idinsert parameterTypeuserinsert into user valuesforeach collectionlist separator, itemuser(#{user.id},#{user.username},#{user.sex},#{user.address},#{user.birthday})/foreach/insert测试类编写 Testpublic void inTest(){SqlSession sqlSession MyBatisUtils.getSqlSession(true);DUserMapper mapper sqlSession.getMapper(DUserMapper.class);long startSystem.currentTimeMillis();log.info(开始的时间{},start);ListUser userListnew ArrayList();User user;for (int i 0; i 5; i) {usernew User();user.setUsername(唐i);user.setSex(男);user.setAddress(河南北京);user.setBirthday(new Date());userList.add(user);}mapper.insert(userList);long end System.currentTimeMillis();log.info(结束的时间{},(end-start));}
总结语法:
foreach循环遍历标签。适用于多个参数或者的关系。foreach collection open close item separator获取参数/foreach属性 collection参数容器类型 (list-集合 array-数组) open开始的 SQL 语句 id in close结束的 SQL 语句 item参数变量名 separator分隔符
1.6 SQL片段抽取
Sql 中可将重复的 sql 提取出来使用时用 include 引用即可最终达到 sql 重用的目的
!--抽取sql片段简化编写--
sql idselectUser select * from student/sql
!--根据id查询--
select idfindById parameterTypeint resultTypeuserinclude refidselectUser/include where id#{id}
/select
!--根据ids遍历--
select idfindByIds parameterTypelist resultTypestudentinclude refidselectUser/includewhereforeach collectionarray openid in( close) itemid separator,#{id}/foreach/where
/select总结语法:
我们可以将一些重复性的 SQL 语句进行抽取以达到复用的效果。
- sql抽取 SQL 语句标签。
- include引入 SQL 片段标签。 sql id“片段唯一标识”抽取的 SQL 语句/sql
include refid“片段唯一标识”/
1.7 知识小结
MyBatis映射文件配置
select查询
insert插入
update修改
delete删除
wherewhere条件
ifif判断
foreach循环
sqlsql片段抽取