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

posted @   emanlee  阅读(46)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2020-12-22 MySQL字符串 首字母转为大写
2008-12-22 javascript 事件调用顺序
点击右上角即可分享
微信分享提示