Mybatis不使用Spring框架(Druid)

1、Mysql database

CREATE TABLE mockrecord
 (id INT(10) unsigned PRIMARY KEY NOT NULL COMMENT 'mock记录编号' AUTO_INCREMENT,
  requestparams TEXT COMMENT '请求参数列表',
  responsejspn TEXT COMMENT '返回Mock响应Json'
 )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2、maven project

3、pom文件

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.48</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.20</version>
    </dependency>

4、mybatis-config.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>
    <properties resource="mydbconfig.properties" />

    <typeAliases>
        <typeAlias type="com.test.mybatis.druid.DruidDataSourceFactory" alias="DRUID" />
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!--//POOLED指的是org.apache.ibatis.datasource.pooled.PooledDataSource, 一个数据源的实现类-->
            <!--<dataSource type="POOLED">-->
            <dataSource type="DRUID">

            <property name="driver" value="${db.driver}" />
                <property name="url" value="${db.url}" />
                <property name="username" value="${db.username}" />
                <property name="password" value="${db.password}" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="MockRecordMapper.xml" />
    </mappers>
</configuration>

5、mydbconfig.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://192.168.8.101:3306/mybatis
db.username=root
db.password=root

6、Bean

package com.test.mybatis.bean;

public class MockRecord {
    public Integer id;
    public String requestparams;
    public String responsejson;

    public MockRecord() {
    }

    public MockRecord(Integer id, String requestparams, String responsejson) {
        this.id = id;
        this.requestparams = requestparams;
        this.responsejson = responsejson;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getRequestparams() {
        return requestparams;
    }

    public void setRequestparams(String requestparams) {
        this.requestparams = requestparams;
    }

    public String getResponsejson() {
        return responsejson;
    }

    public void setResponsejson(String responsejson) {
        this.responsejson = responsejson;
    }

    @Override
    public String toString() {
        return "MockRecord{" +
                "id=" + id +
                ", requestparams='" + requestparams + '\'' +
                ", responsejson='" + responsejson + '\'' +
                '}';
    }
}

7、DruidDataSourceFactory

package com.test.mybatis.druid;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.datasource.DataSourceFactory;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

public class DruidDataSourceFactory implements DataSourceFactory {
    private Properties props;
    @Override
    public DataSource getDataSource() {
        DruidDataSource dds = new DruidDataSource();
        dds.setDriverClassName(this.props.getProperty("driver"));
        dds.setUrl(this.props.getProperty("url"));
        dds.setUsername(this.props.getProperty("username"));
        dds.setPassword(this.props.getProperty("password"));
        // 其他配置可以根据MyBatis主配置文件进行配置
        try {
            dds.init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dds;
    }
    @Override
    public void setProperties(Properties props) {
        this.props = props;
    }
}

8、MockRecordMapper.class,MockRecordMapper.xml

package com.test.mybatis.mapper;

import com.test.mybatis.bean.MockRecord;

public interface MockRecordMapper {
    public MockRecord selectMockRecord(String requestparams);
}



<?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="com.test.mybatis.mapper.MockRecordMapper">
    <select id="selectMockRecord" resultType="com.test.mybatis.bean.MockRecord">
        select id,requestparams,responsejson from mockrecord where requestparams = #{requestparams}
    </select>
</mapper>

9、AppTest集成测试

package com.test.mybatis;

import static org.junit.Assert.assertTrue;

import com.test.mybatis.bean.MockRecord;
import com.test.mybatis.mapper.MockRecordMapper;
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;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void test1() throws IOException
    {
        // 1、获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        // 2、获取sqlSession对象
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            // 3、获取接口的实现类对象
            //会为接口自动的创建一个代理对象,代理对象去执行增删改查方法
            MockRecordMapper mapper = openSession.getMapper(MockRecordMapper.class);
            MockRecord mockRecord = mapper.selectMockRecord("a");
            System.out.println(mapper.getClass());
            System.out.println(mockRecord);
        } finally {
            openSession.close();
        }
    }

}
posted @ 2019-10-18 10:16  xidianzxm  阅读(1775)  评论(0编辑  收藏  举报