package org.apache.ibatis.executor;
public interface Executor 负责动态 SQL 的生成和查询缓存的维护
package org.apache.ibatis.executor;
public class SimpleExecutor extends BaseExecutor 每执行一次 update 或 select,就开启一个 Statement 对象,用完立刻关闭 Statement 对象
package org.apache.ibatis.executor;
public class BatchExecutor extends BaseExecutor update 批处理执行器,使用 Statement 的 addBatch 批处理执行(缓存了多个 Statement,等待 executeBatch 逐一执行)
package org.apache.ibatis.executor;
public class ReuseExecutor extends BaseExecutor 执行 update 或 select,重复使用 Statement 对象。
以 sql 作为 key 查找 Statement 对象,存在就使用,不存在就创建
执行完后,不关闭 Statement 对象,而是放置于 Map<String, Statement>内,供下一次使用。
mybatis-spring-2.0.3 源码解析—————————————————————————————————————————————————————————————————————————————————————————
package org.mybatis.spring.annotation;
@MapperScan(value="com.dongfeng.mybatis.mapper") 不需要对每个 Mapper 都添加@Mapper 注解,可用于 springboot 引导类
mybatis-3.4.5 源码解析—————————————————————————————————————————————————————————————————————————————————————————
package org.apache.ibatis.annotations;
test 使用——————————————————————————————————————————————————————————————————————————————————————————————————
InputStream is = Resources.getResourceAsStream(mybatis-config.xml"); // 加载核心资源配置文件
sqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); // .获取 SqlSession 工厂对象
SqlSession sqlSession = sqlSessionFactory.openSession(true) ; // 通过 SqlSession 工厂对象获取 SqlSession 对象
List<Student> list = sqlSession.selectList( s: "StudentMapper.selectAll"); // 执行映射配置文件中的 sql 语句,并接收结果
StudentDao studentDao=sqlSession.getMapper(StudetnDao.class)
sqlSession.close()
is.close()
mybatis 使用——————————————————————————————————————————————————————————————————————————————————————————————————
resource/mapper/StudentMapper.xml >> 首先创建一个 dao 映射配置,
<?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.saidake.mybatis.mapper.StudentMapper"> <!-- 映射到 mapper 方法上-->
//StudentMapper
public interface StudentMapper {
int[] getAllStudentIds();
}
//StudentMapper.xml
<?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.morris.dao.StudentMapper">
<resultMap id="orderInfo" type="com.morris.po.OrderInfo">
<id property="id" column="order_id"/>
<result property="orderNumber" column="order_number"/>
<association property="orderAddress" javaType="com.morris.po.OrderAddress">
<result property="region" column="region"/>
<result property="district" column="district"/>
<result property="city" column="city"/>
<result property="street" column="street"/>
</association>
</resultMap>
<select id="getAllStudentIds" resultType="int">
select id
from student;
</select>
</mapper>
//验证
public static void main(String [] args) {
SqlSession session = getSqlSession();
StudentMapper sm = session.getMapper(StudentMapper.class);
int[] ids = sm.getAllStudentIds();
for (int i : ids) {
System.out.println(i);
}
}
</mapper>
<select id="queryList" resultType="map">
select table_name tableName, engine engine, table_comment tableComment, create_time createTime from //必须重命名驼峰,才能映射到 entity 对象中(下划线映射到 map 的结果: TABLE_COMMENT=用户共享人表, TABLE_NAME=t_user_share)
information_schema.tables
where table_schema = (select database())
<if test="p.tableName != null and p.tableName.trim() != ''">
and table_name like concat('%', #{p.tableName}, '%') // 对应 mapper 中的参数,默认直接取参数名 @Param("p") Map<String, Object> map (多个参数时,要使用@Param 指定参数名)
</if>
order by create_time desc
</select>