Mybatis环境搭建:自学随笔

环境:IDEA  MySQL

1.建立maven工程,导入坐标:编辑pom.xml,建立mybatis约束。

https://mybatis.org/mybatis-3/zh/getting-started.html中查看mybatis的最新版本以及约束的开头;

<!-- x.x.x为mybaits版本号 -->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>x.x.x</version>
</dependency>

在mysql中使用

mysql> show variables like "%version%";

查看自己的MySQL版本。

<?xml version="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:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lymMybits</groupId>
    <artifactId>Mybits_first</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
            <!-- 定位MyBatis -->
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
            <!-- 定位MySQL -->
        </dependency>
    </dependencies>
</project>

2.创建实体类和dao接口。

实体类User:

package com.Mybaits_test.domain;

import java.io.Serializable;

/**
 * @author  北沧
 *User实体类
 */
public class User implements Serializable {
    private Integer ID;
    private String name;
    private  String password;
    private  String sex;
    private String work;
    //与表中的列名相同
    public Integer getID() {
        return ID;
    }

    public void setID(Integer ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getWork() {
        return work;
    }

    public void setWork(String work) {
        this.work = work;
    }

    @Override
    public String toString() {
        return "User{" +
                "ID=" + ID +
                ", name=‘" + name + ‘\‘‘ +
                ", password=‘" + password + ‘\‘‘ +
                ", sex=‘" + sex + ‘\‘‘ +
                ", work=‘" + work + ‘\‘‘ +
                ‘}‘;
    }
}

DAO接口:

package com.Mybaits_test.Dao;

import com.Mybaits_test.domain.User;

import java.util.List;

/**
 * @author 北沧
 * dao持久层接口
 */
public interface IF_UserDao {
    List<User> findAll();
} 

3.创建Mybatis主配置文件:SQLMapConifg.xml,并创建映射配置文件IF_UesDao.xml。

要注意Conifg和Mapper的约束:

Conifg的约束:

<?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">

Mapper的约束:

<?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">

主配置文件:在resource下建立SQLMapConifg.xml,建立Conifg的约束。

<?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="mysql">
        <!-- mysql环境配置 -->
        <environment id="mysql">
            <!-- 配置事物类型 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置连接池 -->
            <dataSource type="POOLED">
                <!-- 配置连接数据库的连接信息 -->
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test.jdbc?serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 指定映射配置文件的位置,映射配置文件是每个DAO类独立的配置文件 -->
    <mappers>
        <mapper resource="com/Mybaits_test/dao/IF_UesDao.xml"/>
    </mappers>
</configuration>

在resource下创建创建com/Mybaits_test/dao文件夹,并映射配置文件IF_UesDao.xml,建立Mapper的约束。

<?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="com.Mybaits_test.Dao.IF_UserDao">
    <select id="findAll">
        select * from  user
    </select>
</mapper>
相关文章
相关标签/搜索