Mybatis
官网:https://mybatis.org/mybatis-3/zh/index.html
创建项目流程:
1、创建maven空白的项目
2、添加pom.xml依赖代码
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
3、编写mybatis-config.xml系统的核心设置文件
官网有 每一个Mapper.xml都需要在mybatis核心配置文件中注册!
<?xml version="1.0" encoding="UTF8" ?>
<!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://localhost:3306/mybatis?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.xml都需要在mybatis核心配置文件中注册!-->
<mappers>
<mapper resource="com/lm/dao/UserMapper.xml"/>
</mappers>
</configuration>
4、编写一个工具类用于获取SqlSessionFactory
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用Mybatis获取SqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
5、创建UserDao接口类
public interface UserDao {
List<User> getUserList();
}
6、创建一个UserMapper.xml
在namespace绑定UserDao接口类 com.lm.dao.UserDap
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace = 绑定一个对于的dao、mapper接口-->
<mapper namespace="com.lm.dao.UserDao">
<select id="getUserList" resultType="com.lm.pojo.User">
select * from mybatis.user
</select>
</mapper>
7、编写测试类
public class UserDaoTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> userList = mapper.getUserList();
for (User user :userList) {
System.out.println(user.toString());
}
}
}
问题解决:
1、mybatis的xml中文注释异常
- 根据网上的方法,将xml第一行的UTF-8改为UTF8
2、org.apache.ibatis.binding.BindingException: Type interface com.lm.dao.UserDao is not known to the MapperRegistry.
-
Maven没把xml编译到输出目录中
-
解决:pom.xml添加
-
<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>