mybits Getting Case

1: 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
that use the ORM package ideas to achieve the result set.

 

2: mybits Getting Started case: Use mybits be the simplest query (sql: select * from user)

mybatis environment to build (using xml mode)
Step 1: Create maven project and import coordinates
Step 2: Create entity classes and interfaces dao the
third step: to create a master configuration file SqlMapConifg.xml Mybatis the
fourth step: Create mapping configuration file IUserDao.xml

 

Notes environment to build:
the first one: When you create IUserDao.xml and IUserDao.java name to our previous knowledge and consistent.
In Mybatis in its name and the operator interface mapping file persistence layer is also called: Mapper
so: IUserDao and IUserMapper is the same as
the second: to create a directory in the idea of time, it is not the same package and
the package at the time of creation: it is com.itheima.dao tertiary structure
when the created directory: com.itheima.dao directory is a
third: mapping configuration file location mybatis and packages must be the same interface structure dao
fourth: mapping configuration file namespace attribute value mapper tag must be fully qualified class name dao interface
fifth: operating configuration mapping profile (select), the id attribute value must be the name of a method interface dao

When we comply with the third, fourth, five points, we do not need to write dao implementation class in development.

pom.xml

<?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>cn.edu.hznu</groupId>
    <artifactId>mybatis01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>

    </dependencies>

</project>

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="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/eesy_mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="199945"/>
            </dataSource>
        </environment>
    </environments>
<!--    映射配置文件-->
    <mappers>
        <mapper resource="cn/edu/hznu/dao/IUserDao.xml"/>
    </mappers>
</configuration>

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="cn.edu.hznu.dao.IUserDao">
<!--    配置查询索引-->
    <select id="findAll" resultType="cn.edu.hznu.domain.User">
        select * from user
    </select>
</mapper>

IUserDao.xml

Package cn.edu.hznu.dao; 

Import cn.edu.hznu.domain.User; 

Import java.util.List; 

/ ** 
 * the Created by WJJ ON 2020/3/28 
 * 
 persistence user diet * 
 * / 
public  interface IUserDao {
     / * 
     * query all user operations 
     * * / 
    List <the user> the findAll (); 
}

mybatisTest

package cn.edu.hznu.test;

import cn.edu.hznu.dao.IUserDao;
import cn.edu.hznu.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 javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * Created by wjj on 2020/3/28
 */
public class MybatisTest {
    /*
     * 入门案例
     * */
    public  static  void main (String [] args) throws   Exception {
         // 1. read the configuration file 
        the InputStream in Resources.getResourceAsStream = ( "the SqlMapConfig.xml" );
         // 2. Create sqlsessionfactory plant 
        the SqlSessionFactoryBuilder Builder = new new the SqlSessionFactoryBuilder (); 
        Factory a SqlSessionFactory = builder.build (in);
         // 3. production objects sqlsession 
        the SqlSession SQLSESSION = factory.openSession ();
         // 4. Create an interface proxy object using object 
        IUserDao userDao = sqlSession.getMapper (IUserDao. class );
         //The use of proxy objects executing 
        List <the User> Users = userDao.findAll ();
         for (the User User: Users) { 
            System.out.println (User); 
        } 
        // . 6, release resources 
        sqlSession.close (); 
        in .close (); 
    } 
}

Project Directory Structure

 

 

 

The first three steps are to sqlsession object because sqlsession object directly manipulate the database, in three steps, and different design pattern, is coupled to understand, and flexibility

After the fourth step obtained seesion object obtained by the interface corresponding to the class name Mapper object Mapper object has sql statement as well as a query class that uses a proxy mode, the interface proxy, subsequent operation of the master requires access to the proxy object, using agent may enhance existing methods (understood to be the underlying structure of the broker, the broker method may be implemented and modified)

Step Five Step Six execution method

 

Guess you like

Origin www.cnblogs.com/wjune-0405/p/12592039.html