MyBatis设计模式之单例模式

本文深入探讨了MyBatis框架中ErrorContext与Logger的单例模式实现。ErrorContext用于记录线程级别的错误信息,而Logger则为日志打印提供支持。了解这些核心组件的实现有助于更好地掌握MyBatis的工作原理。

mybatis 源码中单例模式如下:

1 获取ErrorContext 实例。

ErrorContext是用在每个线程范围内的单例,用于记录该线程的执行环境错误信息。ErrorContext的单例实现代码:在这里插入图片描述
​​

2 获取org.slf4j.Logger 实例对象。

LogFactory没有实现获取自身的方式,只是当成一个提供日志打印的工具。
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dao.BlogDao"> <resultMap type="Blog" id="blogResult"> <id column="blog_id" property="id" /> <result column="blog_title" property="subject"/> <result column="content" property="content"/> <!-- 映射关联的对象 --> <association property="author" javaType="Author"> <id column="id" property="blog_author_id"/> <result column="userName" property="userName"/> <result column="password" property="password"/> </association> <collection property="comments" ofType="Comment"> <id property="id" column="post_id"/> <result property="subject" column="post_subject"/> <result property="content" column="content"/> </collection> </resultMap> <sql id="where"> <where>userName =#{userName}</where> </sql> <select id="countAll" resultType="int"> select count(*) c from blog; </select> <select id="countSome" resultType="int" parameterType="String"> select count(*) c from blog <include refid="where"/>; </select> <select id="selectAll" parameterType="int" resultMap="blogResult"> select B.id as blog_id, B.subject as blog_title, B.author as blog_author_id, P.id as post_id, P.subject as post_subject, P.content as content from Blog B left outer join Comment P on B.id = P.blogid </select> <insert id="insert" parameterType="com.beans.Blog"> insert into blog(subject,content,author) values(#{subject},#{content},#{author}) </insert> <update id="update" parameterType="com.beans.User"> update blog set subject=#{subject},content=#{content},author=#{author} where userName=#{userName} </update> <delete id="delete" parameterType="int"> delete from blog where subject=#{subject} </delete> </mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值