首页
技术小册
AIGC
面试刷题
技术文章
MAGENTO
云计算
视频课程
源码下载
PDF书籍
「涨薪秘籍」
登录
注册
第一章:注解开发介绍
第二章:环境准备
第三章:注解实现常用的 CRUD
第四章:注解实现一对一关联查询
第五章:注解实现一对多关联查询
第六章:注解实现多对多关联查询
第七章:分页插件 -- PageHelper
第八章:批量操作
第九章:自定义类型处理器
第十章:SQL 构建对象介绍
第十一章:准备工作
第十二章:使用 SQL 功能类构建 SQL 语句
第十三章:查询功能的实现
第十四章:新增功能的实现
第十五章:更新功能的实现
第十六章:删除功能的实现
当前位置:
首页>>
技术小册>>
Mybatis合辑5-注解、扩展、SQL构建
小册名称:Mybatis合辑5-注解、扩展、SQL构建
- sql 脚本: ``` -- ---------------------------- -- Table structure for account -- ---------------------------- DROP TABLE IF EXISTS `account`; CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NULL DEFAULT NULL COMMENT '外键', `money` double NULL DEFAULT NULL COMMENT '存款', PRIMARY KEY (`id`) USING BTREE, INDEX `fk_user_id`(`user_id`) USING BTREE, CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; ``` ``` -- ---------------------------- -- Records of account -- ---------------------------- INSERT INTO `account` VALUES (1, 1, 5000); INSERT INTO `account` VALUES (2, 2, 2000); INSERT INTO `account` VALUES (3, 3, 3000); ``` ``` -- ---------------------------- -- Table structure for course -- ---------------------------- DROP TABLE IF EXISTS `course`; CREATE TABLE `course` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '课程名称', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; ``` ``` -- ---------------------------- -- Records of course -- ---------------------------- INSERT INTO `course` VALUES (1, '语文'); INSERT INTO `course` VALUES (2, '数学'); INSERT INTO `course` VALUES (3, '英语'); ``` ``` -- ---------------------------- -- Table structure for student -- ---------------------------- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '学生姓名', `age` int(11) NULL DEFAULT NULL COMMENT '学生年龄', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; ``` ``` -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES (1, '张三', 20); INSERT INTO `student` VALUES (2, '李四', 25); ``` ``` -- ---------------------------- -- Table structure for student_course -- ---------------------------- DROP TABLE IF EXISTS `student_course`; CREATE TABLE `student_course` ( `sid` int(11) NULL DEFAULT NULL COMMENT '外键,指向学生表的主键', `cid` int(11) NULL DEFAULT NULL COMMENT '外键,指向课程表的主键', INDEX `fk_sid`(`sid`) USING BTREE, INDEX `fk_cid`(`cid`) USING BTREE, CONSTRAINT `fk_cid` FOREIGN KEY (`cid`) REFERENCES `course` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT `fk_sid` FOREIGN KEY (`sid`) REFERENCES `student` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; ``` ``` -- ---------------------------- -- Records of student_course -- ---------------------------- INSERT INTO `student_course` VALUES (1, 1); INSERT INTO `student_course` VALUES (1, 2); INSERT INTO `student_course` VALUES (1, 3); INSERT INTO `student_course` VALUES (2, 3); ``` ``` -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户名', `birthday` datetime(0) NULL DEFAULT NULL COMMENT '生日', `gender` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '性别', `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '地址', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; ``` ``` -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, '张三', '2021-11-02 10:43:06', '男', '江苏苏州'); INSERT INTO `user` VALUES (2, '李四', '2021-10-26 10:43:24', '女', '江苏南京'); INSERT INTO `user` VALUES (3, '王五', '2021-10-26 10:43:45', '男', '江苏徐州'); ``` - 导入相关 jar 包的 Maven 坐标: ``` <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> ``` - mybatis-config.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> <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://192.168.134.100:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <package name="com.github.mapper"/> </mappers> </configuration> ``` - log4j.xml ``` <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <param name="Encoding" value="UTF-8" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" /> </layout> </appender> <logger name="java.sql"> <level value="debug" /> </logger> <logger name="org.apache.ibatis"> <level value="info" /> </logger> <root> <level value="debug" /> <appender-ref ref="STDOUT" /> </root> </log4j:configuration> ```
上一篇:
第一章:注解开发介绍
下一篇:
第三章:注解实现常用的 CRUD
该分类下的相关小册推荐:
Mybatis合辑4-Mybatis缓存机制
Java必知必会-Maven初级
Java语言基础14-枚举和注解
Java语言基础11-Java中的泛型
Java语言基础6-面向对象高级
Java语言基础5-面向对象初级
Java语言基础12-网络编程
Java语言基础8-Java多线程
Java高并发秒杀入门与实战
Mybatis合辑2-Mybatis映射文件
Java必知必会-Maven高级
Java语言基础16-JDK8 新特性