Learn to analyze notes _mybatis01_mybatis design patterns

1. What is the framework?

It is our software development of a solution that is different framework for resolving different problems.
The benefits of using the framework:
the framework encapsulates a lot of detail, so that developers can use minimalist manner, greatly improving development efficiency.

Overview of mybatis

mybatis is a persistence framework, java prepared. It encapsulates many of the details jdbc operations, so that developers only need to focus sql statement itself, without concern registration drive, create a complicated process connections, etc. Using the ORM package ideas to achieve the result set.

SNAKE

Object Relational Mappging object-relational mapping
is to database tables and entity classes and physical properties of the corresponding class together
so that we can work with entity classes to achieve operation of the database table.

2 Create Mybatis main configuration 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">
<!--mybaits的主配置文件-->
<configuration>
    <!--配置环境-->
    <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/mybatis"/>
                //此处的mybatis是你连接的数据库名称
                <property name="username" value="root"/>
                <property name="password" value="root"/>

            </dataSource>
        </environment>
    </environments>

    <!--指定映射配置文件的位置,映射配置文件指的是每个dao配置的文件-->
    <mappers>
        <!--<mapper resource="com/daniel/dao/IUserDao.xml"/>  使用的是xml-->
        <mapper class="com.daniel.dao.IUserDao"/>    <!--使用的是注解 annotations-->
    </mappers>

</configuration>

3. mybatis design pattern analysis

Here is a simple test mybatis class code (before you want to create the entity class and implement dao interfaces, create Mybatis main configuration file).

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

1. Read the configuration file

Purpose: The information is loaded SqlMapConifg.xml

The first: class loader, class path but can only read the configuration file
a second: ServletContext object using the getRealPath () method

2. Create SqlSessionFactory factory

Use the constructors mode, the details of creating objects hidden, so that users can get directly to the object
builder is the builder, we only need to enter in the plant can be obtained.

3. Production SqlSession objects

This step produced using the factory pattern SqlSession objects, can be reduced dependencies between classes.
Objective: Session object created can let the program create dao proxy object interface methods to achieve the purpose of enhancement.
Do not use new SqlSession to create the object because you need to replace re-implementation will change the source code on the actual development of a series of operations, resulting in inconvenience.

4. Create a proxy object

Create a dao implementation class using a proxy model, in order to add findAll methods to achieve the purpose. (FindAll entity class defines methods dao interface)

Published 33 original articles · won praise 0 · Views 506

Guess you like

Origin blog.csdn.net/naerjiajia207/article/details/103395466