使用Mybatis首先要先引入相关依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
然后在XML中配置 SqlSessionFactory
每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先配置的 Configuration 实例来构建出 SqlSessionFactory 实例。
//XML 配置文件中包含了对 MyBatis 系统的核心设置,
//包括获取数据库连接实例的数据源(DataSource)
//以及决定事务作用域和控制方式的事务管理器(TransactionManager)。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
添加一个工具类
获取sqlSessionFactory对象(用工厂模式来创建sqlSession)
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
public SqlSession getSqlSession(){
//SqlSession session = sqlSessionFactory.openSession());
//return session;
return sqlsessionFactory.openSession();
}
接着编写实体类以及接口和实现类(这里mybaits将实现类改成Mapper配置文件)
下面是相关配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<!--返回类型resultType = "路径.类名.权限命名(泛型里面的)" 返回集合resultMap -->
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
测试代码
@Testpublic yoid test(){
//第一步:获利Sqlsession对象
Sqlsession sqlsession = Mybatisutils.getsqlsession();
//方式:getMapper
UserDao userDao = sqlsession.getMapper(UserDao.class):
List<User>userList=userDao.getUserList();
for(User user :userList){
System.out.printin(user);
}
关闭sqlsession
sqlsessionsqlsession.close();
}
测试可能遇到的问题
1.org.apache.ibatis.binding.BindingException: Type interface com.kuang.dao.UserDao is not known to the MapperRegistry.
<!--每一个Mapper.XML都需要在Mybatis核心配置文件中注册!-->
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
2.java.lang.ExceptionInInitializerError和报出相关的配置文件不存在
因为Maven约定大于配置,我们写的配置文件,无法被导出或者生效的问题(资源过滤问题target生成目录里面没有需要在Maven中加入下面代码)
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<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>