Mybatis中mybatis-config.xml常见配置属性

356 阅读1分钟

核心配置文件(dom节点顺序要求,不然报错)

  • 一些常用的配置信息
configuration(配置)
 properties(属性)
 settings(设置)
 typeAliases(类型别名)
 typeHandlers(类型处理器)
 objectFactory(对象⼯⼚)
 plugins(插件,少⽤)
 environments(环境配置,不配多环境,基本在Spring⾥⾯配置)
 environment(环境变量)
 transactionManager(事务管理器)
 dataSource(数据源)
 databaseIdProvider(数据库⼚商标识)
 mappers(映射器)

官⽅⽂档:mybatis.org/mybatis-3/z…

typeAliases别名的使用

typeAliases(类型别名)

  • 使用了他,在后续的配置中就可以不输入类的全限定类名
<typeAliases>
	<!--将Video的全限定类名变为Video引用-->
 <!--<typeAlias type="cn.junko.domain.Video"
alias="Video"/>-->
<!--扫描包下,自动引用类名为别名-->
 <package name="cn.junko.domain"/>
</typeAliases>

这样,在mapper的配置中,就可以简化一些操作

<!--<select id="selectById" parameterType="java.lang.Integer"
resultType="cn.junko.domain.Video">-->
 <select id="selectById" parameterType="java.lang.Integer"
resultType="Video">
 select * from video where id = #{video_id,jdbcType=INTEGER}
 </select>

高性能sql之Mybatis的sql片段使用

在之前,我们常用select * 去查询数据库的内容,在一些小项目是没有太大问题的,但是在一些大型项目(例如电商),高并发项目中,这种操作是十分消耗性能和内存的。

什么是sql片段

  • 根据业务需要,⾃定制要查询的字段,并可以复⽤
    • 将要查询的字段,在sql标签中编写好
    • 然后在查询语句中引用该标签<include refid="base_video_field"/>
<sql id="base_video_field">
 id,title,summary,cover_img
 </sql>

 <select id="selectById" parameterType="java.lang.Integer"
resultType="Video">
 select <include refid="base_video_field"/> from video where
id = # {video_id,jdbcType=INTEGER}
 </select>
 <select id="selectListByXML" resultType="Video">
 select <include refid="base_video_field"/> from video
 </select>