淮南市住房与城乡建设部网站,做徽标哪个网站素材多,做酒店网站所用到的算法,中国电信广告视频一、XML 映射器
1.概述
使用 xml 文件去配置 SQL 代码#xff0c;比传统的 jdbc 简单方便#xff0c;能够少写代码#xff0c;减少使用成本#xff0c;提高工作效率。
1.1SQL 映射文件中的顶级元素
cache – 该命名空间的缓存配置。
cache-ref – 引用其它命名空间的缓…一、XML 映射器
1.概述
使用 xml 文件去配置 SQL 代码比传统的 jdbc 简单方便能够少写代码减少使用成本提高工作效率。
1.1SQL 映射文件中的顶级元素
cache – 该命名空间的缓存配置。
cache-ref – 引用其它命名空间的缓存配置。
resultMap – 描述如何从数据库结果集中加载对象是最复杂也是最强大的元素。
parameterMap – 老式风格的参数映射。此元素已被废弃并可能在将来被移除请使用行内参数映射。文档中不会介绍此元素。
sql – 可被其它语句引用的可重用语句块。
insert – 映射插入语句。
update – 映射更新语句。
delete – 映射删除语句。
select – 映射查询语句
二、select
查询语句是 MyBatis 中最常用的元素之一
1.Select 元素的属性
属性使用目的描述id和Mapper 接口方法名关联在命名空间中唯一的标识符可以被用来引用这条语句。parameterType将会传入这条语句的参数的类全限定名或别名。这个属性是可选的因为 MyBatis 可以通过类型处理器TypeHandler推断出具体传入语句的参数默认值为未设置unset。parameterMap用于引用外部 parameterMap 的属性目前已被废弃。请使用行内参数映射和 parameterType 属性。resultType返回结果集期望从这条语句中返回结果的类全限定名或别名。注意如果返回的是集合那应该设置为集合包含的类型而不是集合本身的类型。resultType 和 resultMap 之间只能同时使用一个。resultMap返回结果集对外部 resultMap 的命名引用。结果映射是 MyBatis 最强大的特性如果你对其理解透彻许多复杂的映射问题都能迎刃而解。resultType 和 resultMap 之间只能同时使用一个。flushCache将其设置为 true 后只要语句被调用都会导致本地缓存和二级缓存被清空默认值false。useCache用于开启缓存将其设置为 true 后将会导致本条语句的结果被二级缓存缓存起来默认值对 select 元素为 true。timeout这个设置是在抛出异常之前驱动程序等待数据库返回请求结果的秒数。默认值为未设置unset依赖数据库驱动。fetchSize这是一个给驱动的建议值尝试让驱动程序每次批量返回的结果行数等于这个设置值。默认值为未设置unset依赖驱动。statementType默认值PREPARED。可选 STATEMENTPREPARED 或 CALLABLE。这会让 MyBatis 分别使用 StatementPreparedStatement 或 CallableStatement默认值PREPARED。resultSetTypeFORWARD_ONLYSCROLL_SENSITIVE, SCROLL_INSENSITIVE 或 DEFAULT等价于 unset 中的一个默认值为 unset 依赖数据库驱动。databaseId如果配置了数据库厂商标识databaseIdProviderMyBatis 会加载所有不带 databaseId 或匹配当前 databaseId 的语句如果带和不带的语句都有则不带的会被忽略。resultOrdered这个设置仅针对嵌套结果 select 语句如果为true则假设结果集以正确顺序排序后执行映射当返回新的主结果行时将不再发生对以前结果行的引用。 这样可以减少内存消耗。默认值false。resultSets适用于多结果集的情况这个设置仅适用于多结果集的情况。它将列出语句执行后返回的结果集并赋予每个结果集一个名称多个名称之间以逗号分隔。
2.常用设置
selectidselectPersonresultTypehashmapresultMappersonResultMaptimeout10 #除非有特殊需求否则不用
三、insert, update 和 delete
1.Insert, Update, Delete 元素的属性
属性描述是否使用id在命名空间中唯一的标识符可以被用来引用这条语句。关联mapper 和 xml 语句parameterType将会传入这条语句的参数的类全限定名或别名。这个属性是可选的因为 MyBatis 可以通过类型处理器TypeHandler推断出具体传入语句的参数默认值为未设置unset。parameterMap用于引用外部 parameterMap 的属性目前已被废弃。请使用行内参数映射和 parameterType 属性。flushCache将其设置为 true 后只要语句被调用都会导致本地缓存和二级缓存被清空默认值对 insert、update 和 delete 语句true。默认使用刷新缓存timeout这个设置是在抛出异常之前驱动程序等待数据库返回请求结果的秒数。默认值为未设置unset依赖数据库驱动。statementType可选 STATEMENTPREPARED 或 CALLABLE。这会让 MyBatis 分别使用 StatementPreparedStatement 或 CallableStatement默认值PREPARED。useGeneratedKeys仅适用于 insert 和 update这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键比如像 MySQL 和 SQL Server 这样的关系型数据库管理系统的自动递增字段默认值false。获取数据库生成的主键idkeyProperty仅适用于 insert 和 update指定能够唯一识别对象的属性MyBatis 会使用 getGeneratedKeys 的返回值或 insert 语句的 selectKey 子元素设置它的值默认值未设置unset。如果生成列不止一个可以用逗号分隔多个属性名称。keyPropertyidkeyColumn仅适用于 insert 和 update设置生成键值在表中的列名在某些数据库像 PostgreSQL中当主键列不是表中的第一列的时候是必须设置的。如果生成列不止一个可以用逗号分隔多个属性名称。databaseId如果配置了数据库厂商标识databaseIdProviderMyBatis 会加载所有不带 databaseId 或匹配当前 databaseId 的语句如果带和不带的语句都有则不带的会被忽略。
2.常用配置
insert idinsertAuthor useGeneratedKeystruekeyPropertyidinsert into Author (username,password,email,bio)values (#{username},#{password},#{email},#{bio})
/insert 2.1 selectKey
属性描述keyProperty |selectKey 语句结果应该被设置到的目标属性。如果生成列不止一个可以用逗号分隔多个属性名称。keyColumn返回结果集中生成列属性的列名。如果生成列不止一个可以用逗号分隔多个属性名称。resultType结果的类型。通常 MyBatis 可以推断出来但是为了更加准确写上也不会有什么问题。MyBatis 允许将任何简单类型用作主键的类型包括字符串。如果生成列不止一个则可以使用包含期望属性的 Object 或 Map。order可以设置为 BEFORE 或 AFTER。如果设置为 BEFORE那么它首先会生成主键设置 keyProperty 再执行插入语句。如果设置为AFTER那么先执行插入语句然后是selectKey 中的语句 - 这和 Oracle数据库的行为相似在插入语句内部可能有嵌入索引调用。statementType和前面一样MyBatis 支持 STATEMENTPREPARED 和 CALLABLE 类型的映射语句分别代表 Statement, PreparedStatement 和 CallableStatement 类型。
selectKeykeyPropertyidresultTypeintorderBEFOREstatementTypePREPARED
四、抽取共同sql
注意mybatis spring 底层都会用到反射创建对象
1.作用
sql可以用来定义可重用的 SQL 代码片段
2.具体使用
sql iduserColumns ${alias}.id,${alias}.username,${alias}.password /sql
select idselectUsers resultTypemapselectinclude refiduserColumnsproperty namealias valuet1//include,include refiduserColumnsproperty namealias valuet2//includefrom some_table t1cross join some_table t2
/select
sql idbasicSelectt1.id id,t1.name name,t1.age age,t1.address address/sql
select idselectSql resultTypecn.wjcoder.domain.Employeeselectinclude refidbasicSelect/includefrom employee t1 where t1.id #{id}
/select
五、参数
1.#{参数名称}
1.1原理
使用 #{}参数语法时MyBatis 会创建 PreparedStatement参数占位符并通过占位符安全地设置参数就像使用 ? 一样 1.2特点安全、快速、首选安全
2.${参数名称}
${参数名称} 会被直接替换而 #{value} 会使用 ? 预处理
select idselectSql resultTypecn.sycoder.domain.Employeeselectinclude refidbasicSelect/includefrom ${tableName} where id #{id}
/select
Employee selectSql(Param(tableName) String tableName,Param(id) Long id); 2.1特点可以做表名拼接更加方便灵活
2.2面试题说说 mybatis # 取参和 $ 取参的区别 ${参数名称} 会被直接替换而 #{value} 会使用 ? 预处理
2.3补充使用 Param 底层自动给我们转换成一个 map 来传参的
六、结果映射
1.ResultType 映射
1.1使用 resultType查询单个Map对象
select idselectMap resultTypemapselect * from employee where id #{id}
/select
Map selectMap(Long id); 1.2使用 resultType查询查询具体单个对象
select idselect resultTypecn.wjcoder.domain.Employeeselect * from employee where id #{id}
/select
Employee select(Param(id) Long id);
1.3查询集合对象
select idselectList resultTypecn.wjcoder.domain.Employeeselect * from employee where id #{id}
/select
ListEmployee selectList(Long id);
1.4查询单个值
select idselectCount resultTypejava.lang.Integerselect count(*) from employee
/select
Integer selectCount();
2.ResultMap 映射
2.1使用ResultType 问题
单独使用ResultType 会出现如下问题 2.2ResultMap
应用场景实体类属性和数据库列名不匹配的时候比如数据库采用经典命名法java 使用驼峰命名法的时候
resultMap idbasicMap typecn.wjcoder.domain.Employee!-- 设置数据库id 的对应字段--id propertyid columnid/idresult propertyempDetail columnemp_detail/resultresult propertyname columnname/result/resultMapselect idselectEmpById resultMapbasicMapselect * from employee where id #{id}/select 解决方式2 settingssetting namemapUnderscoreToCamelCase valuetrue//settings
id result
id propertyid columnpost_id/
result propertysubject columnpost_subject/
id result 属性
属性描述property映射到列结果的字段或属性。如果 JavaBean 有这个名字的属性property会先使用该属性。否则 MyBatis 将会寻找给定名称的字段field。无论是哪一种情形你都可以使用常见的点式分隔形式进行复杂属性导航。 比如你可以这样映射一些简单的东西“username”或者映射到一些复杂的东西上“address.street.number”。 stu.namecolumn数据库中的列名或者是列的别名。一般情况下这和传递给 resultSet.getString(columnName) 方法的参数一样。javaType一个 Java 类的全限定名或一个类型别名关于内置的类型别名可以参考上面的表格。 如果你映射到一个 JavaBeanMyBatis 通常可以推断类型。然而如果你映射到的是 HashMap那么你应该明确地指定 javaType 来保证行为与期望的相一致。
2.3多结果集处理前期准备
新建学生和班级表
create table class
(id bigint auto_incrementprimary key,name varchar(64) null
);create table student
(id bigint auto_incrementprimary key,name varchar(64) null,age int null,class_id bigint null,constraint student_class_id_fkforeign key (class_id) references class (id)
);insert into class values (null,软工1班),(null,计科2班);insert into student (id, name, age,class_id)
values (null,sy,18,1),(null,zs,19,1),(null,zz,20,1),(null,小明,22,2);
2.4一对多处理
一对多查询查询某班学生信息
collection 使用 collection 就可以获取到多个结果集对象
第一步新建 mapper 方法
public interface ClassMapper {MyClass getById(Long id);
}
第二步编写 xml
?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.ClassMapperresultMap idbasicMap typecn.wjcoder.domain.MyClassid propertyid columnid/idresult propertyname columnname/result
!-- 获取学生信息信息--collection propertystus ofTypecn.wjcoder.domain.Studentid propertyid columnsId/idresult propertyname columnsName/resultresult propertyage columnage/resultresult propertyclassId columnclass_id/result/collection/resultMapselect idgetById resultMapbasicMapselectc.*,s.id sId,s.name sName,s.age,s.class_idfromclass c left join student s on c.id s.class_idwhere c.id #{id}/select
/mapper 2.5多对一的处理
多对一查询查询每个学生所在班级信息
关联association如果我的类里面有其它对象的关联关系可以使用 association 来进行操作
属性描述property映射到列结果的字段或属性。如果用来匹配的 JavaBean存在给定名字的属性那么它将会被使用。否则 MyBatis 将会寻找给定名称的字段。无论是哪一种情形你都可以使用通常的点式分隔形式进行复杂属性导航。比如你可以这样映射一些简单的东西“username”或者映射到一些复杂的东西上“address.street.number”。javaType一个 Java 类的完全限定名或一个类型别名关于内置的类型别名可以参考上面的表格。 如果你映射到一个 JavaBeanMyBatis 通常可以推断类型。然而如果你映射到的是HashMap那么你应该明确地指定 javaType 来保证行为与期望的相一致。
传统操作使用级联操作
resultMap idbasicMap typecn.wjcoder.domain.Studentid propertyid columnid/idresult propertyname columnname/resultresult propertyage columnage/resultresult propertyclassId columnclass_id/resultresult propertycls.id columncId/resultresult propertycls.name columncName/result/resultMapselect idlistAllStus resultMapbasicMapselectstu.*,c.id cId,c.name cNamefromstudent stu left join class c on stu.class_id c.id/select
使用 association 操作
resultMap idAssociationMap typecn.wjcoder.domain.Studentid propertyid columnid/idresult propertyname columnname/resultresult propertyage columnage/resultresult propertyclassId columnclass_id/resultassociation propertycls javaTypecn.sycoder.domain.MyClassid propertyid columncId/idresult propertyname columncName/result/association/resultMapselect idlistAllStusByAssociation resultMapAssociationMapselectstu.*,c.id cId,c.name cNamefromstudent stu left join class c on stu.class_id c.id/select 3.嵌套 select 查询
3.1关联关系 assciation select
查询学生信息包含班级信息
resultMap idAssociationSelectMap typecn.wjcoder.domain.Studentid propertyid columnid/idresult propertyname columnname/resultresult propertyage columnage/resultresult propertyclassId columnclass_id/resultassociation propertycls columnclass_idselectcn.wjcoder.mapper.StudentMapper.getClassById/
/resultMap
select idlistAllStusByAssociationSelect resultMapAssociationSelectMapselect * from student
/selectselect idgetClassById resultTypecn.wjcoder.domain.MyClassselect * from class where id #{id}
/select 如果关联的是多个结果集使用 resultSet
属性描述column当使用多个结果集时该属性指定结果集中用于与 foreignColumn 匹配的列多个列名以逗号隔开以识别关系中的父类型与子类型。foreignColumn指定外键对应的列名指定的列将与父类型中 column 的给出的列进行匹配。resultSet指定用于加载复杂类型的结果集名字。
resultMap idblogResult typeBlogid propertyid columnid /result propertytitle columntitle/association propertyauthor javaTypeAuthor resultSetauthors columnauthor_id foreignColumnidid propertyid columnid/result propertyusername columnusername/result propertypassword columnpassword/result propertyemail columnemail/result propertybio columnbio//association
/resultMap
3.2 collection select
需求通过班级去查学生使用嵌套 select 查询
resultMap idcollectionSelect typecn.wjcoder.domain.MyClassid propertyid columnid/idresult propertyname columnname/result!-- 获取学生信息信息--collection propertystus ofTypecn.wjcoder.domain.StudentselectgetStudentByClassId columnid//resultMapselect idgetByClassId resultMapcollectionSelectselect * from class where id #{id}
/select
select idgetStudentByClassId resultTypecn.wjcoder.domain.Studentselect * from student where class_id #{id}
/select 3.3 关联查询的总结
有点 可以实现延迟加载前提是要配置 sql 写起来变得简单了 缺点发起了多条 sql,正常查询只发起一条sql
七、自动映射
自动映射将列名和结果集关联。
select idselectUsers resultMapuserResultMapselectuser_id as id,user_name as userName,hashed_passwordfrom some_tablewhere id #{id}
/selectresultMap iduserResultMap typeUserresult propertypassword columnhashed_password/
/resultMap 可以全局配置将经典的列名转换成驼峰的方式 映射等级分类 NONE - 禁用自动映射。仅对手动映射的属性进行映射。 PARTIAL - 对除在内部定义了嵌套结果映射也就是连接的属性以外的属性进行映射 FULL - 自动映射所有属性。
八、缓存
1.概述
Mybatis 缓存MyBatis 内置了一个强大的事务性查询缓存机制它可以非常方便地配置和定制。
2.会话缓存一级缓存
sqlSession 级别的也就是说使用同一个 sqlSession 查询同一 sql 时直接从缓存数据中取不需要操作数据库。 失效情况 同一个 sqlSession 条件不同 同一个 sqlSession 执行期间执行了增加、删除、修改操作 手动清缓存
3.二级缓存 使用之前必须在mapper.xml 文件中添加 cache/ 二级缓存是 sqlSessionFactory 级别的只要使用同一个 sqlSessionFactory 创建的 sqlSession 查询同一 sql 都可以使用缓存 使用前注意 先再 mapper.xml 中手动开启二级缓存 失效情况 必须给类实现序列化接口其实原理就是把对象信息写进一个序列化文件中通过对象流 两次查询之间添加了任何的增删改操作 insert、update 和 delete 语句会刷新缓存 二级缓存清除策略 LRU – Least Recently Used最近最少使用移除最长时间不被使用的对象。默认使用方式 FIFO – 先进先出按对象进入缓存的顺序来移除它们。 SOFT – 软引用基于垃圾回收器状态和软引用规则移除对象。 WEAK – 弱引用更积极地基于垃圾收集器状态和弱引用规则移除对象。
cacheevictionFIFOflushInterval60000size512readOnlytrue/ flushInterval:刷新间隔单位毫秒 size:引用数目默认是1024 readOnly:只读操作默认是 false true,会给调用的地方返回相同实例对象不能修改 false:可以修改返回实例的拷贝
4.缓存查询顺序 先查二级缓存范围比较大可能有其它会话缓存的信息 再查一级缓存 再查数据库 如果sqlSession关闭会把一级缓存信息缓存到二级缓存