1 MyBatis是什么?
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。(摘自:MyBatis中文官网)
2 MyBatis环境搭建及简单案例
2.1 新建工程,添加Maven依赖
为了MyBatis系列文章讲解方便,我直接在Eclipse中新建一个名为“demo_mybatis”的war工程,并且使用强大的Maven工具来管理jar包。初学者如果对Maven不熟悉,也可以直接建一个普通工程,将jar包下载好之后放到 lib 目录下。
<!-- 添加junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 添加mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- 添加mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.33</version>
</dependency>
<!-- 添加log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
2.2 配置configuration.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/tinytime_demo" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="com/demo/mybatis/mapper/BlogMapper.xml" />
</mappers>
</configuration>
2.3 创建一张Blog表
-- ----------------------------
-- Database
-- ----------------------------
CREATE DATABASE IF NOT EXISTS tinytime_demo DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
-- ----------------------------
-- Table structure
-- ----------------------------
DROP TABLE IF EXISTS `t_mybatis_blog`;
CREATE TABLE `t_mybatis_blog` (
`id` int(11) NOT NULL,
`title` varchar(20) NOT NULL,
`content` varchar(500) DEFAULT NULL,
`author` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 插入一条数据
insert into t_mybatis_blog(id,title,content,author) values(1,'Mybatis之Mybatis初体验','Mybatis之Mybatis初体验','demo_mybatis');
2.4 编写代码
首先,来看看整个目录结构:
2.4.1 实体类Blog.java,与t_blog表对应
public class Blog {
private Integer id;
private String title;
private String content;
private String author;
//getter and setter
}
2.4.2 BlogMapper接口
public interface BlogMapper {
Blog selectBlogByID(Integer id);
}
2.4.3 BlogMapper.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.demo.mybatis.dao.BlogMapper">
<select id="selectBlogByID" resultType="com.demo.mybatis.model.Blog" parameterType="java.lang.Integer">
select id,title,content,author from t_mybatis_blog where id = #{id}
</select>
</mapper>
2.4.4 编写Junit测试代码:BlogMapperTest
public class BlogMapperTest {
@Test
public void selectBlogByID() {
SqlSession sqlSession = getSessionFactory().openSession();
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
Blog blog = blogMapper.selectBlogByID(1);
Assert.assertNotNull(blog);
}
private static SqlSessionFactory getSessionFactory() {
SqlSessionFactory sessionFactory = null;
String resource = "configuration.xml";
try {
sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
} catch (IOException e) {
e.printStackTrace();
}
return sessionFactory;
}
}