一、引言
MyBatis是支持普通SQL查询,存储过程和高级映射到优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis用简单的XML或注解用于配置和原始映射,将接口和java的POJOs(Plan Old Java Objects,普通的Javs对象)映射成数据库中的记录。
每一个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心。SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象来获得。SqlSessionFactoryBuilder对象可以从XML配置文件或Configuration类的实例对象构建SqlSessionFactory对象。
二、入门demo
1.构建数据库表
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
2.根据t_user表构建java类
User.java
package com.mybatis.pojo;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3.创建映射配置文件
MyBatisConfig.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>
<typeAliases>
<typeAlias alias="user" type="com.mybatis.pojo.User"/>
</typeAliases>
<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/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/map/User.xml" />
</mappers>
</configuration>
其中主要配置了数据库连接的相关参数,以及映射文件所在的路径
User.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="/">
<select id="findById" parameterType="int" resultType="com.mybatis.pojo.User">
select *
from t_user where id=#{id}
</select>
</mapper>
该文件主要配置了操作t_user表的相关SQL语句
4.创建查询测试类
Test.Hello.java
package com.mybatis.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 com.mybatis.pojo.User;
public class TestHello {
public static void main(String[] args) {
String resource = "com/mybatis/map/MyBatisConfig.xml";
Reader reader = null;
SqlSession session;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
session = sqlMapper.openSession();
User temp = session.selectOne("findById", 1);
System.out.println(temp.getUsername());
session.close();
}
}
5.测试结果
2015-11-11 15:18:36,119 [main] DEBUG [/.findById] - ==> Preparing: select * from t_user where id=?
2015-11-11 15:18:36,159 [main] DEBUG [/.findById] - ==> Parameters: 1(Integer)
2015-11-11 15:18:36,207 [main] DEBUG [/.findById] - <== Total: 1
root
控制台输出如上信息,其中打印出上面User.xml文件中配置的SQL语句,查询得到一条记录。