准备:
需要提前安装好以下工具软件
(1) IDEA 2021
(2)Java 1.8
(3)数据库 MySQL 5.7 (SQLyog 或 Navicat)
(4)SQL yog 或 Navicat (用于操作MySQL数据库)
步骤:
1 在 MySQL 中创建数据库 mybatisdemo,编码为 utf8
2 新建表:
USE mybatisdemo CREATE TABLE users( uid INT PRIMARY KEY AUTO_INCREMENT, uname VARCHAR(20) NOT NULL, uage INT NOT NULL ); INSERT INTO users(uid,uname,uage) VALUES(NULL,'张##',25),(NULL,'李##',23),(NULL,'王##',23);
3 IDEA 创建 maven工程, 点击 File 菜单 -- New 菜单-- Project
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mybatisdemo</groupId> <artifactId>mybatis_demo01</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <!-- <scope>test</scope>--> <scope>compile</scope> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build> </project>
10 最终的文件结构:
11 在 UserTest.java 的 userFindByIdTest 上右键,Run :
源代码:
链接:https://pan.baidu.com/s/1LAIYzav5X30oTzIjxMhElA
提取码:1234
===============================================
补充:
mapper里面的namespace?
参考:
https://www.cnblogs.com/emanlee/p/15869468.html
session.selectOne("findById", 1);
参考:
如果多个类(接口)中有同名的 findById 方法怎么办?
===============================================
REF
https://www.cnblogs.com/benjieqiang/p/11183580.html
https://blog.csdn.net/qq_32166627/article/details/70741729
https://mybatis.org/mybatis-3/zh/getting-started.html
https://www.w3cschool.cn/mybatis/mybatis-dyr53b5w.html
黑马程序员书籍