简介
MyBatis 是持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射,通过将参数映射到配置的SQL形成最终执行的SQL语句,最后将执行SQL的结果映射成Java对象返回。与其他的ORM框架不同,MyBatis并没有将Java对象与数据库表关联起来,而是将Java方法与SQL语句关联。MyBatis的前身是iBATIS,iBATIS 于2010年改名为MyBatis。
链接
中文文档地址:http://www.mybatis.org/mybatis-3/zh/index.html
MyBatis官方GitHub地址:https://github.com/mybatis/mybatis-3
示例
- IDE:IntelliJ IDEA
- 创建Maven项目
File → Project → Maven(maven-archetype-quickstart)→
GroupId(cn.jujianfei)| ArtifactId(demo-mybatis)→ Next .. → Finish
- 添加依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
- 准备数据库
CREATE TABLE country (
id INT NOT NULL AUTO_INCREMENT,
countryname VARCHAR (20) DEFAULT NULL,
countrycode VARCHAR (20) DEFAULT NULL,
PRIMARY KEY (id)
);
INSERT INTO country (countryname, countrycode)
VALUES
('法国', 'FR'),
('克罗地亚', 'HR'),
('比利时', 'BE'),
('英国', 'UK');
- 配置MyBatis
在main目录上右击 → New → Directory → 输入名称resources → 右击 → Mark Directory as → Resources Root。在该目录下创建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>
<typeAliases>
<package name="cn.jujianfei.model"></package>
</typeAliases>
<environments default="development">
<!-- 环境配置,即连接的数据库。 -->
<environment id="development">
<!-- 指定事务管理类型,type="JDBC"指直接简单使用了JDBC的提交和回滚设置。 -->
<transactionManager type="JDBC"/>
<!-- dataSource指数据源配置,POOLED是JDBC连接对象的数据源连接池的实现。 -->
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/t_mybatis"/>
<property name="username" value="root"/>
<property name="password" value="jujianfei"/>
</dataSource>
</environment>
</environments>
<!-- mappers告诉了MyBatis去哪里找持久化类的映射文件。 -->
<mappers>
<mapper resource="mapper/CountryMapper.xml"/>
</mappers>
</configuration>
- 创建实体类和Mapper.xml文件
在src/main/java下创建一个基础包cn.jujianfei.model,在包下创建实体类Country,代码如下:
public class Country {
private int id;
private String countryName;
private String countryCode;
//getter和setter方法省略
@Override
public String toString(){
return "Id=" + this.getId() +
", CountryName=" + this.getCountryName() +
", CountryCode="+this.getCountryCode();
}
}
在src/main/resources下创建一个文件夹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="mapper.CountryMapper">
<select id="selectAll" resultType="Country">
select id,countryname,countrycode from country
</select>
</mapper>
- 测试
public class AppTest
{
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 test(){
SqlSession sqlSession= sqlSessionFactory.openSession();
List<Country> countryList = sqlSession.selectList("selectAll");
countryList.forEach(System.out::println);
sqlSession.close();
}
}
测试结果
Id=1, CountryName=法国, CountryCode=FR
Id=2, CountryName=克罗地亚, CountryCode=HR
Id=3, CountryName=比利时, CountryCode=BE
Id=4, CountryName=英国, CountryCode=UK
参考资料: http://www.mybatis.org/mybatis-3/zh/index.html
参考资料: https://github.com/mybatis/mybatis-3
参考资料:《MyBatis从入门到精通》