Mybatis two configurations of use

1. First create a maven project

Groupid:cn.poison(自定义域名)
ArtifactId:项目名称
Packing:jar(打包类型)

2. introducing dependencies in pom.xml file

 <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.1.6</version>
        </dependency>
    </dependencies>

3. Write Data Sheet entity class

package cn.poison.pojo;

import java.util.Date;

/**
 * \*用户表实体类
 * \* Created with IntelliJ IDEA.
 * \* Author: Poison
 * \* Date: 2020/1/7
 * \* CreateTime: 16:40
 * \
 */
public class User {
    private Integer id;
    private String username;
    private Date birthday;
    private Character 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 Character getSex() {
        return sex;
    }

    public void setSex(Character 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 + '\'' +
                '}';
    }
}

For example to check all

First, the XML-based configuration

1. Basic configuration database connection information 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">
<!-- mybatis的主配置文件  -->
<configuration>
    <!--  配置环境   -->
    <environments default="mysql">
        <!--  配置mysql的环境 -->
        <environment id="mysql">
            <!-- 配置事务的类型  -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源(连接池)   -->
            <dataSource type="POOLED">
                <!--  mysql数据库注册驱动   -->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <!--  mysql数据库连接地址   -->
                <property name="url" value="jdbc:mysql://localhost:3306/你的数据库名?characterEncoding=utf-8"/>
                <!--  mysql数据库登录用户名  -->
                <property name="username" value="root"/>
                <!--  mysql数据库登录密码 -->
                <property name="password" value="****"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 指定映射配置mapper文件的位置,一般放在resource文件夹下 -->
    <mappers>
        <mapper resource="mybatis/mapper/UserMapper.xml"></mapper>
    </mappers>

</configuration>

Note: xml-based configuration, here you need to specify a file path resource directory mapper.xml

My project is structured as follows

2. Write mapper interfaces (interfaces was also called Dao, in fact, the meaning is the same, all the database operations)

/**
 * \*用户表持久层接口
 * \* Created with IntelliJ IDEA.
 * \* Author: Poison
 * \* Date: 2020/1/7
 * \* CreateTime: 16:38
 * \
 */
public interface UserMapper {
    /**
     * 查询所有用户
     * @return
     */
    List<User> findAll();
}

3. Configure SQL query of XXXmapper.xml file (the file name corresponding to the above mapper interface name)

<?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="cn.poison.mapper.UserMapper">
    <!-- 配置查询所有用户 -->
    <select id="findAll" resultType="cn.poison.pojo.User">
        select * from user
    </select>
</mapper>

Here, xml-based configuration is complete, you can use the test

public static void main(String[] args) throws  Exception{
        //1.读取配置文件
        InputStream in= Resources.getResourceAsStream("mybatis/SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        //3.使用工厂生产SqlSession对象
        UserMapperImpl mapper = new UserMapperImpl(factory);
        //5.使用代理对象执行方法
        List<User> userList = mapper.findAll();
        userList.forEach(li-> System.out.println(li));

        //6.释放资源
        in.close();


    }

Second, the annotation-based configuration

This compares with the embodiment xml-based configuration, reference to the above

1. Other configurations based xml the same, does not require the configuration file based on the use mapper.xml annotation, in use, only need to add annotations in the sql statement and method mapper interface

2. To modify the file in SqlMapConfig.xml

Thus ended configuration

Published 16 original articles · won praise 1 · views 412

Guess you like

Origin blog.csdn.net/qq_38763540/article/details/104073931