<version>4.12</version>
</dependency>
</dependencies>
> [](
)**三、创建mybatis核心配置文件:**
> -------------------------------------------------------------------------------------
**在src/main/resources下创建**:mybatis-config.xml核心配置文件
[![image](https://img-blog.csdnimg.cn/img_convert/7f600e6c565024a6c89e0ac008681ba5.png)](
)
**官方伪代码:**
[领取资料](
)
<?xml version="1.0" encoding="UTF-8" ?>
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>//加群1025684353一起吹水聊天
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
**配置文件代码:**
<?xml version="1.0" encoding="UTF-8" ?>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">//加群1025684353一起吹水聊天
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
> [](
)**四、编写mybatis工具类**
> ---------------------------------------------------------------------------------
[领取资料](
)
package com.kuang.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
//sqlSessionFactory --> sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
try {//加群1025684353一起吹水聊天
//使用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 命令所需的所有方法。
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
> [](
)**五、编写实体类**
> --------------------------------------------------------------------------
>
> **字段与字段类型要与数据库一一对应**
package com.study.pojo;
//实体类
public class User {
private int id;
private String name;//加群1025684353一起吹水聊天
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
[领取资料](
)
> [](
)**六、编写dao接口**
> ----------------------------------------------------------------------------
package com.study.dao;
public interface UserMapper {
List<User> getUserList();
}
> [](
)**七、在UserMapper接口类同包下编写Mapper文件实现接口**
> ----------------------------------------------------------------------------------------------------
<?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接口-->
//加群1025684353一起吹水聊天
<!--id="方法名" resultType="类型"-->
select * from mybatis.user
> [](
)**八、编写完毕,需要在mybatis-config.xml中注册Mapper.xml**
> ------------------------------------------------------------------------------------------------------------
<mapper resource="com/study/dao/UserMapper.xml"/>
</mappers>
[领取资料](
)
**如:**
[![image](https://img-blog.csdnimg.cn/img_convert/5785017b48d73ec2297505cfa27dee1d.png)](
)
**注意:检查文件目录结构是否正确**
[![image](https://img-blog.csdnimg.cn/img_convert/164867f2fd095784a8931a606ba20b02.png)](
)
> [](https://gitee.com/vip204888/java-p7)**九、测试**
> -----------------------------------------------------------------------
**在test/java/com/study/dao下创建UserMapperTest.java测试类**
**如图:**
[![image](https://img-blog.csdnimg.cn/img_convert/df043909b144dd309c1aaf7df0019e9d.png)](https://jq.qq.com/?_wv=1027&k=ak26GrpX)
[领取资料](https://jq.qq.com/?_wv=1027&k=ak26GrpX)
**编写测试代码:**
public class UserMapperTest {
@Test
public void test(){
//第一步:获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//方式一:getMapper
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭SqlSession
sqlSession.close();//加群1025684353一起吹水聊天
}
**运行结果**
[![image](https://img-blog.csdnimg.cn/img_convert/224d2a366c970fc2e4f53ab2507863ce.png)](https://jq.qq.com/?_wv=1027&k=ak26GrpX)
**你们可以能会遇到的问题:**
1. 配置文件没有注册
2. 绑定接口错误。
3. 方法名不对
4. 返回类型不对
5. Maven导出资源问题 #### **特别注意:mybatis配置文件中不能出现中文注释!**
> [](https://gitee.com/vip204888/java-p7)**资源导出问题解决:在pom.xml中添加**
> ---------------------------------------------------------------------------------------
> 建议每次创建项目都提前添加资源过滤配置
### 最后
小编精心为大家准备了一手资料
![](https://img-blog.csdnimg.cn/img_convert/8ca1c3fd8c770e967d61a4474728bfbf.png)
![](https://img-blog.csdnimg.cn/img_convert/ac02e61e7ed71938aacb74e9f737ca98.png)
以上Java高级架构资料、源码、笔记、视频。Dubbo、Redis、设计模式、Netty、zookeeper、Spring cloud、分布式、高并发等架构技术
**[CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】](https://codechina.csdn.net/m0_60958482/java-p7)**
【附】架构书籍
1. BAT面试的20道高频数据库问题解析
2. Java面试宝典
3. Netty实战
4. 算法
![](https://img-blog.csdnimg.cn/img_convert/0bac29cb3d84501fca87c4e188de63c7.png)
**BATJ面试要点及Java架构师进阶资料**
![](https://img-blog.csdnimg.cn/img_convert/ca386f1b775f24a9f37f95fbef21d397.png)
--------------------
> 建议每次创建项目都提前添加资源过滤配置
### 最后
小编精心为大家准备了一手资料
[外链图片转存中...(img-TlYeFr31-1630680180580)]
[外链图片转存中...(img-kqQRK6t3-1630680180582)]
以上Java高级架构资料、源码、笔记、视频。Dubbo、Redis、设计模式、Netty、zookeeper、Spring cloud、分布式、高并发等架构技术
**[CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】](https://codechina.csdn.net/m0_60958482/java-p7)**
【附】架构书籍
1. BAT面试的20道高频数据库问题解析
2. Java面试宝典
3. Netty实战
4. 算法
[外链图片转存中...(img-EqnPcpg9-1630680180583)]
**BATJ面试要点及Java架构师进阶资料**
[外链图片转存中...(img-Zr32UMnp-1630680180585)]
|