Mybatis create a simple project based on xml configuration file

A, mybatis built environment

1. Create a maven project and import coordinates.
2. Create dao entity classes and interfaces.
3. Create mybatis main configuration file mybatis-config.xml.
4. Create a mapping configuration file IUserDaoMapper.xml.

Second, the built environment considerations

1.mybatis mapping configuration file and the position must be the same packet structure dao interface.
2. mapper mapping configuration file tag namespacevalue attribute must be fully qualified class name dao interface
3. The configuration profile mapping operation (select), id attribute values must be the name of the method dao interface
when we keep the the above points, we do not need to write in development dao implementation class.

1. In the pom.xml <version>under way to write the guide package:

<packaging>jar</packaging>

2. go maven repository URL to find the dependence of the coordinates and add mybatis

<dependencies>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.1</version>
	</dependency>
	<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
</dependencies> 

3. Create a profile dbconfig.properties database:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/eesy
username=root
password=123456

4. Create a mybatis-config.xml file.

<?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>
    <!--导入配置数据库的配置文件-->
    <properties resource="dbconfig.properties"></properties>
    <!--配置环境-->
    <environments default="mysql">
        <!--配置mysql的环境-->
        <environment id="mysql">
            <!--配置事务的类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置数据源(连接池)-->
            <dataSource type="POOLED">
                <!--配置连接数据库的基本信息-->
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper class="net/togogo/dao/IUserDaoMapper.xml"></mapper>
    </mappers>
</configuration>

5. Create dao bean class interfaces and the data of Table
Datasheet:
Here Insert Picture Descriptionbean class (User):

package net.togogo.bean;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
    private Integer id;
    private String username;
    private Date birthday;
    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 getAddress() {
        return address;
    }

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

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

IUserDao interface methods:

package net.togogo.dao;

import net.togogo.bean.User;

import java.util.List;

/**
 * 用户的持久层接口
 */
public interface IUserDao {
    /**
     * 查询所有操作
     * @return
     */
    List<User> findAll();

}

6. Create an interface configuration file IUserDao.xml

<?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="net.togogo.dao.IUserDao">
    <select id="findAll" resultType="net.togogo.bean.User">
        select * from user
    </select>
</mapper>

7. Test class mybatisTest
A. Read configuration file
b. Create SqlSessionFactory plant
c. Create the SqlSession
D. Dao interface proxy object creates
e. Performing dao Method
f. Release resources

package net.togogo;

import net.togogo.bean.User;
import net.togogo.dao.IUserDao;
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;

/**
 * mybatis的入门案例
 *
 */
public class mybatisTest {

    public static void main(String[] args) throws IOException {
        //1.读取配置文件
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        //3.使用工厂生产SqlSession对象
        SqlSession session = factory.openSession();
        //4.使用SqlSession创建Dao接口的代理对象
        IUserDao iUserDao = session.getMapper(IUserDao.class);
        //5.使用代理对象执行方法
        List<User> users = iUserDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
        //6.释放资源
        session.close();
        in.close();
    }
}

8. Run results
Here Insert Picture Description

Published 31 original articles · won praise 1 · views 269

Guess you like

Origin blog.csdn.net/weixin_41605945/article/details/104099147