zoukankan      html  css  js  c++  java
  • MyBatis

    2.1.1 Installation

    If you are using Maven just add the following dependency to your pom.xml:

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.1</version>
    </dependency>

    2.1.2 Building SqlSessionFactory from XML

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

    The full details of the XML configuration file can be found later in this document, but here is a simple example:

    org/mybatis/example/mybatis-config.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>

    2.1.4 Acquiring a SqlSession from SqlSessionFactory

    SqlSession session = sqlSessionFactory.openSession();
    try {
      Blog blog = session.selectOne(
        "org.mybatis.example.BlogMapper.selectBlog", 101);
    } finally {
      session.close();
    }

    2.1.5 Exploring Mapped SQL Statements

    <?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="org.mybatis.example.BlogMapper">
      <select id="selectBlog" resultType="Blog">
        select * from Blog where id = #{id}
      </select>
    </mapper>
    Blog blog = session.selectOne(
      "org.mybatis.example.BlogMapper.selectBlog", 101);

    There's one more trick to Mapper classes like BlogMapper. Their mapped statements don't need to be
    mapped with XML at all. Instead they can use Java Annotations. For example, the XML above could
    be eliminated and replaced with:

    package org.mybatis.example;
    public interface BlogMapper {
      @Select("SELECT * FROM blog WHERE id = #{id}")
      Blog selectBlog(int id);
    }

    3.1 Configuration

    配置文件的内容

    • configuration
    • properties
    • settings
    • typeAliases
    • typeHandlers
    • objectFactory
    • plugins
    • environments
      • environment
        • transactionManager
        • dataSource
    • databaseIdProvider
    • mappers

    接口+配置文件方式,实现MyBatis的使用

     Mapper开发规则
    1、 在 mapper.xml 中将 namespace 设置为 mapper.Java 的全限定名
    2、 将 mapper.java 接口的方法名和 mapper.xml 中 statement 的id保持一致。
    3、 将 mapper.java 接口的方法输入参数类型和 mapper.xml 中 statement 的 parameterType 保持一致
    4、 将 mapper.java 接口的方法输出 结果类型和 mapper.xml 中 statement 的 resultType 保持一致

    REF

    mybatis-3.5.1.pdf

    MyBatis使用接口和不使用接口实现查询

    https://blog.csdn.net/qq_41450893/article/details/79620635

    MyBatis中settings属性配置详解

    http://c.biancheng.net/view/4324.html

    MyBatis 接口的使用

    https://www.cnblogs.com/chuijingjing/p/9862105.html

  • 相关阅读:
    linux扩展分区
    linux开机出现initramfs无法进入系统
    openCV编译安装-MSCV-Windows10-Qt
    Qt一键部署配置(Qt程序打包)
    Part8 升序排序 和降序排序
    Part7-.简单查询1
    Part6-向表中插入数据
    Part5-修改表(添加字段、删除字段、查看删除是否成功)
    Part4-删除表
    Part3-复制表
  • 原文地址:https://www.cnblogs.com/emanlee/p/15719734.html
Copyright © 2011-2022 走看看