MyBatis入门学习教程-MyBatis快速入门
一、Mybatis 介绍
MyBatis 是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索封装。MyBatis 可以使用简单的XML 或注解用于配置和原始映射,将接口和 Java 的 POJO(Plain Old Java Objects,普通的 Java 对象)映射成数据库中的记录。
二、mybatis 快速入门
2.1、准备开发环境
1、创建测试项目,普通 java 项目或者是 JavaWeb 项目均可,如下图所示:
2、添加相应的 jar 包
【mybatis】
mybatis-3.1.1.jar
【MYSQL 驱动包】
mysql-connector-java-5.1.7-bin.jar
3、创建数据库和表,针对 MySQL 数据库
SQL 脚本如下:
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('孤傲苍狼', 27); INSERT INTO users(NAME, age) VALUES('白虎神皇', 27);
将 SQL 脚本在 MySQL 数据库中执行,完成创建数据库和表的操作,如下:
到此,前期的开发环境准备工作全部完成。
2.2、使用 MyBatis 查询表中的数据
1、添加 Mybatis 的配置文件 conf.xml
在 src 目录下创建一个 conf.xml 文件,如下图所示:
conf.xml 文件中的内容如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 <configuration> 4 <environments default="development"> 5 <environment id="development"> 6 <transactionManager type="JDBC" /> 7 <!-- 配置数据库连接信息 --> 8 <dataSource type="POOLED"> 9 <property name="driver" value="com.mysql.jdbc.Driver" /> 10 <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> 11 <property name="username" value="root" /> 12 <property name="password" value="XDP" /> 13 </dataSource> 14 </environment> 15 </environments> 16 17 </configuration>
2、定义表所对应的实体类,如下图所示:
User 类的代码如下:
1 package me.gacl.domain; 2 3 /** 4 * @author gacl 5 * users 表所对应的实体类 6 */ 7 public class User { 8 9 //实体类的属性和表的字段名称一一对应 10 private int id; 11 private String name; 12 private int age; 13 14 public int getId() { 15 return id; 16 } 17 18 public void setId(int id) { 19 this.id = id; 20 } 21 22 public String getName() { 23 return name; 24 } 25 26 public void setName(String name) { 27 this.name = name; 28 } 29 30 public int getAge() { 31 return age; 32 } 33 34 public void setAge(int age) { 35 this.age = age; 36 } 37 38 @Override 39 public String toString() { 40 return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; 41 } 42 }
3、定义操作 users 表的 sql 映射文件 userMapper.xml
创建一个 me.gacl.mapping 包,专门用于存放 sql 映射文件,在包中创建一个 userMapper.xml 文件,如下图所示:
userMapper.xml 文件的内容如下:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <!-- 为这个 mapper 指定一个唯一的 namespace,namespace 的值习惯上设置成包名 +sql 映射文件名,这样就能够保证 namespace 的值是唯一的 4 例如 namespace="me.gacl.mapping.userMapper" 就是 me.gacl.mapping(包名)+userMapper(userMapper.xml 文件去除后缀) 5 --> 6 <mapper namespace="me.gacl.mapping.userMapper"> 7 <!-- 在 select 标签中编写查询的 SQL 语句, 设置 select 标签的 id 属性为 getUser,id 属性值必须是唯一的,不能够重复 8 使用 parameterType 属性指明查询时使用的参数类型,resultType 属性指明查询返回的结果集类型 9 resultType="me.gacl.domain.User" 就表示将查询结果封装成一个 User 类的对象返回 10 User 类就是 users 表所对应的实体类 11 --> 12 <!-- 13 根据 id 查询得到一个 user 对象 14 --> 15 <select id="getUser" parameterType="int" 16 resultType="me.gacl.domain.User"> 17 select * from users where id=#{id} 18 </select> 19 </mapper>
4、在 conf.xml 文件中注册 userMapper.xml 文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 <configuration> 4 <environments default="development"> 5 <environment id="development"> 6 <transactionManager type="JDBC" /> 7 <!-- 配置数据库连接信息 --> 8 <dataSource type="POOLED"> 9 <property name="driver" value="com.mysql.jdbc.Driver" /> 10 <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> 11 <property name="username" value="root" /> 12 <property name="password" value="XDP" /> 13 </dataSource> 14 </environment> 15 </environments> 16 17 <mappers> 18 <!-- 注册 userMapper.xml 文件, 19 userMapper.xml 位于 me.gacl.mapping 这个包下,所以 resource 写成 me/gacl/mapping/userMapper.xml--> 20 <mapper resource="me/gacl/mapping/userMapper.xml"/> 21 </mappers> 22 23 </configuration>
5、编写测试代码:执行定义的 select 语句
创建一个 Test1 类,编写如下的测试代码:
package me.gacl.test;import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import me.gacl.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class Test1 {
</span><span style="color: rgba(0, 0, 255, 1); line-height: 1.5 !important">public</span> <span style="color: rgba(0, 0, 255, 1); line-height: 1.5 !important">static</span> <span style="color: rgba(0, 0, 255, 1); line-height: 1.5 !important">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1); line-height: 1.5 !important">throws</span><span style="line-height: 1.5 !important"> IOException { </span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">//</span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">mybatis的配置文件</span> String resource = "conf.xml"<span style="line-height: 1.5 !important">; </span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">//</span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)</span> InputStream is = Test1.<span style="color: rgba(0, 0, 255, 1); line-height: 1.5 !important">class</span><span style="line-height: 1.5 !important">.getClassLoader().getResourceAsStream(resource); </span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">//</span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">构建sqlSession的工厂</span> SqlSessionFactory sessionFactory = <span style="color: rgba(0, 0, 255, 1); line-height: 1.5 !important">new</span><span style="line-height: 1.5 !important"> SqlSessionFactoryBuilder().build(is); </span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">//</span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件) </span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">//</span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">Reader reader = Resources.getResourceAsReader(resource); </span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">//</span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">构建sqlSession的工厂 </span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">//</span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); </span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">//</span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">创建能执行映射文件中sql的sqlSession</span> SqlSession session =<span style="line-height: 1.5 !important"> sessionFactory.openSession(); </span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">/**</span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important"> * 映射sql的标识字符串, * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL </span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">*/</span><span style="line-height: 1.5 !important"> String statement </span>= "me.gacl.mapping.userMapper.getUser";<span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">//</span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">映射sql的标识字符串 </span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">//</span><span style="color: rgba(0, 128, 0, 1); line-height: 1.5 !important">执行查询返回一个唯一user对象的sql</span> User user = session.selectOne(statement, 1<span style="line-height: 1.5 !important">); System.out.println(user); }
}
执行结果如下:
可以看到,数据库中的记录已经成功查询出来了。