1、介绍
Mybatis
Mybatis 是操作数据库的框架:提供一种Mapper类,支持用Java代码对数据库进行增删改查。缺点:需要先在
Mybatis Generator
自动为Mybatis生成简单的增删改查SQL语句的工具
Mybatis-Plus
MyBatis-Plus 是一个Mybatis 增强版工具,在 MyBatis 基础上扩充了功能(由国人团队苞米豆开发)。作用:为了简化开发效率。
Mybatis Plus Generator
比Mybatis Generator更加强大,支持功能更多,可自动生成:Entity、Mapper、Service、Controller等
2、Mybatis Plus Generator用法演示
2.1、工程创建与依赖配置
通过IDEA新建Spring工程,在工程的pom.
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--mp--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!--模板--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--mp代码生成器--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.2.0</version> </dependency> </dependencies>
2.2、数据库构建与项目配置
使用Navicat创建mysql数据库 "vueblog",创建数据表,SQL语句(自己懒得写,参考某大神的现成的SQL):
CREATE TABLE `m_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `username` varchar(64) DEFAULT NULL, `avatar` varchar(255) DEFAULT NULL, `email` varchar(64) DEFAULT NULL, `password` varchar(64) DEFAULT NULL, `status` int(5) NOT NULL, `created` datetime DEFAULT NULL, `last_login` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `UK_USERNAME` (`username`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `m_blog` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(20) NOT NULL, `title` varchar(255) NOT NULL, `description` varchar(255) NOT NULL, `content` longtext, `created` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP, `status` tinyint(4) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4;INSERT INTO `vueblog`.`m_user` (`id`, `username`, `avatar`, `email`, `password`, `status`, `created`, `last_login`) VALUES ('1', 'test', 'https://image-1300566513.cos.ap-guangzhou.myqcloud.com/upload/images/5a9f48118166308daba8b6da7e466aab.jpg', NULL, '96e79218965eb72c92a549dd5a330112', '0', '2020-04-20 10:44:01', NULL);
在工程的application.properties中,配置数据源datasource与mybatis-plus:
server.port=8080# DataSource Configspring.datasource.url=jdbc:mysql://localhost:3306/vueblog?useUnicode=true&useSSL=false&characterEncoding=utf8spring.datasource.username=rootspring.datasource.password=qixingchaospring.datasource.driver-class-name=com.mysql.jdbc.Drivermybatis-plus.mapper-locations=classpath*:/mapper/**Mapper.
2.3、导入、调整CodeGenerator源码
注意:数据源、包名信息一定要根据自己项目信息进行调整
package com.qxc;import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;import com.baomidou.mybatisplus.core.toolkit.StringPool;import com.baomidou.mybatisplus.core.toolkit.StringUtils;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.InjectionConfig;import com.baomidou.mybatisplus.generator.config.*;import com.baomidou.mybatisplus.generator.config.po.TableInfo;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class CodeGenerator { /** * <p> * 读取控制台内容 * </p> */ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!"); } public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("qixingchao"); gc.setOpen(false); gc.setServiceName("%sService"); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/vueblog?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("qixingchao"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName(null); pc.setParent("com.qxc"); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker String templatePath = "/templates/mapper.
2.4 执行CodeGenerator
右键CodeGenerator.java类 --> 选择"Run 'CodeGenerator.main()' "
IDEA底部弹出输入面板,手动输入数据库表名:
回车,自动生成基础代码:
2.5 代码测试
工程Application类增加自动扫描代码MapperScan:
package com.qxc;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan("com.qxc.mapper")public class VueblogApplication { public static void main(String[] args) { SpringApplication.run(VueblogApplication.class, args); }}
UserController类里写接口测试方法:
package com.qxc.controller;import com.qxc.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * <p> * 前端控制器 * </p> * * @author qixingchao * @since 2021-04-12 */@RestController@RequestMapping("/user")public class UserController { @Autowired UserService userService; @GetMapping("/{id}") public Object getUserById(@PathVariable("id")long id){ return userService.getById(id); }}
通过IDEA启动工程,在浏览器输入与接口匹配的URL地址:
原文转载:http://www.shaoqun.com/a/679737.html
旺店通:https://www.ikjzd.com/w/2390
贝贝母婴网:https://www.ikjzd.com/w/1321
1、介绍MybatisMybatis是操作数据库的框架:提供一种Mapper类,支持用Java代码对数据库进行增删改查。缺点:需要先在MybatisGenerator自动为Mybatis生成简单的增删改查SQL语句的工具Mybatis-PlusMyBatis-Plus是一个Mybatis增强版工具,在MyBatis基础上扩充了功能(由国人团队苞米豆开发)。作用:为了简化开发效率。MybatisPl
焦点科技:https://www.ikjzd.com/w/1831
net a porter:https://www.ikjzd.com/w/2132
飞书互动:https://www.ikjzd.com/w/1319.html
推荐另一款可以替代keepa 的插件,CamelCamelCamel插件?:https://www.ikjzd.com/tl/17564
作为卖家,我该如何拥抱快速变化的Wish政策?:https://www.ikjzd.com/home/11100
欧盟封闭边境30天,多地"封国"进入"战时状态",马士基、达飞等巨头怎么样了?:https://www.ikjzd.com/home/118440
没有评论:
发表评论