Mybatis-01
Mybatis
1. 简介
1、什么是Mybatis?
Mybatis 是一款优秀的持久层框架,它支持自定义sql、存储过程以及高级映射。(它类似于JDBC,但较JDBC方便)
(持久层:将业务数据存储到磁盘,也具备长期存储能力,只要磁盘不损坏,如果在断电情况下,重启系统仍然可以读取数据)
2、Mybatis的特点
-
-
灵活
-
解除sql与程序代码的耦合
-
提供映射标签,支持对象与数据库的orm字段关系映射
-
提供对象关系映射标签,支持对象关系组建维护
-
2.第一个Mybatis程序
在搭建第一个Mybatis程序前,我们先做一些准备工作
- 首先我们需要了解并学习它的文档:https://mybatis.org/mybatis-3/zh/index.html
- 接着我们需要添加它的Maven依赖
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>x.x.x</version> </dependency>
3. 最后配置一个属于Mybatis的配置文件
<!-- 一个标准的框架 --> <?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="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource=""/> </mappers> </configuration>
接下来,我们开始编写第一个Mybatis程序(为方便,我们过滤了service层)
1、添加maven依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <!-- lombok包 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency> </dependencies>
2、配置Mybatis,添加数据库信息
<?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.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///customs?useSSL=true&useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="1234"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/charles/dao/UserMapper.xml"/> </mappers> </configuration>
3、添加数据库中的实体类
package com.charles.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @AllArgsConstructor @NoArgsConstructor @Data public class UserInfo { private int id; private String username; private String password; private String perms; }
4、编写dao接口
package com.charles.dao; import com.charles.pojo.UserInfo; import java.util.List; public interface UserMapper { // 查询所有用户 List<UserInfo> queryAll(); }
5、 编写SQL语句
<?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.charles.dao.UserMapper">
<!-- id是namespace中的方法名 resultType是SQL语句执行的返回值 --> <select id="queryAll" resultType="com.charles.pojo.UserInfo"> select * from userinfo; </select> </mapper>
6、要编写一个Utils来获取Mybatis的工厂
package com.charles.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;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession(){
return sqlSessionFactory.openSession();
}
}
7、 测试
package com.charles.dao; import com.charles.pojo.UserInfo; import com.charles.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserInfoDaoTest { @Test public void test(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<UserInfo> userInfos = mapper.queryAll(); for (UserInfo user : userInfos){ System.out.println(user); } // 使用后需要及时关闭,跟JDBC差不多. session.close(); } }
结果展示:与数据库一致