官网地址:https://mybatis.org/mybatis-3/zh/getting-started.html
使用案例:
我们创建一个数据库叫做mybatis
,在其下创建userinfo
数据表,然后创建相应的字段,对应的数据库字段如下:
然后,我们开始使用xml
方式来使用mybatis
。
1. 添加依赖
<!--MySql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!--Mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--Junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
同时,配置下过滤条件:
<!--设置资源过滤-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2. 新建mybatis配置文件
在resources
文件夹下新建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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 关联xml配置的文件 -->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
3. 新建基础类等
新建实体类UserInfo
,如:
public class UserInfo {
private String username;
private int userid;
private String passwd;
然后生成对应的get
和set
以及构造函数等。
数据持久化接口UserDao
:
package com.weizu.dao;
import com.weizu.pojo.UserInfo;
import java.util.List;
public interface UserDao {
List<UserInfo> getAllUsers();
}
在resources
下新建mapper/UserMapper.xml
,该文件中配置如下:
<?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.weizu.dao.UserDao">
<select id="getAllUsers" resultType="com.weizu.pojo.UserInfo">
select * from userinfo
</select>
</mapper>
获取SqlSession
的工具类库:
package com.weizu.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{
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
然后,测试:
public class Test {
@org.junit.Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<UserInfo> allUsers = mapper.getAllUsers();
for(UserInfo userInfo: allUsers){
System.out.println(userInfo);
}
}
}
测试结果:
在数据库中也只有这一条数据。
此时,我们可以发现,在数据持久化的Dao
层,我们没有使用实现该接口,而是直接使用mybatis
来进行实现,简化了操作的流程。
https://www.bilibili.com/video/BV1NE411Q7Nx?p=2&spm_id_from=pageDriver