文章目录
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射
导包
我用的idea maven,直接在仓库导入
- 坐标
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version> 3.5.2</version>
</dependency>
- 方法
idea新建maven项目/模块-构建系统选择Maven-在pom.xml文件添加</dependencies>
,在这个标签中添加坐标-旁边跳出一个刷新符号,点击自动导入 - 补充
以后导包,坐标可以去maven仓库中查找,然后用这种方法导就可
配置文件
说明 主要配置了数据库连接信息 、注册xml
注意 & 用 &;替换
<?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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/girls?user=root&password=root&useUnicode=true&characterEncoding=gbk&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 每一个xml都需要在核心xml中注册-->
<mappers>
<mapper resource="dao/TestMapper.xml"/>
</mappers>
</configuration>
-
Mapper 替换以前的DAO的实现类
- resultType 是返回类型,目前只填泛型类型 也就是你的表所对应的类,返回值用LIst<泛型类型>
- id 写绑定接口中对应方法名
- #{xx} 代表 以前的填充符?
<?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 绑定接口-->
<mapper namespace="dao.TestMapper">
<select id="getTestById" resultType="obj.testObj">
select * from test where id = #{id}
</select>
</mapper>
idea默认的配置文件放在src目录下,如果放在子目录中,则需要加这样几行代码来顺利导出资源
注意 filtering
<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>
java文件
-
工具类
- SqlSession 类似 PrepareStatement+Connection,用完需要关闭
- 获取SqlSession 流程
xml-SqlSessionFactoryBuilder-sqlSessionFactory-SqlSession
private static SqlSessionFactory sqlSessionFactory = null;
// 通过xml获取 sqlSessionFactory
static {
try (InputStream is = Resources.getResourceAsStream("mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
使用
看起来在调用接口,因为xml已经绑定接口,所以会调用xml中的sql语句
TestMapper tm = ss.getMapper(TestMapper.class);
// 传入参数
List<testObj> list = tm.getTestById(1);
错误
被这个折磨了很久,类用. 文件用/
没有在核心xml注册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>