什么是MyBatis
- MyBatis 是一款优秀的持久层框架
- MyBatis 是一个半自动化的ORM框架(Object Relationship Mapping对象关系映射)
- MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的过程
- MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 实体类 【Plain Old Java Objects,普通的 Java对象】映射成数据库中的记录。
持久层
完成持久化工作的代码块,我们可以简单理解为DAO层(Data Access Object数据访问对象)
大多数情况下,数据持久化意味着将内存中的数据保存到磁盘上加以固化,这主要是通过关系型数据库来完成
MyBatis第一个程序
整体流程:
1.搭建MySQL数据库环境
2.导入MyBatis依赖
3.编写代码
4.测试
代码演示
1.搭建数据库环境
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,'王小二','123456'),(2,'张三','abcdef'),(3,'李四','987654');
2.导入MyBatis依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
3.编写MyBatis核心配置文件 "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=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/whb/dao/userMapper.xml"/>
</mappers>
</configuration>
4.创建实体类
public class User {
private int id; //id
private String name; //姓名
private String pwd; //密码
//构造,有参,无参
//set/get
//toString()
}
5.编写Mapper接口类
import com.kuang.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> selectUser();
}
6.编写Mapper.xml配置文件
- namespace 十分重要,他需要映射到对应的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="cn.whb.dao.UserMapper">
<select id="selectUser" resultType="cn.whb.pojo.User">
select * from user
</select>
</mapper>
7.编写测试类
public class MyTest {
@Test
public void selectUser() {
//获取配置文件的流对象,交给SqlSessionFactoryBuilder并解析
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//sqlSessionFactory--->相当于数据库连接池
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//相当于每一个数据库连接对象Connection
SqlSession session = sqlSessionFactory.openSession();
//方法一:
//List<User> users = session.selectList("cn.whb.mapper.UserMapper.selectUser");
//方法二:
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> users = mapper.selectUser();
for (User user: users){
System.out.println(user);
}
session.close();
}
}
8.问题说明
可能出现问题说明:Maven静态资源过滤问题,在pom.xml中添加如下:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>