案例代码:本地下载

1.创建java项目并导入jar包

mybatis-3.2.2.jar:本地下载

mysql-connector-java-5.1.18-bin.jar:本地下载

2.配置MyBatis框架

在src里面配置mybatis.xml。

mybatis.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="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/spring?useUnicode=true&amp;characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/bean/GoodsType.xml" />
</mappers>
</configuration>

3.建立数据库

数据库文件:本地下载

4.编写实体类

GoodsType.java

package com.bean;

public class GoodsType {
    private int typeId;
    private String typeName;
    public GoodsType(int typeId, String typeName) {
        super();
        this.typeId = typeId;
        this.typeName = typeName;
    }
    public GoodsType() {
    }
    public int getTypeId() {
        return typeId;
    }
    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }
    public String getTypeName() {
        return typeName;
    }
    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }
    @Override
    public String toString() {
        return "GoodsType [typeId=" + typeId + ", typeName=" + typeName + "]";
    }

}

5.编写一个与类同名的xml文件

GoodsType.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="com.bean.GoodsType">
<select id="findAll" resultType="com.bean.GoodsType">
select * from goods_type
</select>
</mapper>

6.编写测试的类

test.java

package com.test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

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

import com.bean.GoodsType;

public class test {

    private SqlSession sqlSession;

    public void test01() {
        try {
            List<GoodsType> goodsTypeList = this.sqlSession.selectList("com.bean.GoodsType.findAll");
            for (GoodsType goodsType : goodsTypeList) {
                System.out.println(goodsType);
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            this.sqlSession.close();
        }
    }

    @Test
    public void testF(){
        test01();
    }

    @Before
    public void before() {
        try {
            String resource = "mybatis.xml";
            Reader reader = Resources.getResourceAsReader(resource);
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
            this.sqlSession = ssf.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

7.运行测试类