美文网首页
Mybatis 知识点

Mybatis 知识点

作者: tingshuo123 | 来源:发表于2018-07-30 11:21 被阅读30次

    Mybatis 简介

    Mybatis 是一款开源免费的半自动ORM框架,需要手动定制sql,存储过程,高级映射。用它我们几乎可以避免所有的jdbc代码,只需要简单的配置就可以将Java的pojo类跟数据库记录进行映射,实现了sql语句与代码分离。

    使用 Maven 构建项目

    只需要将下面的 dependency 代码置于 pom.xml 文件中:

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>x.x.x</version> <-- 版本号 -->
    </dependency>
    

    使用 XML 创建 SqlSessionFactory

    每个

    // xml 配置文件路径
    String resource = "org/mybatis/example/mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    

    基本 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="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
          </dataSource>
        </environment>
      </environments>
      <mappers>
        <mapper resource="org/mybatis/example/BlogMapper.xml"/>
      </mappers>
    </configuration>
    

    两种通过SqlSession执行SQL语句的方式

    Blog blog = (Blog) session.selectOne("要执行Sql的ID", 101);
    
    

    使用下面这中方式,需要Mapper xml文件中的命名空间,跟接口类全路径一致,Sql语句id属性值,跟方法名一致,这种方式好处是,不需再自己写实现类。推荐使用下面这种方式:

      BlogMapper mapper = session.getMapper(BlogMapper.class);
      Blog blog = mapper.selectBlog(101);
    

    简单的增删查改

    parameterType 指定传入参数类型,只可以指定单个,简单类型可以直接写,如果是引用类型查,需要写全类路径,可以通过设置别名来简化。
    还有一个问题就是只能传一个参数,如果要传入多个参数删除parameterType属性,再接口方法上添加注解实现:
    public User selectUserByNameAndAge(@param("name")String name,@param("age")int age);

    另一个方式,也需要删除parameterType属性,通过下标方式指定参数。
    接口方法:

    public User selectUserByNameAndAge(String name,int age);
    

    mapper.xml

    <select id="selectUserByNameAndAge" resultType="user">
        select * from user where name = #{0} and age = #{1}
    </select>
    

    resultType 指定返回值类型,增、删、改可以省略。
    查询:

      <select id="selectBlog" parameterType="int" parameter resultType="Blog">
        select * from Blog where id = #{id}
      </select>
    

    增、改、删,将<select> 改成对应的 <inster><update><delect> 即可,返回的都是 int 型的 影响行数。可以省略

    作用域和生命周期

    SqlSessionFactoryBuilder 唯一作用是读取xml文件构建SqlSession,一旦创建了 SqlSessionFactory 对象,就不再需要它了。最好设置为局部变量

    SqlSessionFactory 每次对数据库的操作都需要用它产生 Sqlsession 实例 来执行 SQL 语句,所以 SqlSessionFactory 应该存在于整个应用运行期,最好为应用作用域,并且是单例
    SqlSession 的实例不是线程安全的,所以每个 线程都应该有一个自己的 SqlSession 实例,不能被其他线程共享,最好设置为局部变量,并确保每次使用都能被关闭。

    使用 SqlSession 的标准格式,确保无论如果 SqlSession 都能关闭:

    SqlSession session = sqlSessionFactory.openSession();
    try {
      // do work
    } finally {
      session.close();
    }
    

    设置别名

    再 mabatis配置文件中添加:

    <typeAliases>
    
      <typeAlias alias="Author" type="domain.blog.Author"/>
      <typeAlias alias="Blog" type="domain.blog.Blog"/>
    
    </typeAliases>
    

    也可以使用 name 字段 指定包,为包下所有类设置别名,别名默认值类名首字母小写,可以通过 类名头上加@Alias() 改变。

    <typeAliases>
      <package name="com.project.bean"/>
    </typeAliases>
    

    相关文章

      网友评论

          本文标题:Mybatis 知识点

          本文链接:https://www.haomeiwen.com/subject/mrmxvftx.html