Git007

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【MyBatis学习】01、第一个mybatis程序

思路: 搭建环境-->导入MyBatis-->编写代码-->测试

一、搭建环境

1.创建数据库 (mysql)

复制代码
1 CREATE DATABASE `mybatis`;
2 
3 USE `mybatis`;
4 
5 CREATE TABLE `user`(
6  `id` INT(20) NOT NULL PRIMARY KEY,
7  `name` VARCHAR(30) DEFAULT NULL,
8  `pwd` VARCHAR(20) DEFAULT NULL
9 )ENGINE=INNODB DEFAULT CHARSET=utf8;
复制代码

2.新建项目

  (1) 新建一个普通的maven项目

  (2) 导入maven依赖 (mysql-connector-java、mybatis、junit)

二、创建一个模块

     (基于mybatis官方文档)

  • 编写mybatis的核心配置文件 (src/main/resources/mybatis-config.xml) 

 

复制代码
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <!--核心配置-->
 6 <configuration>
 7     <environments default="development">
 8         <environment id="development">
 9             <transactionManager type="JDBC"/>
10             <dataSource type="POOLED">
11                 <property name="driver" value="com.mysql.jdbc.Driver"/>
12                 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
13                 <property name="username" value="root"/>
14                 <property name="password" value="123456"/>
15             </dataSource>
16         </environment>
17     </environments>
18 </configuration>
复制代码
  • 编写mybatis工具类

复制代码
 1 public class MybatisUtils {
 2     private static SqlSessionFactory sqlSessionFactory;
 3     static{
 4         try {
 5             //使用Mybatis获取sqlSessionFactory对象
 6             String resource = "mybatis-config.xml";
 7             InputStream inputStream = Resources.getResourceAsStream(resource);
 8             sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
 9         } catch (IOException e) {
10             e.printStackTrace();
11         }
12     }
13 
14     //既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
15     // SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
16     // 可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
17     public static SqlSession getSqlSession(){
18         return sqlSessionFactory.openSession();
19     }
20 }
复制代码

二、编写代码

  • 实体类

  User类(private、constructor、setter&&getter、tostring)

  • Dao接口

1 public interface UserMapper {
2     List<User> getUserList();
3 }
  • 接口实现Mapper配置文件

复制代码
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <!-- namespace绑定一个对应的UserMapper接口-->
 6 <mapper namespace="com.wcl.dao.UserMapper">
 7     <!--select查询语句-->
 8     <!--id 相当于重写的方法-->
 9     <!--resultType 为方法返回类型的泛型-->
10     <select id="getUserList" resultType="com.wcl.pojo.User">
11         select * from mybatis.user;
12     </select>
13 </mapper>
复制代码

三、测试

可能出现的错误

(1)BindingException: Type interface com.wcl.dao.UserMapper is not known to the MapperRegistry.

  原因:未在Mybatis配置文件中注册Mapper.xml

1 <!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册-->
2     <mappers>
3         <mapper resource="com/wcl/dao/UserMapper.xml"/>
4     </mappers>

 

(2)Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. 
Cause: java.io.IOException: Could not find resource com/wcl/dao/UserMapper.xml
 原因:Maven默认资源文件配置在resources目录下,但我们写的xml放在java目录下,此目录下导出不了,需要手动导出 
复制代码
 1  <build>
 2         <resources>
 3             <resource>
 4                 <directory>src/main/java</directory>
 5                 <includes>
 6                     <include>**/*.xml</include>
 7                     <include>**/*.properties</include>
 8                 </includes>
 9                 <filtering>true</filtering>
10             </resource>
11 
12             <resource>
13                 <directory>src/main/resources</directory>
14                 <includes>
15                     <include>**/*.xml</include>
16                     <include>**/*.properties</include>
17                 </includes>
18                 <filtering>true</filtering>
19             </resource>
20         </resources>
21     </build>
复制代码

(3)Cause: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 5; 1 字节的 UTF-8 序列的字节 1 无效

mybatis的xml文件的

 

1 <?xml version="1.0" encoding="UTF-8" ?>

 

改为

1 <?xml version="1.0" encoding="UTF8" ?> 

 

Cause: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 5; 1 字节的 UTF-8 序列的字节 1 无效

posted on   cczzhh007  阅读(23)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示