Mybatis基本开发步骤
1、创建数据库
2、导入相关依赖jar包
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
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="jdbc:mysql://localhost:3306/library?serverTimeZone=UTC"/>
<property name="url" value="com.mysql.cj.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
</configuration>
4、创建实体类
public class Admin {
private Integer id;
private String username;
private String password;
private Integer type;
}
5、编写Mapper接口类
public interface AdminMapper {
List<Admin> selectAdminAll();
}
6、编写Mapper.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="cn.jyu.dao.AdminMapper">
<select id="selectAdminAll" resultType="cn.jyu.po.Admin">
select * from admin
</select>
</mapper>
namespace对应着Mapper接口的路径
id对应着改接口的方法名,必须一致。
resultType对应着该方法的返回类型。
7、在核心配置文件中配置Mapper.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="jdbc:mysql://localhost:3306/library?serverTimeZone=UTC"/>
<property name="url" value="com.mysql.cj.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/jyu/dao/Mapper/AdminMapper.xml"/>
</mappers>
</configuration>
8、编写测试类
@Test
public void test() throws IOException {
//加载核心配置文件信息,获取流对象
InputStream inputStream = Resources.getResourceAsStream("MybatisConfig.xml");
//根据核心配置文件信息,使用SqlSessionFactoryBuilder初始化一个SqlSessionFactory对象,用于生产SqlSession
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//从SqlSessionFactory中获取一个SqlSession对象
SqlSession sqlSession = sessionFactory.openSession();
//获取对应的Mapper接口
AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
//调用方法
List<Admin> admins = mapper.selectAdminAll();
for (Admin t: admins) {
System.out.println(t);
}
}
错误记录,文件名错误:
MybatisConfig文件忘记添加后缀.xml,找错找了半天。
除了自己粗心之外,也要吐槽一下idea,后缀都没有,前面的图标却还是xml的。
排错方法:
查看target下的classes文件,看配置文件是否加载进来