Mybatis study notes the environment to build ---

Mybatis environment to build

(A) environment to build

(1) Step 1: Create project and import maven jar package

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
  </dependency>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.41</version>
  </dependency>
  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
  </dependency>

</dependencies>

 

(2) Step 2: Create entity classes and interfaces dao

(3) Step 3: Create Mybatis main configuration file SqlMapperConfig.xml

<? Xml Version = "1.0" encoding = "UTF-8" ?> 

<! DOCTYPEconfiguration 

        the PUBLIC "- // mybatis.org//DTD Config 3.0 // EN" 

        "http://mybatis.org/dtd/mybatis- -config.dtd. 3 " > 

<-! MyBatis main configuration file -> 

< configuration > 

    <-! configuration environment -> 

    < environments default =" mysql " > 

        <-! mysql configuration environment -> 

        < Environment ID = "MySQL" > 

            <-! type of transaction configuration -> 

            <transactionManager type="JDBC"></transactionManager> 

            <! - the configuration data source (connection pool) -> 

            < the dataSource type = "the POOLED" > 

                <! - . 4 basic configuration database connection information -> 

                < Property name = "Driver" value = "COM. mysql.jdbc.Driver " > </ Property > 

                < Property name =" URL " value =" JDBC: MySQL: // localhost:? 3306 / Zml to true useUnicode = & amp; = UTF-characterEncoding. 8 " > </ Property > 

                < Property name = "username" value = "the root" > </property>

                <property name= "password" value = "the root" > </ Property > 

            </ the dataSource > 

            

        </ Environment > 

    </ Environments > 

    <-! position specified mapping configuration file, a mapping configuration file refers to each separate profiles dao -> 

< by mappers > 

    < Mapper Resource = "DAO / UserDao.xml" > </ Mapper > 

</ by mappers > 

</ Configuration >

 

(4) Step Four: Create a mapping configuration file UserDao.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPEmapper

        PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="dao.UserDao">

    <select id="findAll" resultType="entity.Users">

        select * from users;

    </select>

</mapper>
 

 

(B) Notes environment to build

(1) creation and UserDao.java when UserDao.xml name to our previous knowledge and keeping - induced. In Mybatis in its name and the operator interface mapping file persistence layer is also called: Mapper so: UserDao and UserMapper is the same

(2) create a directory in the idea of ​​time, it is not the same package and the package when creating: com.itheina.dao tertiary structure when it is created in the directory: com.itheima.dao is a directory

(3) mapping the position of the profile must mybatis interface and the same packet structure dao

Namespace attribute value mapper tag (4) mapping configuration file must be fully qualified class name of the interface dao

(5) operating configuration mapping configuration file (select), the value of the id attribute must be the name of the method dao interface, when we comply with the third, fourth, five points, we do not need to write achieve dao in development class

(Iii) a first instance Mybatis

 

(1) The first step:

pom.xml

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
  </dependency>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.41</version>
  </dependency>
  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
  </dependency>

</dependencies>

 

(2) Step two:

Users.java

package entity;



public class Users {

    private int id;

    private String username;

    private String password;



    public int getId() {

        return id;

    }



    public void setId(int id) {

        this.id = id;

    }



    public String getUsername() {

        return username;

    }



    public void setUsername(String username) {

        this.username = username;

    }



    public String getPassword() {

        return password;

    }



    public void setPassword(String password) {

        this.password = password;

    }



    @Override

    public String toString() {

        return "Users{" +

                "id=" + id +

                ", username='" + username + '\'' +

                ", password='" + password + '\'' +

                '}';

    }

}

 

UserDao.java

package dao;



import entity.Users;



import java.util.List;



public interface UserDao {

     List<Users> findAll();

}

 

(3) Third Step:

SqlMapperConfig.xml

<? Xml Version = "1.0" encoding = "UTF-8" ?> 

<! DOCTYPEconfiguration 

        the PUBLIC "- // mybatis.org//DTD Config 3.0 // EN" 

        "http://mybatis.org/dtd/mybatis- -config.dtd. 3 " > 

<-! MyBatis main configuration file -> 

< configuration > 

    <-! configuration environment -> 

    < environments default =" mysql " > 

        <-! mysql configuration environment -> 

        < Environment ID = "MySQL" > 

            <-! type of transaction configuration -> 

            <transactionManager type="JDBC"></transactionManager> 

            <! - the configuration data source (connection pool) -> 

            < the dataSource type = "the POOLED" > 

                <! - . 4 basic configuration database connection information -> 

                < Property name = "Driver" value = "COM. mysql.jdbc.Driver " > </ Property > 

                < Property name =" URL " value =" JDBC: MySQL: // localhost:? 3306 / Zml to true useUnicode = & amp; = UTF-characterEncoding. 8 " > </ Property > 

                < Property name = "username" value = "the root" > </property>

                <property name= "password" value = "the root" > </ Property > 

            </ the dataSource > 

            

        </ Environment > 

    </ Environments > 

    <-! position specified mapping configuration file, a mapping configuration file refers to each separate profiles dao -> 

< by mappers > 

    < Mapper Resource = "DAO / UserDao.xml" > </ Mapper > 

</ by mappers > 

</ Configuration >

 

(4) Fourth Step

UserDao.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPEmapper

        PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="dao.UserDao">

    <select id="findAll" resultType="entity.Users">

        select * from users;

    </select>

</mapper>

 

(5) Step 5: Create a test class

UserTest.java

package test;



import dao.UserDao;

import entity.Users;

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 org.junit.Test;



import java.io.IOException;

import java.io.InputStream;

import java.util.List;



public class UserTest {

    @Test

    public void findall() throws IOException {

        //1.读取配置文件

        In InputStream = Resources.getResourceAsStream ( "SqlMapperConfig.xml" ); 

        // 2. Create a factory SqlSessionFactory 

        the SqlSessionFactoryBuilder Builder = new new the SqlSessionFactoryBuilder (); 

        SqlSessionFactory Factory's = builder.build (in); 

        // 3. Use factory SqlSession objects 

        SqlSession session = factory.openSession (); 

        // 4. SqlSession used to create an interface proxy object dao 

        UserDao UserDaoImpl = session.getMapper (UserDao. class ); 

        // 5. the use of a proxy object execution method 

        List <the Users> Users = UserDaoImpl.findAll ( ); 

        for(Users u:users){

            System.out.println(u);

        }

        //6.释放资源

        session.close();

        in.close();

    }

}

 

Guess you like

Origin www.cnblogs.com/dyddzhp/p/11331676.html