最近接触的项目中使用了Mybatis框架,觉得 Mybatis使用起来非常方便,决定从基础开始学习Mybatis。我准备的环境如下:
JDK1.5
Mybatis 3.3.0版本
MySQL数据库
Eclipse Neon.3 Release (4.6.3)
首先通过Eclipse创建一个Maven Simple Project,通过配置其pom.xml添加我们所需要的jar包,在这里我添加了Log4j,JUnit和MySQL驱动等依赖。
4.0.0
tk.mybatis
sample
0.0.1-SNAPSHOT
UTF-8
junit
junit
4.12
test
org.mybatis
mybatis
3.3.0
mysql
mysql-connector-java
5.1.38
org.slf4j
slf4j-api
1.7.12
org.slf4j
slf4j-log4j12
1.7.12
log4j
log4j
1.2.17
maven-compiler-plugin
1.5
1.5
在这里我通过xml形式对Mybatis进行配置,在src/main/resources下创建mybatis-config.xml配置文件如下:
在Mybatis中,一个表一般对应一个实体类,用于进行增删改查等操作。这里我在数据库中建的表结构如下
根据表结构,在src/main/java下创建包tk.mybatis.sample.model,在该包下创建实体类Country如下
package tk.mybatis.sample.model;
public class Country {
private long id;
private String countryname;
private String countrycode;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCountryname() {
return countryname;
}
public void setCountryname(String countryname) {
this.countryname = countryname;
}
public String getCountrycode() {
return countrycode;
}
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
}
接着在src/main/resources下创建tk/mybatis/sample/mapper目录,在该目录下创建CountryMapper.xml文件如下:
在
在src/main/resources中添加log4j.properties配置文件如下:
#global property
log4j.rootLogger=ERROR, stdout
#MyBatis log property
log4j.logger.tk.mybatis.sample.mapper=TRACE
#console out property
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
在src/test/java中创建tk.mybatis.sample.mapper包,在该包下创建CountryMapperTest测试类如下:
package tk.mybatis.sample.mapper;
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.BeforeClass;
import org.junit.Test;
import tk.mybatis.sample.model.Country;
public class CountryMapperTest {
private static SqlSessionFactory sqlSessionFactory;
@BeforeClass
public static void init(){
try{
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
} catch (IOException ignore) {
ignore.printStackTrace();
}
}
@Test
public void testSelectAll(){
SqlSession sqlSession = sqlSessionFactory.openSession();
try{
List countryList = sqlSession.selectList("selectAll");
printCountryList(countryList);
} finally {
//关闭sqlSession
sqlSession.close();
}
}
private void printCountryList(List countryList) {
// TODO Auto-generated method stub
for(Country country : countryList){
System.out.printf("%-4d%-4s%-4s\n",country.getId()
,country.getCountryname(),country.getCountrycode());
}
}
}
首先通过Resources工具类将mybatis-config.xml配置文件读入Reader。
再通过SqlSessionFactoryBuilder使用Reader创建SqlSessionFactory工厂对象。
使用时通过工厂对象获取一个SqlSession,通过其selectList方法找到CountryMapper.xml中id为selectAll的方法并执行查询。
Mybatis使用JDBC执行SQL,获得查询结果集,根据返回值类型将结果映射到Country类型的集合中。
在结束时需要关闭SqlSession,否则会因为连接未关闭导致数据库连接数过多造成崩溃。
项目测试成功后,会输出如下内容:
整个项目结构如下所示:
下一篇:Mybatis学习日记(二)——单个参数的增删改查