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

原文地址:https://www.cnblogs.com/emanlee/p/15719734.html