啥是框架?
按我的理解:框架的出现是为了让代码的编写更加的方便简洁,代码越到后面越少,框架负责把重复性的复杂的代码都省略掉,在编写代码时主要负责编写有变通性的代码。
Mybatis框架详解
要有一定的增删改查,操作数据库基础
Mybatis框架:
MyBatis专注于SQL本身,是一个足够灵活的DAO层解决方案,适用于性能要求较高或者需求多变的互联网项目,红色字体的是复制的;到后面和Spring框架联动后编写代码会更加方便。
mybatis配置思路
1.先整合mybatis的环境
2.创建包结构以及导入外部包
3.编辑mybatis配置文件
4.编写代码
5.测试
随便设计个数据库
Mysql数据库表,要有外键关系。下面设计一个表可以参考
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for grade -- ---------------------------- DROP TABLE IF EXISTS `grade`; CREATE TABLE `grade` ( `GradeID` int(0) NOT NULL AUTO_INCREMENT, `GradeName` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, PRIMARY KEY (`GradeID`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for student -- ---------------------------- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(0) NOT NULL AUTO_INCREMENT COMMENT '这是一个序号', `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '可以存储有限长度1~一万多', `bigname` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '家庭住址', `riqi` datetime(0) NULL DEFAULT NULL COMMENT '这是时间类型', `GradeID` int(0) NOT NULL COMMENT '这是外键', `age` int(0) NULL DEFAULT NULL COMMENT '年龄', PRIMARY KEY (`id`) USING BTREE, INDEX `fk_student_grade`(`GradeID`) USING BTREE, CONSTRAINT `fk_student_grade` FOREIGN KEY (`GradeID`) REFERENCES `grade` (`GradeID`) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB AUTO_INCREMENT = 21 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
创建idea中的maven项目
都别选直接下一步,你真想选我也没办法。
导入需要的依赖
<?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.xinxi2</groupId><!--这个每个人都不太一样别复制--> <artifactId>Student_Mybatis</artifactId><!--这个也是--> <version>1.0-SNAPSHOT</version><!--这个你猜是不是--> <dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!--mysql 数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
创建目录、bean层、dao层和一个测试类
先创建简单的层结构,主要让你学会Mybatis,能测试就行
配置Mybatis数据库连接
在resources里面创建mybatis.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="jdbc.properties"></properties><!--获取jdbc文件--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/><!--这四个都要和jdbc配置文件映射上--> <property name="url" value="${url}"/> <property name="username" value="${user}"/> <property name="password" value="${pwd}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/xinxi2/dao/StudentMapper.xml"/><!--要使用Mapper.xml都要有这一步--> <mapper resource="com/xinxi2/dao/GradeMapper.xml"/><!--有几个就写几个--> </mappers> </configuration>
创建bean层
Student类
package com.xinxi2.bean; import java.util.Date; public class Student { private int id; private String name; private String bigname; private Date riqi; private int gradeid; private int age; 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 getBigname() { return bigname; } public void setBigname(String bigname) { this.bigname = bigname; } public Date getRiqi() { return riqi; } public void setRiqi(Date riqi) { this.riqi = riqi; } public int getGradeid() { return gradeid; } public void setGradeid(int gradeid) { this.gradeid = gradeid; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Grade类
package com.xinxi2.bean; public class Grade { private int GradeID; private String GradeName; public int getGradeID() { return GradeID; } public void setGradeID(int gradeID) { GradeID = gradeID; } public String getGradeName() { return GradeName; } public void setGradeName(String gradeName) { GradeName = gradeName; } }