项目相关包文件的建立:
1.1新建Java项目,导入相关的包
新建一个lib文件,导入基础的包,其中commons-logging-x.x.jar、mybatis-x.x.x.jar、mysql-connector-java-x.x.x-bin.jar是基础包,log4j-x.x.x.jar包和junit-x.x.x.jar包是为了显示日志信息和方便测试使用的包。别忘了Build Path。
1.2建库+表
create database mybatis;
use mybatis;
CREATE TABLE users(id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(20), age INT);
INSERT INTO users(NAME, age) VALUES('Tom', 12);
INSERT INTO users(NAME, age) VALUES('Jack', 11);
1.3添加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" />
<dataSource type="POOLED">
<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>
</configuration>
1.4定义表和表所对应的实体类
这里一定要定义实体类的无参构造方法,否则会报异常。
package com.entity;
public class User {
private int id;
private String name;
private int age;
public User()
{
}
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
1.5定义操作 **user **表的 **sql **映射文件 userMapper.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.config.userMapper">
<!-- 根据id得到一个user对象 -->
<!-- 参数类型:parameterType resultType:结果集类型(全类名)-->
<select id="getUser" parameterType="int"
resultType="com.entity.User">
select * from user where id=#{id}
</select>
</mapper>
1.6在 **conf.xml **文件中注册 **userMapper.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://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- 注册映射文件 -->
<mappers>
<mapper resource="com/config/userMapper.xml"/>
</mappers>
</configuration>
1.7编写测试代码:执行定义的 select 语句
package com.test;
import java.io.IOException;
import java.io.Reader;
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 com.entity.User;
public class Test1 {
@Test
public void getUser() throws IOException
{
String resource = "config.xml";
//加载 mybatis 的配置文件(它也加载关联的映射文件)
Reader reader = Resources.getResourceAsReader(resource);
//构建 sqlSession 的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//创建能执行映射文件中 sql 的 sqlSession
SqlSession session = sessionFactory.openSession();
//映射 sql 的标识字符串
String statement = "com.config.userMapper.getUser";
//执行查询返回一个唯一 user 对象的 sql
User user = session.selectOne(statement, 15);
session.close();
System.out.println(user);
}
}