①Eclipse 中打开 【File】----【New】选择【Other】
②选择【 Maven】 下的 【Maven Project】,点击【Next 】
③选择【Create a simple project (skip archetype selection )】前的复选框,点击【Next】
④输入 Group Id(tk.mybatis )、 Artifact Id (simpleMaven)、 Version (0.0.1-SNAPSHOT), 点击 【Finish】
Maven创建完成,如图。
pom.xml配置如下:在内加入
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tk.mybatis</groupId>
<artifactId>simple</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
①准备数据库
CREATE TABLE country(
id int NOT NULL auto_increment, //auto_increment主键自动增长
countryname varchar(255)NULL,
countrycode varchar(255)NULL,
PRIMARY KEY(id)
);
insert country(countryname,countrycode)
values('中国','CN'),
('美国','US'),
('俄罗斯','RU'),
('英国','GB'),
('法国','FR');
–全局配置文件:
<?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>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="model"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value=""/>
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="tk/mybatis/simpleMaven/mapper/CountryMapper.xml"/>
</mappers>
</configuration>
③创建实体类和Mapper.xml文件
src/main/java下创个model包,据DB表country ,在 model 包下创建实体类 Country
package model;
public class Country {
private Long id;
private String countryname;
private String countrycode;
public void setId(Long id) {this.id=id;}
public Long getId() {return id;}
public void setCountryname(String countryname) {this.countryname=countryname;}
public String getCountryname() {return countryname;}
public void setCountrycode(String countrycode) {this.countrycode=countrycode;}
public String getCountrycode() {return countrycode;}
}
src/main/resources 下面创tk/mybatis/simpleMaven/mapper 目录,再在该目录下面创CountryMapper.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="tk.mybatis.simpleMaven.mapper.CountryMapper">
<select id="selectAll" resultType="Country">
select id,countryname,countrycode from country;
</select>
</mapper>
④配置Log4j,以便查看MyBatis操作DB的过程
#全局配置
log4j.rootLogger=ERROR,stdout
#MyBatis日志配置
log4j.logger.tk.mybatis.simpleMaven.mapper.CountryMapper=DEBUG
#控制台输出配置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p[%t]-%m%n
⑤编写测试代码,运行MyBatis:首先在 src/test/java中创建mapper 包,后创建 CountryMapperTest测试类,代码如下。
package 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 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<Country> countryList=sqlSession.selectList("selectAll");
printCountryList(countryList);}
finally{
//不要忘记关闭 sqlSession
sqlSession.close();
}
}
private void printCountryList(List<Country> countryList){
for(Country country:countryList){
System.out.printf("%-4d%4s%4s\n",country.getId(),
country.getCountryname(),country.getCountrycode());
}
}
}