一、什么是MyBatis
MyBatis官网:mybatis – MyBatis 3 | 入门
-
MyBatis是一款优秀的持久层框架
-
支持自定义 SQL、存储过程以及高级映射
-
MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
1.1maven仓库
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
1.2持久化
数据持久化
-
持久化就是将程序的数据在持久状态和瞬时状态转化的过程
-
内存:断电即失
-
数据库(jdbc),io文件持久化
1.3、持久层
Dao层、service层、Controller层
-
完成持久化工作的代码块
-
曾界限十分明显
二、思路
思路:搭建环境-->导入MyBatis-->编写代码-->测试
2.1搭建环境
搭建数据库
以MySQL新建数据库mybatis -->创建表user -->填入id name pwd
2.2打开idea创建新项目
- 2.2打开idea创建新项目
-
创建工程名 :Mabatis-Study,下面的Location自动生成
- 删除src
- 导入父工程,导入依赖:
<?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>
<!--父工程-->
<groupId>org.example</groupId>
<artifactId>Mybatis-Study</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 导入依赖 -->
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
-
可能会出现如下问题,刷新一下就好了
-
点击src->main->resources,创建file 命名为mybatis-config.xml
这样就可以找到url的地址
-
核心配置文件:
<?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代表核心配置文件-->
<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?userSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.xml都需要在Mybaties核心配置文件中注册-->
<mappers>
<mapper resource="com/mybatis/dao/UserMapper.xml"/>
</mappers>
</configuration>
-
编写mybatis工具类
-
建立dao包和util包,在util包中键MybatisUtils
package com.mybatis.util;
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;
//SqlSessionFactory -->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
//获取 第一步:SqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。//
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。jdbc connection
public static SqlSession getSqlSession(){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
2.3、编写代码
实体类
package com.mybatis.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.mybatis.dao;
import com.mybatis.pojo.User;
import java.util.List;
public interface UserDao {
List<User> getUserList();
}
接口实现类 有原来的UserDaoImpl转变成一个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.mybatis.dao.UserDao">
<!-- 查询语句-->
<select id="getUserList" resultType="com.mybatis.pojo.User" >
select * from mybatis.user;
</select>
</mapper>
2.4、测试
-
junit测试:
package com.mybatis.dao;
import com.mybatis.pojo.User;
import com.mybatis.util.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 userDao=sqlSession.getMapper(UserDao.class);
List<User> userList=userDao.getUserList();
for (User user:userList){
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
}
注意点
若出现在这个错误,找到pom.xml,将这一行代码输入进去
<!--在bulid中配置resources,来防止我们资源到处失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
就运行成功了
运行该程序所需要以下七步