当前位置: 首页 > news >正文

香水网站建设规划书湘潭市建设工程质量监督站网站

香水网站建设规划书,湘潭市建设工程质量监督站网站,重庆金融网站建设,html制作个人主页一、动态SQL 1.概述】 1.1动态SQL#xff1a; 是 MyBatis 的强大特性之一#xff0c;解决拼接动态SQL时候的难题#xff0c;提高开发效 1.2分类#xff1a; if choose(when,otherwise) trim(where,set) foreach 2.if 2.1 做 where 语句后面条件查询的,if 语句是可以…一、动态SQL 1.概述】 1.1动态SQL 是 MyBatis 的强大特性之一解决拼接动态SQL时候的难题提高开发效 1.2分类 if choose(when,otherwise) trim(where,set) foreach 2.if 2.1 做 where 语句后面条件查询的,if 语句是可以拼接多条的。 2.2 需求根据学生name 做模糊查询 代码 ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttps://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecn.wjcoder.mapper.StudentMapperselect idselectLikeName resultTypecn.wjcoder.domain.Studentselect id,name,agefrom studentwhere age 20if testname ! nulland name like concat(#{name},%)/if/select /mapper public interface StudentMapper {ListStudent selectLikeName(String name); } 3.choose、when、otherwise 3.1概述 不想使用所有条件时候他们可以从多个条件中选择一个使用相当于java 的 if ... else if ... else。 3.2需求按年龄20查找如果id 不空按id 查找名字不空按名字查找否则按班级id 查找 select idselectChoose resultTypecn.wjcoder.domain.Studentselect include refidbaseSql/from studentwhere age 20choosewhen testid ! nulland id #{id}/whenwhen testname ! nulland name like concat(#{name},%)/whenotherwiseand class_id #{clsTd}/otherwise/choose/select ListStudent selectChoose(Param(id) Long id, Param(name) String name,Param(clsId) Long clsId); 3.3不传 id 参数传入name z   3.4不传入 id 参数和 name 参数 4.trim、where、set 4.1trim trim : 用于去掉或者添加标签中的内容prefix可以在 trim 标签内容前面添加内容prefixOverrides可以覆盖前面的某些内容  suffix在 trim 标签后面添加内容  suffixOverrides去掉 trim 标签内容最后面的值  4.2where where 后面直接跟 if  age null  使用了 where 标签之后解决了这些问题4.3set set 元素可以用于动态包含需要更新的列 ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttps://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecn.wjcoder.mapper.StudentMappersql idbaseSqlid,name,age/sqlupdate idupdateSetupdate studentsetif testname ! nullname #{name},/ifif testage ! nullage #{age},/if/setwhereif testid ! nullid #{id}/if/where/update void updateSet(Param(age) Integer age,Param(name)String name,Param(clsId) Long clsId,Param(id)Long id); 5.foreach foreach :用于对集合遍历。 动态 SQL 的另一个常见使用场景是对集合进行遍历尤其是在构建 IN 条件语句的时候 select idselectForeach resultTypecn.wjcoder.domain.Studentselect * from studentwhereforeach collectionids itemid indexi openid in( close) separator,#{id}/foreach/where/select ListStudent selectForeach(Param(ids) ListLong ids); collection传参的数组集合 item遍历拿到的每一个元素 index索引 open : foreach 标签内容的开始符 close : foreach 标签内容的结束符 separator分隔符 取值取的就是 item 的元素值 注意当使用 Map 对象或者 Map.Entry 对象的集合时index 是键item 是值。 6.script script:要在带注解的映射器接口类中使用动态 SQL可以使用 script 元素。 使用注解操作 mybatis 需求查询所有的学生信息用注解方式实现 Select(select * from student)ListStudent selectAll(); 更新学生信息使用 script 标签 Update({script,update student, set, if testname ! nullname#{name},/if, if testage ! nullage#{age},/if, if testclsId ! nullclass_id#{clsId},/if, /set,where id#{id},/script})void updateStu(Param(age) Integer age,Param(name)String name,Param(clsId) Long clsId,Param(id)Long id); 7.bind bind 元素允许你在 OGNL 表达式以外创建一个变量并将其绑定到当前的上下文。 需求通过用户name 进行模糊查询 select idlistLike resultTypecn.sycoder.domain.Studentbind nameret value% name %/select * from studentwhere name like #{ret}/select 二、MyBatis api 1.概述 *官方的不用 用下面这种 2.SqlSession 2.1概述 通过这个接口SqlSession来执行命令获取映射器实例和管理事务SqlSessions 是由 SqlSessionFactory 实例创建的。SqlSessionFactory 对象包含创建 SqlSession 实例的各种方法。而 SqlSessionFactory 本身是由 SqlSessionFactoryBuilder 创建的它可以从 XML、注解或 Java 配置代码来创建 SqlSessionFactory。 2.2SqlSessionFactoryBuilder 有 5 个 builder 方法 SqlSessionFactory build(InputStream inputStream) SqlSessionFactory build(InputStream inputStream, String environment) SqlSessionFactory build(InputStream inputStream, Properties properties) SqlSessionFactory build(InputStream inputStream, String env, Properties props) SqlSessionFactory build(Configuration config) 2.3SqlSessionFactory 获取方式 String resource mybatis-config.xml; InputStream inputStream Resources.getResourceAsStream(resource); SqlSessionFactoryBuilder builder new SqlSessionFactoryBuilder(); SqlSessionFactory factory builder.build(inputStream); 最终会将xml 配置文件或者 properties 配置转换成一个 Configuration ,最后一个 build 方法接受一个 Configuration 实例。Configuration 类包含了对一个 SqlSessionFactory 实例你可能关心的所有内容。 Configuration 类信息 提供了六个方法创建 SqlSession 的实例 SqlSession openSession() SqlSession openSession(boolean autoCommit) SqlSession openSession(Connection connection) SqlSession openSession(TransactionIsolationLevel level) SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) SqlSession openSession(ExecutorType execType) SqlSession openSession(ExecutorType execType, boolean autoCommit) SqlSession openSession(ExecutorType execType, Connection connection) Configuration getConfiguration(); 主要支持如下操作 事务处理你希望在 session 作用域中使用事务作用域还是使用自动提交auto-commit对很多数据库和/或 JDBC 驱动来说等同于关闭事务支持 数据库连接你希望 MyBatis 帮你从已配置的数据源获取连接还是使用自己提供的连接 语句执行你希望 MyBatis 复用 PreparedStatement 和/或批量更新语句包括插入语句和删除语句吗 默认的 openSession() 方法没有参数它会创建具备如下特性的 SqlSession 事务作用域将会开启也就是不自动提交 将由当前环境配置的 DataSource 实例中获取 Connection 对象。mybatis-config.xml 事务隔离级别将会使用驱动或数据源的默认设置(mysql 默认REPEATABLE_READ) 预处理语句不会被复用也不会批量处理更新。 如果你需要开启事务自动提交 向 autoCommit 可选参数传递 true 值即可开启自动提交功能 如果你需要提供数据库隔离级别 修改这个的值TransactionIsolationLevel 提供了枚举 如果你需要修改执行类型 修改ExecutorType值 ExecutorType.SIMPLE该类型的执行器没有特别的行为。它为每个语句的执行创建一个新的预处理语句。 ExecutorType.REUSE该类型的执行器会复用预处理语句。 ExecutorType.BATCH该类型的执行器会批量执行所有更新语句如果 SELECT 在多个更新中间执行将在必要时将多条更新语句分隔开来以方便理解。 2.4SqlSession 语句执行方法:这些方法被用来执行定义在 SQL 映射 XML 文件中的 SELECT、INSERT、UPDATE 和 DELETE 语句。 T T selectOne(String statement, Object parameter) E ListE selectList(String statement, Object parameter) T CursorT selectCursor(String statement, Object parameter) K,V MapK,V selectMap(String statement, Object parameter, String mapKey) int insert(String statement, Object parameter) int update(String statement, Object parameter) int delete(String statement, Object parameter) RowBounds可用于分页需求 int offset 100; int limit 25; RowBounds rowBounds new RowBounds(offset, limit); 立即批量更新方法(如果不调用这个方法批处理不执行只是缓存而已) ListBatchResult flushStatements() 事务控制方法 void commit() void commit(boolean force) void rollback() void rollback(boolean force) 本地缓存:Mybatis 使用到了两种缓存 本地缓存local cache:每当一个新 session 被创建MyBatis 就会创建一个与之相关联的本地缓存 二级缓存second level cache 清空本地缓存一般不去动 void clearCache() 确保 SqlSession 被关闭:如果没有使用新特性的方式一定要finally手动关闭 void close() 2.5使用映射器 方法 T T getMapper(ClassT type) 自定义方法执行最终都是调用 mybatis 的方法实现 public interface AuthorMapper {// (Author) selectOne(selectAuthor,5);Author selectAuthor(int id);// (ListAuthor) selectList(“selectAuthors”)ListAuthor selectAuthors();// (MapInteger,Author) selectMap(selectAuthors, id)MapKey(id)MapInteger, Author selectAuthors();// insert(insertAuthor, author)int insertAuthor(Author author);// updateAuthor(updateAuthor, author)int updateAuthor(Author author);// delete(deleteAuthor,5)int deleteAuthor(int id); } 映射器注解映射注解示例 插入语句 Insert(insert into table3 (id, name) values(#{nameId}, #{name})) int insertTable3(Name name); 查询语句 Results(id userResult, value {Result(property id, column uid, id true),Result(property firstName, column first_name),Result(property lastName, column last_name) }) Select(select * from users where id #{id}) User getUserById(Integer id); 三、分页查询 1.概述 MyBatis 分页插件 PageHelper是一款非常不错并且企业用得很多的mybatis 分页插件 2.如何使用 2.1引入分页插件 导入 maven 依赖 pom dependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper/artifactIdversion5.3.0/version /dependency 2.2配置拦截插件 在 spring 中配置 bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBean!-- 注意其他配置 --property namepluginsarraybean classcom.github.pagehelper.PageInterceptorproperty nameproperties!--使用下面的方式配置参数一行配置一个 --valueparamsvalue1/value/property/bean/array/property /bean 在 MyBatis 配置 xml 中配置拦截器插件 !--plugins在配置文件中的位置必须符合要求否则会报错顺序如下:properties?, settings?,typeAliases?, typeHandlers?,objectFactory?,objectWrapperFactory?,plugins?,environments?, databaseIdProvider?, mappers? -- plugins!-- com.github.pagehelper为PageHelper类所在包名 --plugin interceptorcom.github.pagehelper.PageInterceptor!-- 使用下面的方式配置参数后面会有所有的参数介绍 --property nameparam1 valuevalue1//plugin /plugins 2.3分页插件参数介绍 pageNum : 当前页码pageSize : 每页显示的数量list : 分页后的集合数据total : 总记录数 pages : 总页数prePage 上一页nextPage: 下一页。 2.4具体使用 开启分页拦截查询 //①开启分页功能,参数1是当前页码参数是每页显示的条数 PageHelper.startPage(1, 2); 执行查询 //②开始执行结果返回list ListStudent list mapper.selectAll(); Page page (Page) list; PageInfoStudent info new PageInfo(list); System.out.println(list); page 里面包含的属性 private int pageNum; private int pageSize; private long total; private int pages; 四、实战SQL常用操作 1.插入时获取主键 1.1注解方式 1.2xml 配置的方式 insert idinsertXml useGeneratedKeystrue keyPropertyid keyColumnidinsert into student values(null,#{name},#{age},#{classId}) /insert 2.模糊查询 select idlistLike1 resultTypecn.sycoder.domain.Studentselect * from studentwhere name like concat(%,#{name},%) /select select * from student where name like ? 3.批量操作 3.1批量插入 开启sqlSession 批处理 ExecutorType.BATCH,但是记得刷新 statements session.flushStatements(); try (SqlSession session sqlSessionFactory.openSession(ExecutorType.BATCH,true)) {StudentMapper mapper session.getMapper(StudentMapper.class);Student student new Student();mapper.insert(student);Student student1 new Student();mapper.insert(student1);ListBatchResult batchResults session.flushStatements();} catch (Exception e) {e.printStackTrace(); } 使用 foreach insert idbatchInsert useGeneratedKeystrue keyColumnid keyPropertyidinsert into student (name,age) valuesforeach collectionlist itemstu separator,(#{stu.name},#{stu.age})/foreach /insert int batchInsert(ListStudent list); 3.2批量删除 $(ids) Delete(delete from student where id in(${ids})) int deleteBatch(String ids); foreach delete iddeldelete from student whereforeach collectionids  itemid indexi openid in( close) separator,#{id}/foreach/where /delete 3.3批量修改用处不大 如果是给某一堆id 修改相同属性值可以使用foreach
http://www.hkea.cn/news/14368598/

相关文章:

  • 高密建设局网站甘肃省建筑信息平台
  • 网站设计与管理方向游戏平台网站开发
  • 营销型网站 平台宣传册设计与制作免费
  • 重庆做企业网站设计的公司检察机关门户网站建设自查报告6
  • 网站月付服务器抖音代运营服务内容明细
  • word模板网站asp做网站
  • vps怎么做多个网站搜索引擎优化策略
  • 郑州专业网站建设公司详情辽宁自助网站建设公司
  • 辽宁省朝阳市做网站网站开发调查问卷题
  • wordpress仪表盘空白搜索引擎优化的目的是对用户友好
  • 企业网站推广方法有哪些延津县建设局网站
  • 网站开发所需技术二手建筑铝模板哪里有卖
  • 关于当当网站建设方案乐清网论坛
  • 江苏省国家示范校建设专题网站redis wordpress 内存
  • 寺庙建设网站的意义小店网站制作
  • 一站式网站建设有哪些网剧推广一次5元
  • 教学设计代做去什么网站世界500强企业排名
  • 成都建网站公司电话北大青鸟计算机培训学费
  • 企业网站建设推广实训报告浙江省建设工程招投标网站
  • 如何在阿里巴巴上建设公司网站网站的统计代码是什么意思
  • 做影视网站犯法吗360推广平台登录入口
  • 搞定在线图片编辑seo为什么不景气了
  • 加强网站建设的意义大学生做家教网站
  • 可以做业务推广的网站有哪些内容网页设计如何添加视频
  • 黄石网站建设价格吉林市百姓网免费发布信息网
  • 海淀手机网站设计公司嘉定品牌网站建设
  • 建一个网站大概需要多少钱千锋教育广州校区
  • 做一个网站需要多少人途牛网站建设功能需求分析
  • 网站建设 珠海营销型网站建设服务商
  • 男女做那事是什 网站网站建设多少费用