MyBatis学习笔记(一)

MyBatis(一)

MyBatis是一个持久层框架,它对JDBC的操作数据库的过程进行了封装,使开发者只需要关注SQL本身,而不需要花费精力去处理例如注册驱动、创建connection、创建statement、手动设置参数、结果集检索等JDBC繁杂的过程代码。

  • MyBatis配置
    1、 mybatis配置SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了mybatis的运行环境等信息。mapper.xml文件即sql映射文件,文件中配置了操作数据库的sql语句。此文件需要在SqlMapConfig.xml中加载。
    2、 通过mybatis环境等配置信息构造SqlSessionFactory即会话工厂
    3、 由会话工厂创建sqlSession即会话,操作数据库需要通过sqlSession进行。
    4、 mybatis底层自定义了Executor执行器接口操作数据库,Executor接口有两个实现,一个是基本执行器、一个是缓存执行器。
    5、 Mapped Statement也是mybatis一个底层封装对象,它包装了mybatis配置信息及sql映射信息等。mapper.xml文件中一个sql对应一个Mapped Statement对象,sql的id即是Mapped statement的id。
    6、 Mapped Statement对sql执行输入参数进行定义,包括HashMap、基本类型、pojo,Executor通过Mapped Statement在执行sql前将输入的java对象映射至sql中,输入参数映射就是jdbc编程中对preparedStatement设置参数。
    7、 Mapped Statement对sql执行输出结果进行定义,包括HashMap、基本类型、pojo,Executor通过Mapped Statement在执行sql后将输出结果映射至java对象中,输出结果映射过程相当于jdbc编程中对结果的解析处理过程。

SQLMapConfig.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="sqlmap/user.xml"/>
  </mappers>
</configuration>

标签必须按顺序编写

mapper.xml配置文件(sql语句)

<?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="user">
  <select id="selectBlog" parameterType="Integer" resultType="com.gh.mybatis.pojo.User">
    select * from user where id = #{v}
  </select>
</mapper>
 <!--id为sql语句的id,parameterType为参数类型,resultType为结果类型,#{}为占位符-->

#{ } select * from user where id =? 占位符

//模糊查询
    ${ } select * from user where username like 字符串拼接:
    select * from user where username like '%${value}%'	//只能用value

目标方法:

//加载核心配置文件
		String resource = "sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		
		//创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		//创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		
		//执行sql语句
		User user = sqlSession.selectOne("user.selectBlog", 10);

注意:新增数据时,最后必须提交
sqlSession.commit()方法。新增数据时,在insert标签里使用selectKey标签可以返回新增数据的主键。

  • Mybatis与hibernate的区别
    -MyBatis需要程序员自己别写sql语句,可严格控制sql执行性能,灵活度高,但无法做到数据库的无关性
    -Hibernate 对象/关系映射能力强,数据无关性好

  • Mapper动态代理方式
    开发规范:
    Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。
    1、Mapper.xml文件中的namespace与mapper接口的类路径相同。
    2、Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
    3、Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同。
    4、Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同。
    接口:

public interface UserMapper {
	
	/**
	 * 四个原则
	 * 接口方法名 == User.xml中id名
	 * 返回值类型 与 Mapper.xml文件中返回值类型要一致
	 * 方法的入参类型与Mapper.xml中入参类型要一致
	 * 命名空间绑定此接口
	 * @return
	 */
	public User selectBlog(Integer id);
}

目标方法:

//SqlSession帮助生成一个实现类(给接口)
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		User user = userMapper.selectBlog(10);
		System.out.println(user);

**不需要实现类
小结:
1、selectOne和selectList:动态代理对象调用sqlSession.selectOne()和sqlSession.selectList()是根据mapper接口方法的返回值决定的,如果返回list则调用selectList方法,如果返回单个对象则调用selectOne方法。
2、namespace
mybatis官方推荐使用mapper代理方法开发mapper接口,程序员不用编写mapper接口实现类,使用mapper代理方法时,输入参数可以使用pojo包装对象或map对象,包装dao的通用性。

  • mappers 映射器
    Mapper配置的几种方法
    1、<mapper resource=""/>
    使用相对于类路径的资源
    如:<mapper resource="sqlmap/User.xml"/>
    2、<mapper class=""/>
    使用mapper接口类路径
    如:<mapper class="com.xxx.UserMapper"/>
    注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。
    3、<package name=""/>
    注册指定包下的所有mapper接口
    如:<package name="com.gh.mybatis.mapper"/>
    注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值