一,MyBatis介绍
MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录
二,MyBatis优点
不用再去写复杂的JDBC代码
- 简单易学,不依赖第三方的程序或者框架。
- 官网文档强大,开源,我们可以随时分析源码’;
- 实现解耦,低耦合,高内聚
- ORM;对象关系映射
- 提供XML标签;
三, Mybatis程序
1,搭建实验环境,创建一个数据库
1-1,MySQL代码
CREATE DATABASE `mybatis` ;
USE mybatis
;
DROP TABLE IF EXISTS user
;
CREATE TABLE user
(
id
INT(20) NOT NULL,
name
VARCHAR(30) DEFAULT NULL,
pwd
VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO user
(id
,name
,pwd
)
VALUES (1,‘闪电侠‘,‘123‘),(2,‘蜘蛛侠‘,‘abc‘),(3,‘钢铁侠‘,‘520‘);
1-2,运行结果
2,创建一个普通的Maven项目
2-1,创建Maven
2-2,编写pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>com.Shandx<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>ssm-mybatis-study<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>1.0-SNAPSHOT<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"><<span class="hljs-name">dependencies</span>></span>
<span class="hljs-comment"><!--单元测试--></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>junit<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>junit<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>4.11<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-comment"><!--mybatis的包--></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.mybatis<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>mybatis<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>3.5.1<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-comment"><!--连接数据库的驱动包--></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>mysql<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>mysql-connector-java<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>5.1.47<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependencies</span>></span>
<span class="hljs-tag"><<span class="hljs-name">build</span>></span>
<span class="hljs-comment"><!--希望maven在导出项目的时候,能够将我们的配置及资源导出--></span>
<span class="hljs-tag"><<span class="hljs-name">resources</span>></span>
<span class="hljs-tag"><<span class="hljs-name">resource</span>></span>
<span class="hljs-tag"><<span class="hljs-name">directory</span>></span>src/main/java<span class="hljs-tag"></<span class="hljs-name">directory</span>></span>
<span class="hljs-tag"><<span class="hljs-name">includes</span>></span>
<span class="hljs-tag"><<span class="hljs-name">include</span>></span>**/*.properties<span class="hljs-tag"></<span class="hljs-name">include</span>></span>
<span class="hljs-tag"><<span class="hljs-name">include</span>></span>**/*.xml<span class="hljs-tag"></<span class="hljs-name">include</span>></span>
<span class="hljs-tag"></<span class="hljs-name">includes</span>></span>
<span class="hljs-tag"><<span class="hljs-name">filtering</span>></span>false<span class="hljs-tag"></<span class="hljs-name">filtering</span>></span>
<span class="hljs-tag"></<span class="hljs-name">resource</span>></span>
<span class="hljs-tag"><<span class="hljs-name">resource</span>></span>
<span class="hljs-tag"><<span class="hljs-name">directory</span>></span>src/main/resources<span class="hljs-tag"></<span class="hljs-name">directory</span>></span>
<span class="hljs-tag"><<span class="hljs-name">includes</span>></span>
<span class="hljs-tag"><<span class="hljs-name">include</span>></span>**/*.properties<span class="hljs-tag"></<span class="hljs-name">include</span>></span>
<span class="hljs-tag"><<span class="hljs-name">include</span>></span>**/*.xml<span class="hljs-tag"></<span class="hljs-name">include</span>></span>
<span class="hljs-tag"></<span class="hljs-name">includes</span>></span>
<span class="hljs-tag"><<span class="hljs-name">filtering</span>></span>false<span class="hljs-tag"></<span class="hljs-name">filtering</span>></span>
<span class="hljs-tag"></<span class="hljs-name">resource</span>></span>
<span class="hljs-tag"></<span class="hljs-name">resources</span>></span>
<span class="hljs-tag"></<span class="hljs-name">build</span>></span>
</project>
3,编写代码
3-1,创建pojo实体类
public class User {
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> id;
<span class="hljs-keyword">private</span> String name;
<span class="hljs-keyword">private</span> String pwd;
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">User</span><span class="hljs-params">()</span> </span>{
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">User</span><span class="hljs-params">(<span class="hljs-keyword">int</span> id, String name, String pwd)</span> </span>{
<span class="hljs-keyword">this</span>.id = id;
<span class="hljs-keyword">this</span>.name = name;
<span class="hljs-keyword">this</span>.pwd = pwd;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getId</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> id;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setId</span><span class="hljs-params">(<span class="hljs-keyword">int</span> id)</span> </span>{
<span class="hljs-keyword">this</span>.id = id;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> name;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span><span class="hljs-params">(String name)</span> </span>{
<span class="hljs-keyword">this</span>.name = name;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getPwd</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> pwd;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPwd</span><span class="hljs-params">(String password)</span> </span>{
<span class="hljs-keyword">this</span>.pwd = password;
}
<span class="hljs-meta">@Override</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-string">"User{"</span> +
<span class="hljs-string">"id="</span> + id +
<span class="hljs-string">", name=‘"</span> + name + <span class="hljs-string">‘\‘‘</span> +
<span class="hljs-string">", pwd=‘"</span> +pwd + <span class="hljs-string">‘\‘‘</span> +
<span class="hljs-string">‘}‘</span>;
}
}
3-2,在resources下建一个mybatis-config.xml,编写mybatis配置文件
<?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>
<!--配置环境,这里可以有多套环境 default代表默认的是那一套-->
<environments default="development">
<!--配置一套环境 id .环境的名字-->
<environment id="development">
<!--transactionManager:事务管理,type:jdbc-->
<transactionManager type="JDBC"/>
<!--dataSource 数据源-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--xml中不允许&符号直接出现,我们需要使用 & 代替-->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&charsetEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
3-3,创建工具类
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
//mybatis的工具类,重复的代码的提纯
public class MyBatisUtils {
<span class="hljs-comment">//类变量不需要设置默认值;</span>
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">SqlSessionFactory</span> sqlSessionFactory;
<span class="hljs-keyword">static</span> {
<span class="hljs-comment">//在maven中,所有的资源文件一般都放在resources目录下,我们可以直接拿到。</span>
<span class="hljs-keyword">try</span> {
<span class="hljs-type">String</span> resource = <span class="hljs-string">"mybatis-config.xml"</span>;
<span class="hljs-type">InputStream</span> inputStream = <span class="hljs-type">Resources</span>.getResourceAsStream(resource);
sqlSessionFactory = new <span class="hljs-type">SqlSessionFactoryBuilder</span>().build(inputStream);
} <span class="hljs-keyword">catch</span> (<span class="hljs-type">IOException</span> e) {
e.printStackTrace();
}
}
<span class="hljs-comment">//设置SqlSessionFactory公共的方法</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">SqlSessionFactory</span> getSqlSessionFactory(){
<span class="hljs-keyword">return</span> sqlSessionFactory;
}
}
3-4,映射文件的接口
import com.shandx.pojo.User;
import java.util.List;
public interface UserMapper {
<span class="hljs-comment">//获取全部的用户</span>
<span class="hljs-function">List<User> <span class="hljs-title">selectUser</span><span class="hljs-params">()</span></span>;
}
3-5,编写对应的mapper映射文件
<?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接口的类-->
<mapper namespace="com.Shandx.dao.UserMapper">
<!--select标签的id对应映射接口的方法名字 resultType:返回结果的类型 中间就编写sql语句-->
<select id="selectUser" resultType="com.Shandx.pojo.User">
select * from user
</select>
</mapper>
3-6,在mybatis配置文件中绑定编写映射文件
<?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>
<!--配置环境,这里可以有多套环境 default代表默认的是那一套-->
<environments default="development">
<!--配置一套环境 id .环境的名字-->
<environment id="development">
<!--transactionManager:事务管理,type:jdbc-->
<transactionManager type="JDBC"/>
<!--dataSource 数据源-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--xml中不允许&符号直接出现,我们需要使用 & 代替-->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&charsetEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<span class="hljs-comment"><!--关联映射文件--></span>
<span class="hljs-tag"><<span class="hljs-name">mappers</span>></span>
<span class="hljs-tag"><<span class="hljs-name">mapper</span> <span class="hljs-attr">resource</span>=<span class="hljs-string">"com/kuang/dao/userMapper.xml"</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">mappers</span>></span>
</configuration>
4,测试一下
在maven的test文件夹下编写对应的测试类
import com.Shandx.dao.UserMapper;
import com.Shandx.pojo.User;
import com.Shandx.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
@Test
public void selectUser(){
<span class="hljs-comment">//1.拿到sqlSessionFactory对象</span>
<span class="hljs-type">SqlSessionFactory</span> sqlSessionFactory = <span class="hljs-type">MyBatisUtils</span>.getSqlSessionFactory();
<span class="hljs-comment">//2.通过sqlSessionFactory对象openSession()创建一个sqlSession。</span>
<span class="hljs-type">SqlSession</span> sqlSession = sqlSessionFactory.openSession();
<span class="hljs-comment">//3.通过sqlSession获得mapper对象 , 参数为映射文件对应的接口类的class对象</span>
<span class="hljs-type">UserMapper</span> mapper = sqlSession.getMapper(<span class="hljs-type">UserMapper</span>.<span class="hljs-keyword">class</span>);
<span class="hljs-comment">//4.通过mapper对象来执行操作;</span>
<span class="hljs-type">List</span><<span class="hljs-type">User</span>> users = mapper.selectUser();
<span class="hljs-comment">//获得结果集</span>
<span class="hljs-keyword">for</span> (<span class="hljs-type">User</span> user : users) {
<span class="hljs-type">System</span>.out.<span class="hljs-built_in">println</span>(user);
}
}
}