Mybatis

简介

  • Mybatis是一个半自动的ORM框架
  • ORM(Object Relational Mapping)对象关系映射,将Java的一个对象与数据表中一行记录一一对应
  • ORM框架提供了实体类与数据表的映射关系,通过映射文件的配置,实现对象的持久化存储

    特点

  • 支持自定义SQL、存储过程

  • 对原有的JDBC进行了封装,几乎消除了所有JDBC代码,让开发者只需关注SQL本身
  • 支持XML和注解配置方式自定完成ORM操作,实现结果映射

    部署

  1. 创建MAVEN项目
  2. 添加pom文件中的依赖 ```xml <?xml version=”1.0” encoding=”UTF-8”?> <project xmlns=”http://maven.apache.org/POM/4.0.0

    1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    org.example Mybatis

    1.0-SNAPSHOT

    1. <maven.compiler.source>8</maven.compiler.source>
    2. <maven.compiler.target>8</maven.compiler.target>

    1. <!--Mybatis -->
    2. <dependency>
    3. <groupId>org.mybatis</groupId>
    4. <artifactId>mybatis</artifactId>
    5. <version>3.4.6</version>
    6. </dependency>
    7. <!-- mysql驱动 -->
    8. <dependency>
    9. <groupId>mysql</groupId>
    10. <artifactId>mysql-connector-java</artifactId>
    11. <version>5.1.47</version>
    12. </dependency>
    13. <!-- lombok -->
    14. <dependency>
    15. <groupId>org.projectlombok</groupId>
    16. <artifactId>lombok</artifactId>
    17. <version>1.18.12</version>
    18. <scope>provided</scope>
    19. </dependency>
    20. <!-- junit单元测试 -->
    21. <dependency>
    22. <groupId>junit</groupId>
    23. <artifactId>junit</artifactId>
    24. <version>4.12</version>
    25. <scope>test</scope>
    26. </dependency>
  1. </dependencies>

  1. 3. 添加Mybatis配置文件
  2. ```xml
  3. <?xml version="1.0" encoding="UTF-8" ?>
  4. <!DOCTYPE configuration
  5. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  6. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  7. <configuration>
  8. <environments default="development">
  9. <environment id="development">
  10. <transactionManager type="JDBC"/>
  11. <dataSource type="POOLED">
  12. <!--数据库连接配置-->
  13. <property name="driver" value="com.mysql.jdbc.Driver"/>
  14. <property name="url" value="jdbc:mysql://10.30.59.136/wy?serverTimezone=GMT&amp;characterEncoding=UTF-8"/>
  15. <property name="username" value="root"/>
  16. <property name="password" value="123456"/>
  17. </dataSource>
  18. </environment>
  19. </environments>
  20. <!--sql映射文件路径-->
  21. <mappers>
  22. <mapper resource="mappers/BookMapper"/>
  23. </mappers>
  24. </configuration>

Mybatis使用

  1. 创建数据表实体类 ```java package pojo;

public class Bookdatabase { public int getId() { return id; }

  1. public void setId(int id) {
  2. this.id = id;
  3. }
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public int getAge() {
  11. return age;
  12. }
  13. public void setAge(int age) {
  14. this.age = age;
  15. }
  16. @Override
  17. public String toString() {
  18. return "Bookdatabase{" +
  19. "id=" + id +
  20. ", name='" + name + '\'' +
  21. ", age=" + age +
  22. '}';
  23. }
  24. private int id;
  25. private String name;
  26. private int age;

}

  1. 2. 创建接口类
  2. ```java
  3. package dao;
  4. import pojo.Bookdatabase;
  5. import java.util.List;
  6. public interface Bookdatamap {
  7. public int insertBook(Bookdatabase book);
  8. }
  1. 创建sql映射文件
    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <!--设置名称空间-->
    6. <mapper namespace="dao.Bookdatamap">
    7. <!--定义SQL语句,id为绑定的借口类中的方法名-->
    8. <insert id="insertBook">
    9. insert into test(id,name,age) values(#{id},#{name},#{age})
    10. </insert>
    11. </mapper>