SSM框架中mybatis接口测试工具类

工具类如下:
注意“./xml/mybatis-config.xml”指的是项目目录中mybatis的配置路径

package base;

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.Reader;
import java.sql.Connection;

/**
 * mybatis工具类
 @author panda
 *
 */
public class MybatisUtil {

    private static ThreadLocal threadLocal = new ThreadLocal();
    private static SqlSessionFactory sqlSessionFactory;
    static{
        try {
            Reader reader = Resources.getResourceAsReader("./xml/mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    private MybatisUtil() {}

    public static SqlSession getSqlSession(){
        SqlSession sqlSession = threadLocal.get();
        if(sqlSession == null){
            sqlSession = sqlSessionFactory.openSession();
            threadLocal.set(sqlSession);
        }
        return sqlSession;
    }

    public static void closeSqlSession(){
        SqlSession sqlSession = threadLocal.get();
        if(sqlSession != null){
            sqlSession.close();
            threadLocal.remove();
        }
    }

    public static void main(String[] args) {
        Connection conn = MybatisUtil.getSqlSession().getConnection();
        System.out.println(conn!=null ? "连接成功" : "连接失败");
    }
}

mybatis-config.xml 的配置:



<configuration>
    
    <settings>
        
        <setting name="cacheEnabled" value="true" />
        
        <setting name="lazyLoadingEnabled" value="true" />
        <setting name="multipleResultSetsEnabled" value="true" />
        <setting name="useColumnLabel" value="true" />
        <setting name="defaultExecutorType" value="REUSE" />
        <setting name="defaultStatementTimeout" value="25000" />
    settings>

    
    <typeAliases>
        
    typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/xxxx"/>
                <property name="username" value="xxxx"/>
                <property name="password" value="xxxx"/>
            dataSource>
        environment>
    environments>
    <mappers>
        <mapper resource="com/blog/ifootage/mapper/xml/FeedsMapper.xml"/>
    mappers>
configuration>

关联的的feedsMapper接口与实体类



<mapper namespace="com.blog.ifootage.mapper.FeedsMapper">

    
    <select id="selectFeedsById" resultType="com.blog.ifootage.entity.Feeds" parameterType="java.lang.Integer">
    SELECT * FROM mto_feeds WHERE  author_id = #{authorId}
    select>


mapper>
package com.blog.ifootage.entity;

import com.baomidou.mybatisplus.activerecord.Model;

import java.io.Serializable;
import java.util.Date;


/**
 * 

* *

* * @author panda * @since 2017-06-15 */
//@TableName("mto_feeds") public class Feeds extends Model { private static final long serialVersionUID = 1L; private Long id; // @TableField("author_id") private Integer author_id; private Date created; // @TableField("own_id") private Integer own_id; // @TableField("post_id") private Integer post_id; private Integer type; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Integer getType() { return type; } public void setType(Integer type) { this.type = type; } @Override protected Serializable pkVal() { return this.id; } public Integer getAuthor_id() { return author_id; } public void setAuthor_id(Integer author_id) { this.author_id = author_id; } public Integer getOwn_id() { return own_id; } public void setOwn_id(Integer own_id) { this.own_id = own_id; } public Integer getPost_id() { return post_id; } public void setPost_id(Integer post_id) { this.post_id = post_id; } }

开始MyBatis的接口测试了

/**
 * Created by pengdan on 2017/10/14.
 */
public class Test1 {

    @Test
    public void select(){
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            List i = sqlSession.selectList("com.blog.ifootage.mapper.FeedsMapper.selectFeedsById",20);
            System.out.println("本次操作影响"+i+"行数据");
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }
        finally{
            MybatisUtil.closeSqlSession();
        }
    }
}

你可能感兴趣的:(工具使用,JAVA应用层)