IDEA部署Mybatis框架:
1.第一步
2.选择maven,点击next
3.项目名
4. 删除父项目的Src
5.新建一个子项目
6.点击父项目的pro.xml文件 ,进行配置mysql驱动,mybatis驱动,junit
7.在父项目的pro.xml中加上下面图片中的代码,防止子项目的xml导不出
8.在子项目的pro.xml中也加上,上面图片中的代码
<!-- 在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>
</project>
9.在子项目的resources下面创建一个mybatis的xml
10.在mybatis—config.xml中配置MySQL相关配置
<!-- configuration核心配置文件 -->
<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/mybatis?useUnicode=true&characterEncoding=UTF-8&userSSL=true&serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 每个mapper.xml都需要在Mybatis核心配置文件中注册-->
<mappers>
<mapper resource="com/item/dao/UserInfoMapper.xml"/>//注意:这是你的Mapper层的接口中的xml文件,根据需求写,不要copy
</mappers>
</configuration>
11.在自己的utils层建一个mybatisutils的实体类,并进行加载mybatis-config.xml的配置文件
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();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
// 你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
12.在mapper层新建一个mapper对应的xml文件,进行操作数据库
如若不懂:可以去看视频教学 视频地址:https://www.bilibili.com/video/BV1NE411Q7Nx?p=2 如若对您有帮助,请点赞
【狂神说Java】Mybatis最新完整教程IDEA版通俗易懂