Getting started quickly learn -Mybatis framework

Chapter 2 Mybatis Framework Quick Start

By the previous study, we have been able to build a custom frame Mybatis the basics learned. This process is the basic skills test, we have a lot stronger, but the reality is cruel, we have defined Mybatis framework and real Mybatis framework compared to, or seem small. The popular within the industry framework Mybatis Now we will turn to learn.

2.1 Mybatis ready framework for the development of

2.1.1 official website to download Mybatis framework

"Mybatis download" can download the latest Mybatis development package from Baidu in.
Here Insert Picture Description
Access Select interface language, developed into the Chinese version of the document.
Here Insert Picture Description
Here Insert Picture Description

2.2 development environment to build Mybatis

2.2.1 Creating maven project

创建 mybatis01 的工程,
工程信息如下: 
Groupid:com.itheima 
ArtifactId:mybatis01
Packing:jar

2.2.2 adding the coordinates of Mybatis3.4.5

Add Mybatis3.4.5 in pom.xml file coordinates, as follows:

        <dependencies>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.5</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.6</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.12</version>
            </dependency>
        </dependencies>

2.2.3 write User entity class

/**
 *
 *<p>Title: User</p>
 *<p>Description: 用户的实体类</p>
 */
public class User implements Serializable {

    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 + "]";
    }


}

2.2.4 write the persistence layer interface IUserDao

IUserDao persistence interface is what we layer interface (or may be written UserDao UserMapper), specific code as follows:

public interface IUserDao {


    /**
     *查询所有用户
     *@return
     */
    List<User> findAll();
}

2.2.5 write persistence layer mapping file interface IUserDao.xml

Requirements:
Create positions: persistence layer interfaces must be on the same package.
Name: the file name must be named to the persistence layer interface name, extension is .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="com.itheima.dao.IUserDao">
    <!-- 配置查询所有操作 -->
    <select id="findAll" resultType="com.itheima.domain.User"> select * from user
</select>
</mapper>

2.2.6 write SqlMapConfig.xml profile

<?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>
    <!-- 配置 mybatis 的环境 -->
    <environments default="mysql">
        <!-- 配置 mysql 的环境 -->
        <environment id="mysql">
            <!-- 配置事务的类型 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置连接数据库的信息:用的是数据源(连接池) -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ee50"/>
                <property name="username" value="root"/>
                <property name="password" value="1234"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 告知 mybatis 映射配置的位置 -->
    <mappers>
        <mapper resource="com/itheima/dao/IUserDao.xml"/>
    </mappers>
</configuration>

2.2.7 write test classes

    public class MybatisTest {

        public static void main(String[] args)throws Exception {
            //1.读取配置文件
            InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.创建 SqlSessionFactory 的构建者对象
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            //3.使用构建者创建工厂对象 SqlSessionFactory SqlSessionFactory factory = builder.build(in);
            //4.使用 SqlSessionFactory 生产 SqlSession 对象
            SqlSession session = factory.openSession();
            //5.使用 SqlSession 创建 dao 接口的代理对象
            IUserDao userDao = session.getMapper(IUserDao.class);
            //6.使用代理对象执行查询所有方法List<User> users = userDao.findAll(); for(User user : users) {
            System.out.println(user);
            //7.释放资源
            session.close(); in.close();
        }
            
    }
Released 2044 original articles · won praise 2213 · Views 200,000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/105220021