mybatis
概述
- MyBatis 是一款优秀的持久层框架,用于简化 JDBC 开发
案例
利用MyBatis框架,从MySQL中查询所有的用户
- 查询user表中所有数据
- 创建user表,添加数据
- 创建模块,导入坐标
- 编写用户实体类(User)
- 编写 MyBatis 核心配置文件 复制资料里面的mybatis-config.xml到resources
- 创建UserMapper接口
- 编写 SQL 映射文件 复制资料里面的UserMapper.xml到resources
- 加载核心配置文件,获取 SqlSessionFactory 对象
- 获取 SqlSession 对象,执行 SQL 语句
- 释放资源
<?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>com.GDKM</groupId>
<artifactId>mybatis-day01-01-helloword</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--MyBatis核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!--MyBatis日志配置-->
<!-- 添加slf4j日志api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.20</version>
</dependency>
<!-- 添加logback-classic依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 添加logback-core依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>
import java.sql.Date;
public class User {
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
构造方法 set get toString方法
}
<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--配置所有别名-->
<typeAliases>
<!--typeAlias标签: 给一个实体类配置别名 type属性:包名.类名 alias属性:别名-->
<!--<typeAlias type="com.gdkm.pojo.User" alias="User"/>-->
<!--package标签:包扫描,扫描包中所有类取别名,别名就是类名
name属性:包名-->
<package name="com.gdkm.pojo"/>
</typeAliases>
<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/day18?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--<mapper resource="UserMapper.xml"/>-->
<!--package标签: 包扫描, 扫描包里面的接口和映射文件
前提1: 接口名要和映射文件名字相同
前提2: 编译后接口和映射文件要在同一个包中-->
<package name="com.gdkm.mapper"/>
</mappers>
</configuration>
public interface UserMapper {
List<User>findAllUsers();
}
<?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="com.gdkm.mapper.UserMapper">
<select id="findAllUsers" resultType="User">
SELECT * FROM USER;
</select>
</mapper>
public class TestMybatis {
@Test
public void testHello() throws IOException {
String resource = "mybatis-config.xml";
//把文件变成流 import org.apache.ibatis.io.Resources
InputStream inputStream = Resources.getResourceAsStream(resource);
//解析xml文件 sqlSessionFactory相当于数据库连接池
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
//sqlSessionFactory.openSession()理解为从连接池中获取连接
//sqlSession 表示数据库连接
SqlSession sqlSession = sqlSessionFactory.openSession();
//给UserMapper接口生成一个实现类(代理对象)
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//调用方法,执行对应的SQL语句,把结果封装成对象
List<User> userList = mapper.findAllUsers();
userList.forEach(System.out::println);
sqlSession.close();
}
}