一, 编写接口
package com.ceshiren.dao;
import com.ceshiren.dao.entity.TestUser;
import java.io.IOException;
import java.util.List;
public interface IUserDao {
public List<TestUser> findAll() throws IOException;
}
二, 编写实现类
package com.ceshiren.dao;
import com.ceshiren.dao.entity.TestUser;
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;
import java.util.List;
public class IUserDaoImpl implements IUserDao{
@Override
public List<TestUser> findAll() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
List<Object> objects = sqlSession.selectList("user.findAll", TestUser.class);
objects.forEach(System.out::println);
sqlSession.close();
return null;
}
}
三, 编写 jdbc.properties
jdbc.driver=om.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://110.40.250.165:3307/turorials?characterEncoding=UTF-8&useUnicode=true&auseSSL=false&serverTimezone=GMT
jdbc.username=root
jdbc.password=root
四, 编写核心配置文件
<?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>
<!--配置连接数据库的环境-->
<!-- 加载外部的配置文件-->
<properties resource="jdbc.properties"/>
<!-- 设置别名-->
<typeAliases>
<!-- 单个实体类的别名-->
<!-- <typeAlias type="com.ceshiren.dao.entity.TestUser" alias="user1"/>-->
<!-- 多个实体类的情况下,用包名+类名的方式,推荐这种方式-->
<package name="com.ceshiren.dao.entity"/>
</typeAliases>
<!-- default - 默认环境配置-->
<environments default="development">
<environment id="development">
<!-- transactionManager - 事务管理器-->
<transactionManager type="JDBC"/>
<!-- dataSource - 数据库的连接池 POOLED - 表示使用mybatis的连接池; UNPOOLED - 不用mybatis的连接池,每次都用新的连接 -->
<dataSource type="POOLED">
<!-- 配置数据库连接属性-->
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- mapper映射关系引入-->
<mappers>
<mapper resource="TestUserMapping.xml"/>
</mappers>
</configuration>
五, 编写mapperXML
<?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">
<!-- namespace:命名空间,与sql标签的id属性共同构成唯一标识-->
<mapper namespace="user">
<!-- resultType:返回结果类型,完成自动的映射封装,value为对应封装的实体类全路径(包名+类名)-->
<!-- 起别名的情况下,resultType用类名命名,不区分大小写-->
<select id ="findAll" resultType="TestUser"> <!--这里建立映射关系-->
SELECT * FROM test_user
</select>
</mapper>
六, 测试
package com.ceshiren.dao;
import com.ceshiren.dao.entity.TestUser;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class IUserDaoImplTest {
@Test
void findAll() throws IOException {
IUserDao userDao = new IUserDaoImpl();
List<TestUser> all = userDao.findAll();
all.forEach(System.out::println);
}
}