MyBatis的开发步骤
- 搭建环境、导包
- 编写MyBatis核心配置文件(configuration.xml)
- 编写代码
- 测试
搭建环境
-
创建一个普通的maven项目,删除src目录(将此项目当成父工程)
-
版本控制
<properties>
<mybatis.version>3.5.7</mybatis.version>
<junit.version>4.12</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
3.新建module,导入maven
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
编写核心配置文件(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.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///person?useUnicode=true&characterEncoding=utf8&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
编写代码
-
实体类
-
Dao接口
public interface PersonDao {
// 获取所有人员信息
List<Person> findAll();
}
-接口实现类
<?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:包名要跟Dao/Mapper接口的包名一致 -->
<mapper namespace="cn.blb.mybatis01.dao.PersonDao">
<!--
id:对应接口中的方法
resultType:sql语句执行的返回值
-->
<select id="findAll" resultType="cn.blb.mybatis01.pojo.Person">
select * from person
</select>
</mapper>
-编写测试类
SqlSessionFactory sqlSessionFactory;
@Before
public void testBefore() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testFindAll() {
// 这种写法不用关闭sqlSession
try(SqlSession sqlSession = sqlSessionFactory.openSession()) {
PersonDao personDao = sqlSession.getMapper(PersonDao.class);
List<Person> list = personDao.findAll();
list.forEach(System.out::println);
};
}
可能出现的异常
1. BindingException
-
异常:
org.apache.ibatis.binding.BindingException: Type interface cn.blb.mybatis01.dao.PersonDao is not known to the MapperRegistry
-
问题: 核心配置文件中注册mappers
-
解决方案:在配置文件中注册mapper
<mappers>
<mapper resource="cn/blb/mybatis01/dao/mapper/PersonMapper.xml"/>
</mappers>
2.BuilderException
-
异常:
org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource cn/blb/mybatis01/dao/PersonMapper.xml
-
问题:src/main/java中找不到PersonMapper.xml
-
解决方案:在pom中配置
<build>
<!--
资源插件 告诉maven把 src/main/java目录中的 指定扩展名的文件 拷贝到 target/classes目录中。
-->
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes>
<!--包括目录下的.properties,.xml 文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<!-- filtering 选项 false 不启用过滤器, *.property 已经起到过
滤的作用了 -->
<filtering>false</filtering>
</resource>
</resources>
</build>