使用MyBatis的步骤

1.创建空的Java工程,安装MyBatis依赖

<?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>org.example</groupId>
    <artifactId>mybatis03</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

2.创建JavaBean类

在src/main/java下新建com.ttpfx.domain包,并创建User类:

package com.ttpfx.domain;

import java.util.Date;

public class User {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

3.创建Dao接口

在src/main/java新建com.ttpfx.dao包,并创建UserDao接口:

package com.ttpfx.dao;

import com.ttpfx.domain.User;

import java.util.List;

public interface UserDao {
    List<User> getAll();
}

4.创建XML映射文件

在src/main/resources下创建com/ttpfx/dao/UserDao.xml

注意事项:

  1. xml文件的路径和名称与com.ttpfx.dao.UserDao接口对应
  2. mapper标签的namespace属性为UserDao的全限定类名
  3. select标签的id与UserDao接口的方法对应
  4. 指定select标签的resultType为User类的全限定类名
<?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.ttpfx.dao.UserDao">
    <select id="getAll" resultType="com.ttpfx.domain.User">
        select id, username, birthday, sex, address from user;
    </select>
</mapper>

5.创建MyBatis主配置文件

在src/main/resources下创建SqlMapConfig.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="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/t_mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/ttpfx/dao/UserDao.xml"/>
    </mappers>
</configuration>

6.测试使用

在src/test/java新建com.ttpfx.test包,并创建MybatisTest类:

package com.ttpfx.test;

import com.ttpfx.dao.UserDao;
import com.ttpfx.domain.User;
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.InputStream;
import java.util.List;

public class MybatisTest {
    public static void main(String[] args) throws Exception {
        // 1.读取配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 2.创建SqlSession工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(in);
        // 3.创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 4.创建UserDao动态代理
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        // 5.使用userDao
        List<User> list = userDao.getAll();
        list.forEach(System.out::println);
        // 6.销毁资源
        sqlSession.close();
        in.close();
    }
}
上一篇:Android反编译教程


下一篇:JAVA之IO技术相关 如何设置软件的使用次数