1、Mybatis概述
Mybatis基于java的
持久层
框架,内部封装了JDBC,开发者只需要关注SQL语句
- 特点
1、sql语句在xml里,便于统一管理和优化
2、解除sql与程序代码耦合:通过DAO层,将业务逻辑和数据访问逻辑分离
提供映射标签,
2、 第一个Mybatis项目
Mybatis3中文文档
- 2.1 搭建环境
【1】搭建数据库
【2】新建Maven项目,导入Mybatis依赖【pom.xml文件】
<!-- mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!-- 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
- 2.2 创建一个模块Module
<modules>
<module>mybatis_01</module>
</modules>
- 2.3 mybatis-config.xml 核心配置文件
1、XML 配置文件中包含了对 MyBatis 系统的核心设置,包括获取数据库连接实例的数据源(DataSource)以及决定事务作用域和控制方式的事务管理器(TransactionManager)
2、在resources
目录下创建mybatis-config.xml
<?xml version="1.0" encoding="utf-8" ?>
<!--Mybatis核心配置-->
<!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"></transactionManager>
<!-- 数据库连接-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/springboot"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 加载sql语句-->
<mappers>
<mapper resource="com/dm/dao/EmployeeMapper.xml"/>
</mappers>
</configuration>
- 2.4 将工程目录按照层划分
pojo层是实体类
public class Employee {
//id,name,age,habbies,address
}
dao层是Mapper接口类
【1】EmployeeDao
// mapper接口
public interface EmployeeDao {
List<Employee> getEmployees();
【2】EmployeeMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace绑定一个对应的dao/mapper接口-->
<mapper namespace="com.dm.dao.EmployeeDao">
<select id="getEmployees" resultType="com.dm.pojo.Employee">
select * from employees;
</select>
</mapper>
utils层是工具类
public class MyBatisUtils {
private static SqlSessionFactory sqlSessionFactory;
//使用mybatis第一步,获取sqlSessionFactory对象
static {
try{
String resource = "mybatis-config.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}catch (IOException e) {
throw new RuntimeException(e);
}
}
// 获取sqlSession实例
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
记得将EmployeeMapper.xml注册到mybatis-config.xml文件中!!!
mybatis-config.xml 文件中
<!--每一个Mapper.XML都需要在Mybatis核心配置文件中注册!-->
<mappers>
<mapper resource="com/daimalu/dao/UserMapper.xml"/>
</mappers>
2.5 测试类
public class TestEmploy {
// 获取sqlSession对象
SqlSession sqlSession;
@Test
public void test(){
sqlSession = MyBatisUtils.getSqlSession();
EmployeeDao employmapper = sqlSession.getMapper(EmployeeDao.class);
List<Employee> employList = employmapper.getEmployees();
// employee 对象
for (Employee employee:employList){
System.out.println(employee.getAddress());
}
}
}
问题集锦
1、Cannot find class: com.mysql.cj.jdbc.Driver
2、Cause: java.io.IOException: Could not find resource com/dm/dao/EmployeeMappe
3、java不支持源选项5,请使用7或更高