第⼗六章 新版MyBatis3.X玩转常⻅配置

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>

 本身就内置很多别名,⽐如IntegerStringListMap

3集 ⾼性能sqlMyBatis3.XSql⽚段使⽤ 

简介:讲解Mybatissql⽚段的使⽤  

  • 你是否常⽤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();
        }

    }

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值