思路:搭建环境–>导入MyBatis–>编写代码–>测试。
整体结构
第一步:搭建环境
创建数据库
create database MyBatis;
use MyBatis;
create table user(
id int(20) not null,
name varchar(30) default null,
pwd varchar(30) default null,
primary key (id)
)engine=InnoDB default char set=utf8;
show tables;
insert into user(id,name,pwd) values
(1,'张三','123456'),
(2,'李斯','123456'),
(3,'狂吴','123456')
新建项目
1、新建一个普通的maven项目
2、导入依赖
<!-- 导入依赖 -->
<dependencies>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- MyBatis驱动 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
创建一个模块
具体可在官网文档中进行查看
- 编写MyBatis的核心配置文件。在resources下新建一个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>
<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/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="xiao123"/>
</dataSource>
</environment>
</environments>
<!--每个Mapper.xml都需要在MyBatis核心配置文件中注册!-->
<mappers>
<mapper resource="com/xiao/dao/UserDao/UserMapper.xml"/>
</mappers>
</configuration>
-
连接数据库
-
编写MyBatis工具类
package com.xiao.utils;
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 java.io.IOException;
import java.io.InputStream;
//该工具类用于将资源加载进来并创建一个可执行的SQL的对象
//sqlSessionFactory是用来构造sqlSession的
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
//第一步:获取sqlSessionFactory对象
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。
public static SqlSession getSqlSession(){
//SqlSession sqlSession = sqlSessionFactory.openSession();
//return sqlSession;
return sqlSessionFactory.openSession();
}
}
编写代码
- 实体类
package com.xiao.pojo;
//实体类
public class user {
private int id;
private String name;
private String pwd;
public user() {
}
public user(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "user{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
- dao接口
package com.xiao.dao;
import com.xiao.pojo.user;
import java.util.List;
public interface UserDao {
List<user> getUserList();
}
- 接口实现类由JDBC的UserDaoImp转变为一个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">
<!--namespace相当于绑定一个Dao(mapper)接口-->
<mapper namespace="com.xiao.dao.UserDao">
<select id="getUserList" resultType="com.xiao.pojo.user">
select * from mybatis.user;
</select>
</mapper>
测试
- junit测试
package com.xiao.dao;
import com.xiao.pojo.user;
import com.xiao.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
//第一步:获得sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//执行sql
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<user> userList = mapper.getUserList();
for (user user : userList) {
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
}
会遇到的错误:
org.apache.ibatis.binding.BindingException: Type interface com.xiao.dao.UserDao is not known to the MapperRegistry.
解决:在MyBatis配置文件中添加注册
<mappers>
<mapper resource="com/xiao/dao/UserDao/UserMapper.xml"/>
</mappers>
java.lang.ExceptionInInitializerError
解决:在pom.xml文件中添加如下代码
<!--在build中配置resources,来防止资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>