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

学习电子商务网站建设与管理的感想正定城乡建设网站

学习电子商务网站建设与管理的感想,正定城乡建设网站,惠州网络推广,广州网站备案要求目录 和其它持久化层技术对比 搭建MyBatis 开发环境 创建maven工程 创建MyBatis的核心配置文件 创建mapper接口 创建MyBatis的映射文件 通过junit测试功能 加入log4j日志功能 核心配置文件详解 MyBatis的增删改查 新增 删除 修改 查询一个实体类对象 查询list集…目录 和其它持久化层技术对比 搭建MyBatis 开发环境 创建maven工程 创建MyBatis的核心配置文件 创建mapper接口 创建MyBatis的映射文件 通过junit测试功能 加入log4j日志功能 核心配置文件详解 MyBatis的增删改查 新增 删除 修改 查询一个实体类对象 查询list集合 MyBatis获取参数值的两种方式 MyBatis的各种查询功能 查询一个实体类对象 查询一个list集合 查询单个数据 查询一条数据为map集合 查询多条数据为map集合 特殊SQL的执行 模糊查询 批量删除 动态设置表名 添加功能获取自增的主键 自定义映射resultMap resultMap处理字段和属性的映射关系 多对一映射处理 级联方式处理映射关系 使用association处理映射关系 分步查询 一对多映射处理 collection 分步查询 动态SQL if where trim choose、when、otherwise foreach SQL片段 MyBatis的缓存 MyBatis的一级缓存 MyBatis的二级缓存 MyBatis缓存查询的顺序 整合第三方缓存EHCache 分页插件 分页插件的使用步骤 分页插件的使用 和其它持久化层技术对比 JDBC SQL 夹杂在Java代码中耦合度高导致硬编码内伤 维护不易且实际开发需求中 SQL 有变化频繁修改的情况多见 代码冗长开发效率低 Hibernate 和 JPA 操作简便开发效率高 程序中的长难复杂 SQL 需要绕过框架 内部自动生产的 SQL不容易做特殊优化 基于全映射的全自动框架大量字段的 POJO 进行部分映射时比较困难。 反射操作太多导致数据库性能下降 MyBatis 轻量级性能出色 SQL 和 Java 编码分开功能边界清晰。Java代码专注业务、SQL语句专注数据 开发效率稍逊于HIbernate但是完全能够接受 搭建MyBatis 开发环境 IDEidea 2021.1 构建工具maven 3.6.1 MySQL版本MySQL 8 MyBatis版本MyBatis 3.5.7 MySQL不同版本的注意事项1、驱动类driver-class-name MySQL 5版本使用jdbc5驱动驱动类使用com.mysql.jdbc.Driver MySQL 8版本使用jdbc8驱动驱动类使用com.mysql.cj.jdbc.Driver2、连接地址url MySQL 5版本的url jdbc:mysql://localhost:3306/ssm MySQL 8版本的url jdbc:mysql://localhost:3306/ssm?serverTimezoneUTC否则运行测试用例报告如下错误 java.sql.SQLException: The server time zone value Öйú±ê׼ʱ¼ä is unrecognized or represents more 创建maven工程 ①打包方式jar ②引入依赖 dependencies!-- Mybatis核心 --dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.7/version/dependency!-- junit测试 --dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope/dependency!-- MySQL驱动 --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.16/version/dependency /dependencies 创建MyBatis的核心配置文件 习惯上命名为mybatis-config.xml  核心配置文件存放的位置是src/main/resources目录下   ?xml version1.0 encodingUTF-8 ? !DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd configuration!--设置连接数据库的环境--environments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/ssm?serverTimezoneUTC/property nameusername valueroot/property namepassword value123456//dataSource/environment/environments !--引入映射文件-- mapperspackage namemappers/UserMapper.xml/ /mappers /configuration 创建mapper接口 public interface UserMapper {/*** 添加用户信息*/int insertUser(); } 创建MyBatis的映射文件 相关概念ORMObject Relationship Mapping对象关系映射。 对象Java的实体类对象 关系关系型数据库 映射二者之间的对应关系 Java概念数据库概念类表属性字段/列对象记录/行 ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.atguigu.mybatis.mapper.UserMapper!--int insertUser();--nsert idinsertUserinsert into t_user values(null,admin,123456,23,男,12345qq.com)/insert /mapper 通过junit测试功能 //读取MyBatis的核心配置文件 InputStream is Resources.getResourceAsStream(mybatis-config.xml); //创建SqlSessionFactoryBuilder对象 SqlSessionFactoryBuilder sqlSessionFactoryBuilder new SqlSessionFactoryBuilder(); //通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory生产SqlSession对象 SqlSessionFactory sqlSessionFactory sqlSessionFactoryBuilder.build(is); //创建SqlSession对象此时通过SqlSession对象所操作的sql都必须手动提交或回滚事务 //SqlSession sqlSession sqlSessionFactory.openSession(); //创建SqlSession对象此时通过SqlSession对象所操作的sql都会自动提交 SqlSession sqlSession sqlSessionFactory.openSession(true); //通过代理模式创建UserMapper接口的代理实现类对象 UserMapper userMapper sqlSession.getMapper(UserMapper.class); //调用UserMapper接口中的方法就可以根据UserMapper的全类名匹配元素文件通过调用的方法名匹配 映射文件中的SQL标签并执行标签中的SQL语句 int result userMapper.insertUser(); //sqlSession.commit(); System.out.println(结果result); SqlSession代表Java程序和数据库之间的会话HttpSession是Java程序和浏览器之间的会话 SqlSessionFactory是“生产”SqlSession的“工厂”。 工厂模式如果创建某一个对象使用的过程基本固定那么我们就可以把创建这个对象的 相关代码封装到一个“工厂类”中以后都使用这个工厂类来“生产”我们需要的对象。 加入log4j日志功能 ①加入依赖 !-- log4j日志 -- dependencygroupIdlog4j/groupIdartifactIdlog4j/artifactIdversion1.2.17/version /dependency ②加入log4j的配置文件 log4j的配置文件名为log4j.xml存放的位置是src/main/resources目录下 ?xml version1.0 encodingUTF-8 ? !DOCTYPE log4j:configuration SYSTEM log4j.dtd log4j:configuration xmlns:log4jhttp://jakarta.apache.org/log4j/appender nameSTDOUT classorg.apache.log4j.ConsoleAppenderparam nameEncoding valueUTF-8 /layout classorg.apache.log4j.PatternLayoutparam nameConversionPattern value%-5p %d{MM-dd HH:mm:ss,SSS}%m (%F:%L) \n //layout/appenderlogger namejava.sqllevel valuedebug //loggerlogger nameorg.apache.ibatislevel valueinfo //loggerrootlevel valuedebug /appender-ref refSTDOUT //root /log4j:configuration 日志的级别 FATAL(致命)ERROR(错误)WARN(警告)INFO(信息)DEBUG(调试) 从左到右打印的内容越来越详细 核心配置文件详解 核心配置文件中的标签必须按照固定的顺序 properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,refl ectorFactory?,plugins?,environments?,databaseIdProvider?,mappers? ?xml version1.0 encodingUTF-8 ? !DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd configuration!--MyBatis核心配置文件中标签的顺序properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?--!--引入properties文件--properties resourcejdbc.properties /!--设置类型别名--typeAliases!--typeAlias设置某个类型的别名属性type设置需要设置别名的类型alias设置某个类型的别名若不设置该属性那么该类型拥有默认的别名即类名且不区分大小写--!--typeAlias typecom.atguigu.mybatis.pojo.User/typeAlias--!--以包为单位将包下所有的类型设置默认的类型别名即类名且不区分大小写--package namecom.atguigu.mybatis.pojo//typeAliases!--environments配置多个连接数据库的环境属性default设置默认使用的环境的id--environments defaultdevelopment!--environment配置某个具体的环境属性id表示连接数据库的环境的唯一标识不能重复--environment iddevelopment!--transactionManager设置事务管理方式属性typeJDBC|MANAGEDJDBC表示当前环境中执行SQL时使用的是JDBC中原生的事务管理方式事务的提交或回滚需要手动处理MANAGED被管理例如Spring--transactionManager typeJDBC/!--dataSource配置数据源属性type设置数据源的类型typePOOLED|UNPOOLED|JNDIPOOLED表示使用数据库连接池缓存数据库连接UNPOOLED表示不使用数据库连接池JNDI表示使用上下文中的数据源--dataSource typePOOLED!--设置连接数据库的驱动--property namedriver value${jdbc.driver}/!--设置连接数据库的连接地址--property nameurl value${jdbc.url}/!--设置连接数据库的用户名--property nameusername value${jdbc.username}/!--设置连接数据库的密码--property namepassword value${jdbc.password}//dataSource/environmentenvironment idtesttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver valuecom.mysql.cj.jdbc.Driver/property nameurlvaluejdbc:mysql://localhost:3306/ssmserverTimezoneUTC/property nameusername valueroot/property namepassword value123456//dataSource/environment/environments!--引入映射文件--mappers!--mapper resourcemappers/UserMapper.xml/--!--以包为单位引入映射文件要求1、mapper接口所在的包要和映射文件所在的包一致2、mapper接口要和映射文件的名字一致--package namecom.atguigu.mybatis.mapper//mappers /configuration MyBatis的增删改查 新增 !--int insertUser();-- insert idinsertUserinsert into t_user values(null,admin,123456,23,男) /insert 删除 !--int deleteUser();-- delete iddeleteUserdelete from t_user where id 7 /delete 修改 !--int updateUser();-- update idupdateUserupdate t_user set usernameybc,password123 where id 6 /update 查询一个实体类对象 !--User getUserById();-- select idgetUserById resultTypecom.atguigu.mybatis.bean.Userselect * from t_user where id 2 /select 查询list集合 !--ListUser getUserList();-- select idgetUserList resultTypecom.atguigu.mybatis.bean.Userselect * from t_user /select 注意 1、查询的标签select必须设置属性resultType或resultMap用于设置实体类和数据库表的映射 关系 resultType自动映射用于属性名和表中字段名一致的情况 resultMap自定义映射用于一对多或多对一或字段名和属性名不一致的情况 MyBatis获取参数值的两种方式 MyBatis获取参数值的两种方式**${}**和**#{}**${}的本质就是字符串拼接#{}的本质就是占位符赋值${}使用字符串拼接的方式拼接sql若为字符串类型或日期类型的字段进行赋值时需要手动加单引号 但是#{}使用占位符赋值的方式拼接sql此时为字符串类型或日期类型的字段进行赋值时可以自动添加单引号 MyBatis的各种查询功能 查询一个实体类对象 /** * 根据用户id查询用户信息 * param id * return */ User getUserById(Param(id) int id); !--User getUserById(Param(id) int id);-- select idgetUserById resultTypeUserselect * from t_user where id #{id} /select 查询一个list集合 /** * 查询所有用户信息 * return */ ListUser getUserList(); !--ListUser getUserList();-- select idgetUserList resultTypeUserselect * from t_user /select 当查询的数据为多条时不能使用实体类作为返回值否则会抛出异常 TooManyResultsException但是若查询的数据只有一条可以使用实体类或集合作为返回值 查询单个数据 /** * 查询用户的总记录数 * return * 在MyBatis中对于Java中常用的类型都设置了类型别名 * 例如 java.lang.Integer--int|integer * 例如 int--_int|_integer * 例如 Map--map,List--list */ int getCount(); !--int getCount();-- select idgetCount resultType_integerselect count(id) from t_user /select 查询一条数据为map集合 /** * 根据用户id查询用户信息为map集合 * param id * return */ MapString, Object getUserToMap(Param(id) int id); !--MapString, Object getUserToMap(Param(id) int id);-- !--结果 {password123456, sex男 , id1, age23, usernameadmin}-- select idgetUserToMap resultTypemapselect * from t_user where id #{id} /select 查询多条数据为map集合 ①方式一 /** * 查询所有用户信息为map集合 * return * 将表中的数据以map集合的方式查询一条数据对应一个map若有多条数据就会产生多个map集合此 时可以将这些map放在一个list集合中获取 */ ListMapString, Object getAllUserToMap(); !--MapString, Object getAllUserToMap();-- select idgetAllUserToMap resultTypemapselect * from t_user /select ②方式二 /** * 查询所有用户信息为map集合 * return * 将表中的数据以map集合的方式查询一条数据对应一个map若有多条数据就会产生多个map集合并 且最终要以一个map的方式返回数据此时需要通过MapKey注解设置map集合的键值是每条数据所对应的 map集合 */ MapKey(id) MapString, Object getAllUserToMap(); !--MapString, Object getAllUserToMap();-- !-- {1{password123456, sex男, id1, age23, usernameadmin},2{password123456, sex男, id2, age23, username张三},3{password123456, sex男, id3, age23, username张三} } -- select idgetAllUserToMap resultTypemapselect * from t_user /select 特殊SQL的执行 模糊查询 /** * 测试模糊查询 * param mohu * return */ ListUser testMohu(Param(mohu) String mohu); !--ListUser testMohu(Param(mohu) String mohu);-- select idtestMohu resultTypeUser!--select * from t_user where username like %${mohu}%--!--select * from t_user where username like concat(%,#{mohu},%)--select * from t_user where username like %#{mohu}% /select 批量删除 /** * 批量删除 * param ids * return */ int deleteMore(Param(ids) String ids); !--int deleteMore(Param(ids) String ids);-- delete iddeleteMoredelete from t_user where id in (${ids}) /delete 动态设置表名 /** * 动态设置表名查询所有的用户信息 * param tableName * return */ ListUser getAllUser(Param(tableName) String tableName); !--ListUser getAllUser(Param(tableName) String tableName);-- select idgetAllUser resultTypeUserselect * from ${tableName} /select 添加功能获取自增的主键 场景模拟 t_clazz(clazz_id,clazz_name) t_student(student_id,student_name,clazz_id) 1、添加班级信息 2、获取新添加的班级的id 3、为班级分配学生即将某学的班级id修改为新添加的班级的id /** * 添加用户信息 * param user * return * useGeneratedKeys设置使用自增的主键 * keyProperty因为增删改有统一的返回值是受影响的行数因此只能将获取的自增的主键放在传输的参数user对象的某个属性中 */ int insertUser(User user); !--int insertUser(User user);-- insert idinsertUser useGeneratedKeystrue keyPropertyidinsert into t_user values(null,#{username},#{password},#{age},#{sex}) /insert 自定义映射resultMap resultMap处理字段和属性的映射关系 若字段名和实体类中的属性名不一致则可以通过resultMap设置自定义映射 !--resultMap设置自定义映射属性id表示自定义映射的唯一标识type查询的数据要映射的实体类的类型子标签id设置主键的映射关系result设置普通字段的映射关系association设置多对一的映射关系collection设置一对多的映射关系属性property设置映射关系中实体类中的属性名column设置映射关系中表中的字段名 -- resultMap iduserMap typeUserid propertyid columnid/idresult propertyuserName columnuser_name/resultresult propertypassword columnpassword/resultresult propertyage columnage/resultresult propertysex columnsex/result /resultMap !--ListUser testMohu(Param(mohu) String mohu);-- select idtestMohu resultMapuserMap!--select * from t_user where username like %${mohu}%-- select id,user_name,password,age,sex from t_user where user_name likeconcat(%,#{mohu},%) /select 若字段名和实体类中的属性名不一致但是字段名符合数据库的规则使用_实体类中的属性 名符合Java的规则使用驼峰 此时也可通过以下两种方式处理字段名和实体类中的属性的映射关系 a可以通过为字段起别名的方式保证和实体类中的属性名保持一致 b可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase可 以在查询表中数据时自动将_类型的字段名转换为驼峰 例如字段名user_name设置了mapUnderscoreToCamelCase此时字段名就会转换为 userName 多对一映射处理 场景模拟 查询员工信息以及员工所对应的部门信息 级联方式处理映射关系 resultMap idempDeptMap typeEmpid columneid propertyeid/idresult columnename propertyename/resultresult columnage propertyage/resultresult columnsex propertysex/resultresult columndid propertydept.did/resultresult columndname propertydept.dname/result /resultMap !--Emp getEmpAndDeptByEid(Param(eid) int eid);-- select idgetEmpAndDeptByEid resultMapempDeptMapselect emp.*,dept.* from t_emp emp left join t_dept dept on emp.did dept.did where emp.eid #{eid} /select 使用association处理映射关系 resultMap idempDeptMap typeEmpid columneid propertyeid/idresult columnename propertyename/resultresult columnage propertyage/resultresult columnsex propertysex/resultassociation propertydept javaTypeDeptid columndid propertydid/idresult columndname propertydname/result/association /resultMap !--Emp getEmpAndDeptByEid(Param(eid) int eid);-- select idgetEmpAndDeptByEid resultMapempDeptMapselect emp.*,dept.* from t_emp emp left join t_dept dept on emp.did dept.did where emp.eid #{eid} /select 分步查询 ①查询员工信息 /** * 通过分步查询查询员工信息 * param eid * return */ Emp getEmpByStep(Param(eid) int eid); resultMap idempDeptStepMap typeEmpid columneid propertyeid/idresult columnename propertyename/resultresult columnage propertyage/resultresult columnsex propertysex/result!--select设置分步查询查询某个属性的值的sql的标识namespace.sqlIdcolumn将sql以及查询结果中的某个字段设置为分步查询的条件--association propertydeptselectcom.atguigu.MyBatis.mapper.DeptMapper.getEmpDeptByStep columndid /association /resultMap !--Emp getEmpByStep(Param(eid) int eid);-- select idgetEmpByStep resultMapempDeptStepMapselect * from t_emp where eid #{eid} /select ②根据员工所对应的部门id查询部门信息 /** * 分步查询的第二步 根据员工所对应的did查询部门信息 * param did * return */ Dept getEmpDeptByStep(Param(did) int did); !--Dept getEmpDeptByStep(Param(did) int did);-- select idgetEmpDeptByStep resultTypeDeptselect * from t_dept where did #{did} /select 一对多映射处理 collection /** * 根据部门id查新部门以及部门中的员工信息 * param did * return */ Dept getDeptEmpByDid(Param(did) int did); resultMap iddeptEmpMap typeDeptid propertydid columndid/idresult propertydname columndname/result!--ofType设置collection标签所处理的集合属性中存储数据的类型--collection propertyemps ofTypeEmpid propertyeid columneid/idresult propertyename columnename/resultresult propertyage columnage/resultresult propertysex columnsex/result/collection /resultMap !--Dept getDeptEmpByDid(Param(did) int did);-- select idgetDeptEmpByDid resultMapdeptEmpMapselect dept.*,emp.* from t_dept dept left join t_emp emp on dept.did emp.did where dept.did #{did} /select 分步查询 ①查询部门信息 /** * 分步查询部门和部门中的员工 * param did * return */ Dept getDeptByStep(Param(did) int did); resultMap iddeptEmpStep typeDeptid propertydid columndid/idresult propertydname columndname/resultcollection propertyemps fetchTypeeagerselectcom.atguigu.MyBatis.mapper.EmpMapper.getEmpListByDid columndid/collection /resultMap !--Dept getDeptByStep(Param(did) int did);-- select idgetDeptByStep resultMapdeptEmpStepselect * from t_dept where did #{did} /select ②根据部门id查询部门中的所有员工 /** * 根据部门id查询员工信息 * param did * return */ ListEmp getEmpListByDid(Param(did) int did); !--ListEmp getEmpListByDid(Param(did) int did);-- select idgetEmpListByDid resultTypeEmpselect * from t_emp where did #{did} /select 分步查询的优点可以实现延迟加载 但是必须在核心配置文件中设置全局配置信息 lazyLoadingEnabled延迟加载的全局开关。当开启时所有关联对象都会延迟加载 aggressiveLazyLoading当开启时任何方法的调用都会加载该对象的所有属性。否则每个属性会按需加载 此时就可以实现按需加载获取的数据是什么就只会执行相应的sql。此时可通过association和 collection中的fetchType属性设置当前的分步查询是否使用延迟加载 fetchTypelazy(延迟加 载)|eager(立即加载) 动态SQL Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能它存在的意义是为了 解决 拼接SQL语句字符串时的痛点问题。 if if标签可通过test属性的表达式进行判断若表达式的结果为true则标签中的内容会执行反之标签中的内容不会执行 !--ListEmp getEmpListByCondition(Emp emp);-- select idgetEmpListByMoreTJ resultTypeEmpselect * from t_emp where 11if testename ! and ename ! nulland ename #{ename}/ifif testage ! and age ! nulland age #{age}/ifif testsex ! and sex ! nulland sex #{sex}/if /select where where和if一般结合使用 a 若where标签中的if条件都不满足则where标签没有任何功能即不会添加where关键字 b 若where标签中的if条件满足则where标签会自动添加where关键字并将条件最前方多余的and去掉 注意where标签不能去掉条件最后多余的and select idgetEmpListByMoreTJ2 resultTypeEmpselect * from t_empwhereif testename ! and ename ! nullename #{ename}/ifif testage ! and age ! nulland age #{age}/ifif testsex ! and sex ! nulland sex #{sex}/if/where /select trim trim用于去掉或添加标签中的内容 常用属性 prefix在trim标签中的内容的前面添加某些内容 prefixOverrides在trim标签中的内容的前面去掉某些内容 suffix在trim标签中的内容的后面添加某些内容 suffixOverrides在trim标签中的内容的后面去掉某些内容 select idgetEmpListByMoreTJ resultTypeEmpselect * from t_emptrim prefixwhere suffixOverridesandif testename ! and ename ! nullename #{ename} and/ifif testage ! and age ! nullage #{age} and/ifif testsex ! and sex ! nullsex #{sex}/if/trim /select choose、when、otherwise choose、when、 otherwise相当于if...else if..else !--ListEmp getEmpListByChoose(Emp emp);-- select idgetEmpListByChoose resultTypeEmpselect include refidempColumns/include from t_empwherechoosewhen testename ! and ename ! nullename #{ename}/whenwhen testage ! and age ! nullage #{age}/whenwhen testsex ! and sex ! nullsex #{sex}/whenwhen testemail ! and email ! nullemail #{email}/when/choose/where /select foreach !--int insertMoreEmp(ListEmp emps);-- insert idinsertMoreEmpinsert into t_emp valuesforeach collectionemps itememp separator,(null,#{emp.ename},#{emp.age},#{emp.sex},#{emp.email},null)/foreach /insert !--int deleteMoreByArray(int[] eids);-- delete iddeleteMoreByArraydelete from t_emp whereforeach collectioneids itemeid separatororeid #{eid}/foreach /delete !--int deleteMoreByArray(int[] eids);-- delete iddeleteMoreByArraydelete from t_emp where eid inforeach collectioneids itemeid separator, open( close)#{eid}/foreach /delete SQL片段 sql片段可以记录一段公共sql片段在使用的地方通过include标签进行引入 sql idempColumnseid,ename,age,sex,did /sql select include refidempColumns/include from t_emp MyBatis的缓存 MyBatis的一级缓存 一级缓存是SqlSession级别的通过同一个SqlSession查询的数据会被缓存下次查询相同的数据就会从缓存中直接获取不会从数据库重新访问 使一级缓存失效的四种情况 不同的SqlSession对应不同的一级缓存 同一个SqlSession但是查询条件不同 同一个SqlSession两次查询期间执行了任何一次增删改操作 同一个SqlSession两次查询期间手动清空了缓存 MyBatis的二级缓存 二级缓存是SqlSessionFactory级别通过同一个SqlSessionFactory创建的SqlSession查询的结果 会被缓存此后若再次执行相同的查询语句结果就会从缓存中获取二级缓存开启的条件 a在核心配置文件中设置全局配置属性cacheEnabledtrue默认为true不需要设置 b在映射文件中设置标签cache/ c二级缓存必须在SqlSession关闭或提交之后有效 d查询的数据所转换的实体类类型必须实现序列化的接口使二级缓存失效的情况 两次查询之间执行了任意的增删改会使一级和二级缓存同时失效 MyBatis缓存查询的顺序 先查询二级缓存因为二级缓存中可能会有其他程序已经查出来的数据可以拿来直接使用。 如果二级缓存没有命中再查询一级缓存 如果一级缓存也没有命中则查询数据库 SqlSession关闭之后一级缓存中的数据会写入二级缓存 整合第三方缓存EHCache 添加依赖 !-- Mybatis EHCache整合包 -- dependencygroupIdorg.mybatis.caches/groupIdartifactIdmybatis-ehcache/artifactIdversion1.2.1/version /dependency !-- slf4j日志门面的一个具体实现 -- dependencygroupIdch.qos.logback/groupIdartifactIdlogback-classic/artifactIdversion1.2.3/version /dependency 各jar包功能 jar包名称作用mybatis-ehcacheMybatis和EHCache的整合包ehcacheEHCache核心包slf4j-apiSLF4J日志门面包logback-classic支持SLF4J门面接口的一个具体实现 创建EHCache的配置文件ehcache.xml ?xml version1.0 encodingutf-8 ? ehcache xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation../config/ehcache.xsd!-- 磁盘保存路径 --diskStore pathD:\atguigu\ehcache/defaultCachemaxElementsInMemory1000maxElementsOnDisk10000000eternalfalseoverflowToDisktruetimeToIdleSeconds120timeToLiveSeconds120diskExpiryThreadIntervalSeconds120memoryStoreEvictionPolicyLRU/defaultCache /ehcache 设置二级缓存的类型 cache typeorg.mybatis.caches.ehcache.EhcacheCache/ 加入logback日志 存在SLF4J时作为简易日志的log4j将失效此时我们需要借助SLF4J的具体实现logback来打印日志。 创建logback的配置文件logback.xml ?xml version1.0 encodingUTF-8? configuration debugtrue!-- 指定日志输出的位置 --appender nameSTDOUT classch.qos.logback.core.ConsoleAppenderencoder!-- 日志输出的格式 --!-- 按照顺序分别是 时间、日志级别、线程名称、打印日志的类、日志主体内容、换行--pattern[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger][%msg]%n/pattern/encoder/appender!-- 设置全局日志级别。日志级别按顺序分别是 DEBUG、INFO、WARN、ERROR --!-- 指定任何一个日志级别都只打印当前级别和后面级别的日志。 --root levelDEBUG!-- 指定打印日志的appender这里通过“STDOUT”引用了前面配置的appender --appender-ref refSTDOUT //root!-- 根据特殊需求指定局部日志级别 --logger namecom.atguigu.crowd.mapper levelDEBUG/ /configuration 分页插件 limit index,pageSizepageSize每页显示的条数pageNum当前页的页码index当前页的起始索引index(pageNum-1)*pageSizecount总记录数totalPage总页数totalPage count / pageSize;if(count % pageSize ! 0){totalPage 1;}pageSize4pageNum1index0 limit 0,4pageSize4pageNum3index8 limit 8,4pageSize4pageNum6index20 limit 8,4首页 上一页 2 3 4 5 6 下一页 末页 分页插件的使用步骤 ①添加依赖 dependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper/artifactIdversion5.2.0/version /dependency ②配置分页插件 在MyBatis的核心配置文件中配置插件 plugins!--设置分页插件--plugin interceptorcom.github.pagehelper.PageInterceptor/plugin /plugins 分页插件的使用 a 在查询功能之前使用PageHelper.startPage(int pageNum, int pageSize)开启分页功能 pageNum当前页的页码 pageSize每页显示的条数 b 在查询获取list集合之后使用PageInfoT pageInfo new PageInfo(ListT list, int navigatePages)获取分页相关数据 list分页之后的数据 navigatePages导航分页的页码数 c 分页相关数据 PageInfo{pageNum8, pageSize4, size2, startRow29, endRow30, total30, pages8,listPage{counttrue, pageNum8, pageSize4, startRow28, endRow32, total30,pages8, reasonablefalse, pageSizeZerofalse},prePage7, nextPage0, isFirstPagefalse, isLastPagetrue, hasPreviousPagetrue,hasNextPagefalse, navigatePages5, navigateFirstPage4, navigateLastPage8,navigatepageNums[4, 5, 6, 7, 8]}pageNum当前页的页码pageSize每页显示的条数size当前页显示的真实条数total总记录数pages总页数prePage上一页的页码nextPage下一页的页码isFirstPage/isLastPage是否为第一页/最后一页hasPreviousPage/hasNextPage是否存在上一页/下一页navigatePages导航分页的页码数navigatepageNums导航分页的页码[1,2,3,4,5] 来源【尚硅谷】SSM框架全套教程MyBatisSpringSpringMVCSSM整合一套通关
http://www.w-s-a.com/news/298117/

相关文章:

  • wordpress更换图标seo网站建设公司
  • 网站备案 深圳小程序怎么进入公众号
  • 实名认证域名可以做电影网站吗坪山网站设计的公司
  • wdcp怎么上传做好的网站管理咨询公司名称参考
  • 设计师网站pin分销系统小程序开发
  • 高端品牌网站建设兴田德润实惠企业网站建设应该怎么做
  • 做研学的网站优秀软文案例
  • 网站个人简介怎么做建设网站卡盟
  • 影楼做网站安庆建设机械网站
  • 访问网站的原理wix做网站流程
  • 众鱼深圳网站建设设计师网名叫什么好听
  • 中小学生做试卷的网站6网站建设需要注意哪些细节
  • 以个人名义做地方门户网站社保服务个人网站
  • 上海企业做网站设计制作感悟150字
  • asp.netmvc网站开发ps设计网页
  • win2008 挂网站 404官方网站是什么
  • 网站只做内容 不做外链做姓氏图的网站
  • 中国建设银行信用卡黑名单网站wordpress怎么解密密码
  • 建设银行如何网站设置密码广州网站营销推广
  • 企业做网站的步骤与做网站注意事项四川省住房建设厅网站打不开
  • 网页设计网站规划报告百度文库官网登录入口
  • 郑州医疗网站开发wordpress能注册
  • 创建网站的英语石家庄微信网站建设
  • 分享几个x站好用的关键词微信商城小程序开发一般需要多少钱
  • 做韩国外贸网站wordpress手机版中文
  • 建站群赚钱有前途吗蚌埠北京网站建设
  • 北京网站建设求职简历十堰seo优化教程
  • 网站顶部可关闭广告微信小程序多少钱
  • 网站背景怎么弄斜杠青年seo工作室
  • ps个人网站首页怎么制作如何做网站的版块规划