第1集 MyBatis3.X 配置⽂件mybatis-confifig.xml常⻅属性
简介:讲解
Mybatis
的
mybatis-confifig.xml
常⻅配置
核⼼配置⽂件(dom节点顺序要求,不然报错)
记住常⽤的,不常⽤的简单介绍,
configuration (配置)properties(属性)settings(设置)typeAliases(类型别名)typeHandlers(类型处理器)objectFactory(对象⼯⼚)plugins(插件,少⽤)environments(环境配置,不配多环境,基本在 Spring ⾥⾯配置)environment(环境变量)transactionManager(事务管理器)dataSource(数据源)databaseIdProvider(数据库⼚商标识)mappers (映射器)
官⽅⽂档:https://mybatis.org/mybatis-3/zh/confifiguration.html#
第2集 MyBatis3.X 查询typeAlias别名的使⽤
简介:讲解Mybatis的查询类别名typeAlias的使⽤
typeAlias
类型别名,给类取个别名,可以不⽤输⼊类的全限定名
<!--<select id="selectById" parameterType="java.lang.Integer"
resultType="net.xdclass.online_class.domain.Video">-->
<select id="selectById" parameterType="java.lang.Integer"
resultType="Video">
select * from video where id = #{video_id,jdbcType=INTEGER}
</select>
如果有很多类,是否需要⼀个个配置?
不⽤⼀个个配置,使⽤包扫描即可
<typeAliases>
<!--<typeAlias type="net.xdclass.online_class.domain.Video"
alias="Video"/>-->
<package name="net.xdclass.online_class.domain"/>
</typeAliases>
本身就内置很多别名,⽐如Integer、String、List、Map 等
第3集 ⾼性能sql之MyBatis3.X的Sql⽚段使⽤
简介:讲解Mybatis的sql⽚段的使⽤
- 你是否常⽤select * 去查询数据库
-
⼩项⽬没问题,⾼并发项⽬不推荐这样使⽤,查询性能低,应该选择需要的字段
-
- 什么是sql⽚段
-
根据业务需要,⾃定制要查询的字段,并可以复⽤
-
Mapper.xml文件实现:
本集核心代码
<!--sql片段的使用-->
<sql id="base_video_field">
id,title,summary,price,point
</sql>
<!--引用SQL片段 <include refid="base_video_field">-->
<!--查询根据id来查询视频-->
<select id="queryVideoById" resultType="Video" parameterType="Integer">
select <include refid="base_video_field" /> from video
where id = #{id,jdbcType =INTEGER }
</select>
mapper接口:
Video queryVideoById(int id);
测试类:
@Test
public void queryVideoByIdTest(){
//使用Resources读取配置文件
InputStream is = null;
try {
//读取resources目录下的文件
is = Resources.getResourceAsStream("MybatisConfigure.xml");
} catch (IOException e) {
e.printStackTrace();
}
if (is != null) {
//构建Session工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
VideoMapper mapper = sqlSession.getMapper(VideoMapper.class);
Video video = mapper.queryVideoById(34);
System.out.println(video.toString());
sqlSession.close();
}
}
运行结果: