mybatis的学习线路图

mybatis中的重点就是:

1 如何保证sqlSession 中的线程安全

2 mapper 代理的底层实现

3 mybatis 的高级查询(一对一,一对多,多对多)

4 延迟加载

5 一二级缓存与缓存配置

6 mybatis 分页

0 jdbc 和mybatis 的对比,jdbc 的劣势

1 宏观上把握MyBatis框架

2 原始的到dao开发和弊端(sqlSession的线程不安全(用数据库连接池来解决),硬编码用xml 配置来解决)

3 为了摒弃dao 开发的弊端所以采用mapper 代理的方式进行开发

通过这种方式来相当于对mapper 接口的实现,通过代理的方式创建mapper对象,实现mapper 的功能

SqlSession sqlSession = sqlSessionFactory.openSession();
//创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findUserById(1);

输出映射(返回的参数的类型是pojo,基本数据类型,hashmap):最后总结一下:使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。
并且在查询出来什么列就在resultmap 中配置什么列,下面的sql 查询出两个参数,那么上面就映射两个参数

<resultMap type="user" id="userResultMap">
    <id column="id_" property="id"/>
    <result column="username_" property="username"/>
</resultMap>
<select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
    SELECT id id_,username username_ from user where id = #{id}
</select>

resultType="mybatis.po.User"(一)根据用户id 查询用户信息

<select id="findUserById" parameterType="int" resultType="mybatis.po.User">
        select * from user where id = #{id}
</select>

resultType的值是一个实体类,返回值是实体的话,保证实体里面的属性和数据库的属性值是相同的

resultType="mybatis.po.User"(二) 返回的是list

模糊查询,返回的是list,但是resultType 还是实体类

<!-- 根据用户名称模糊查询用户信息,可能返回多条 -->
    <select id="findUserByName" parameterType="java.lang.String" resultType="mybatis.po.User">
        select * from user where username like '%${value}%'
    </select>
SqlSession sqlSession = getSession();
//selectList表示查询出一个列表(多条记录)进行映射
List<User> list = sqlSession.selectList("test.findUserByName", "张三");
System.out.println(list);
//释放资源,最好放在finally中,这里只是测试程序,就不弄了
sqlSession.close();

动态sql

Excutor(执行器):是一个接口,有两个实现(基本执行器和缓存执行器),通过sqlSession 内部通过该执行器操作数据

mappedStatement(底层封装对象),作用:对操作数据库存储封装,包括sql 语句,输入参数,输出结果类型

2 mybaits 中打印sql 日志配置

3 走进MyBatis的世界

 1)mybatis 配置需要的jar 包

2)通过SqlMapConfig.xml 来配置jdbc事务管理,数据库连接池

3)

猜你喜欢

转载自blog.csdn.net/qq_20610631/article/details/82734000