maven build mybatis test

Use maven to build the project, pom file guide package as follows:

<?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>com.zs</groupId>
    <artifactId>mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45-bin</version>
        </dependency>
    </dependencies>

</project>

After you import mybatis core package, create entity classes:

package com.zs.entity;

public class Login {
    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 "Login{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

Create a dao layer interface:

com.zs.dao JPackage; 

Import com.zs.entity.Login; 

/ ** 
 * Create login interfaces 
 * / 
public  interface LoginDao {
     / ** 
     * query the user information according to the query id 
     * / 
    the Login getById ( int id); 

}

Creating mybatis basic 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">
<configuration>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <!--配置mybatis连接数据库的连接池信息-->
            <configuration database basic information<-!>= "the POOLED"of the typedataSource
                -->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybase?characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!- <->The project mappper configuration files come in
    mappers>
        <mapper resource="mapper/login.xml"/>
    </mappers>
</configuration>

mapper file as follows:

<!DOCTYPE mapper PUBLIC "-//mybatis.org// Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zs.dao.LoginDao">
    <select id="getById" resultType="com.zs.entity.Login">
        select * from login where id=#{id}
    </select>
</mapper>

Then create a utility class, because of the need to read the configuration information to connect the database xml file, create the same tools as the previous link to the database, which also created tools:

package com.zs.util;

import com.sun.deploy.util.SessionProperties;
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;

public class SqlSessionUtil {
    private static SqlSessionFactory sessionFactory;
    static {
        InputStream in = null;
        try {
            in = Resources.getResourceAsStream("mybatis-config.xml");
            sessionFactory=new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static SqlSession getSession(){
        return sessionFactory.openSession();
    }
}

carry out testing:

import com.zs.dao.LoginDao;
import com.zs.entity.Login;
import com.zs.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

public class loginTest {
    public static void main(String[] args) {
        SqlSession session = SqlSessionUtil.getSession();
        LoginDao mapper = session.getMapper(LoginDao.class);
        Login byId = mapper.getById(1);
        System.out.println(byId);
    }
}

The results are as follows:

 

Guess you like

Origin www.cnblogs.com/Zs-book1/p/10939461.html