当前位置 : 主页 > 编程语言 > java >

MyBatis快速入门

来源:互联网 收集:自由互联 发布时间:2022-08-15
一、MyBatis介绍 MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的

一、MyBatis介绍

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

MyBatis快速入门_Maven

二、MyBatis安装

2.1 Maven项目

如果使用 Maven 来构建项目,则需将下面的 dependency 代码置于 pom.xml 文件中

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

2.2 JAR包安装

要使用 MyBatis, 只需将 mybatis-x.x.x.jar 文件置于lib中,并导入项目即可。

三、Mybatis功能架构

Mybatis功能架构分为三层:

  • API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库。接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理。
  • 数据处理层:负责具体的SQL查找、SQL解析、SQL执行和执行结果映射处理等。它主要的目的是根据调用的请求完成一次数据库操作。
  • 基础支撑层:负责最基础的功能支撑,包括连接管理、事务管理、配置加载和缓存处理,这些都是共用的东西,将他们抽取出来作为最基础的组件。为上层的数据处理层提供最基础的支撑。
  • 四、快速Demo

  • 在数据库中创建表,添加数据
  • 创建模块,导入坐标
  • pom.xml:

    <?xml versinotallow="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocatinotallow="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>MyBatatisDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
    <maven.compiler.source>13</maven.compiler.source>
    <maven.compiler.target>13</maven.compiler.target>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.5</version>
    </dependency>

    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.46</version>
    </dependency>

    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.2</version>
    <scope>test</scope>
    </dependency>
    </dependencies>
    </project>

  • 编写MyBatis核心配置文件 ---> 替换连接信息,解决硬编码问题
  • resource/mybatis-config.xml

    <?xml versinotallow="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.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://XXXX:3306/petsystem?useSSL=false"/>
    <property name="username" value="root"/>
    <property name="password" value="XXXX"/>
    </dataSource>
    </environment>
    </environments>
    <mappers>
    <package name="com.laccoliths.mapper"/>
    </mappers>
    </configuration>

  • 编写SQL映射文件 ---> 统一管理sql语句,解决硬编码问题
  • resource/com.laccoliths.mapper/PetSystemMapper.xml

    <?xml versinotallow="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="com.laccoliths.mapper.PetSystemMapper">
    <select id="selectPetAll" resultType="com.laccoliths.pojo.Pet">
    select * from pet;
    </select>
    <select id="selectPetById" resultType="com.laccoliths.pojo.Pet">
    select * from pet where id = #{id};
    </select>
    </mapper>

  • 编码
  • 定义POJO类
  • main/java/com.laccoliths.pojo/Pet.java

    package com.laccoliths.pojo;

    /**
    * @author Laccoliths
    */
    public class Pet {
    private String id;
    private String name;
    private String gender;
    private String age;
    private String varieties;
    private String symptom;
    private String isCure;
    private String clientName;
    private String doctorName;

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getGender() {
    return gender;
    }

    public void setGender(String gender) {
    this.gender = gender;
    }

    public String getAge() {
    return age;
    }

    public void setAge(String age) {
    this.age = age;
    }

    public String getVarieties() {
    return varieties;
    }

    public void setVarieties(String varieties) {
    this.varieties = varieties;
    }

    public String getSymptom() {
    return symptom;
    }

    public void setSymptom(String symptom) {
    this.symptom = symptom;
    }

    public String getIsCure() {
    return isCure;
    }

    public void setIsCure(String isCure) {
    this.isCure = isCure;
    }

    public String getClientName() {
    return clientName;
    }

    public void setClientName(String clientName) {
    this.clientName = clientName;
    }

    public String getDoctorName() {
    return doctorName;
    }

    public void setDoctorName(String doctorName) {
    this.doctorName = doctorName;
    }

    public Pet(String id, String name, String gender, String age, String varieties, String symptom, String isCure, String clientName, String doctorName) {
    this.id = id;
    this.name = name;
    this.gender = gender;
    this.age = age;
    this.varieties = varieties;
    this.symptom = symptom;
    this.isCure = isCure;
    this.clientName = clientName;
    this.doctorName = doctorName;
    }

    @Override
    public String toString() {
    return "Pet{" +
    "id='" + id + '\'' +
    ", name='" + name + '\'' +
    ", gender='" + gender + '\'' +
    ", age='" + age + '\'' +
    ", varieties='" + varieties + '\'' +
    ", symptom='" + symptom + '\'' +
    ", isCure='" + isCure + '\'' +
    ", clientName='" + clientName + '\'' +
    ", doctorName='" + doctorName + '\'' +
    '}';
    }
    }

    b. 加载核心配置文件,获取SqlSessionFactory对象

    main/java/com.laccoliths/MyBattisDemo.java

    // 新建Demo类
    String resources = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resources);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    c. 获取SqlSession对象,执行SQL语句

    // 获取SqlSession对象,用它来执行Sql语句
    SqlSession sqlSession = sqlSessionFactory.openSession();
    List<Object> pets = sqlSession.selectList("PetMapper.selectPetAll");

    for (Object pet : pets) {
    System.out.println(pet.toString());
    }

    d. 释放资源

    sqlSession.close();

    完整Demo源码:

    package com.laccoliths;

    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;

    /**
    * @author Laccoliths
    */
    public class MyBatisDemo {
    public static void main(String[] args) throws IOException {
    // 加载Mybatis核心配置文件,获取SqlSessionFactory
    String resources = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resources);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    // 获取SqlSession对象,用它来执行Sql语句
    SqlSession sqlSession = sqlSessionFactory.openSession();
    List<Object> pets = sqlSession.selectList("PetMapper.selectPetAll");

    for (Object pet : pets) {
    System.out.println(pet.toString());
    }

    sqlSession.close();
    }
    }

    【文章出处:香港多ip站群服务器 http://www.558idc.com/hkzq.html提供,感恩】
    网友评论