1.源码下载
https://github.com/mybatis/mybatis-3
2.添加依赖
这一步很重要,如果想要使用自己的数据库调试,要在mybatis的pom中添加自己的驱动jar包。
本人使用的是MySQL,添加如下依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency>
3.添加自己的测试包
在项目的test包中新建自己的测试包
创建mybatis配置文件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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> </transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="hhh123"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/apache/ibatis/ztest/MbGoodsMapper.xml"/> </mappers> </configuration>
创建测试使用的POJO
package org.apache.ibatis.ztest; import java.math.BigDecimal; import java.util.Date; public class MbGoods { private String id; private String name; private BigDecimal price; private String description; private String type; private Integer status; private String createBy; private Date createTime; private String updateBy; private Date updateTime; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getCreateBy() { return createBy; } public void setCreateBy(String createBy) { this.createBy = createBy; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getUpdateBy() { return updateBy; } public void setUpdateBy(String updateBy) { this.updateBy = updateBy; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @Override public String toString() { return "MbGoods{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", price=" + price + ", description='" + description + '\'' + ", type='" + type + '\'' + ", status=" + status + ", createBy='" + createBy + '\'' + ", createTime=" + createTime + ", updateBy='" + updateBy + '\'' + ", updateTime=" + updateTime + '}'; } }
创建Mapper
package org.apache.ibatis.ztest; import org.apache.ibatis.annotations.Param; public interface MbGoodsMapper { MbGoods selectById(@Param("id") String id); }
创建Mapper.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="org.apache.ibatis.ztest.MbGoodsMapper"> <select id="selectById" resultType="org.apache.ibatis.ztest.MbGoods"> select * from mb_goods where id = #{id} </select> </mapper>
在配置文件mybatis-config.xml中对mapper进行配置
<?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"> </transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="hhh123"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/apache/ibatis/ztest/MbGoodsMapper.xml"/> </mappers> </configuration>
编写测试类
package org.apache.ibatis.ztest;
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.io.Reader;
public class MyTest {
private static SqlSessionFactory sqlSessionFactory;
@Test
public void test01() throws IOException {
//1、创建SqlSessionFactory
String resource = "org/apache/ibatis/ztest/mybatis-config.xml";
final Reader reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
//2、获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//3、获取mapper
MbGoodsMapper mapper = sqlSession.getMapper(MbGoodsMapper.class);
//4、执行数据库操作,并处理结果集
MbGoods goods = mapper.selectById("12");
System.out.println(goods);
}
}
到此执行成功,可以源码调试了