springboot整合mybatis-plus3.0-beta

声明

这只是一个基本springboot整合mybatis-plus3.0-beta的例子,包含springboot搭建、整合mybatis-plus3.0-beta,以及遇到的坑。

springboot

关于sprintboot的介绍太多了,用我的话概述就是快速构建项目,搭建常规SSM你可能需要30分钟及以上,用springboot你只要5分钟。

mybatis-plus作用

mybatis-plus3.0-beta可以提供通用dao层、通用service层、分页功能,并提供代码生成器。单表可以快速生成后台mapper、service、serviceImpl、空的controller类。

搭建springboot项目

我用的是eclispe,也没有装插件,所以通用这个网址生成springboot项目。springboot向导
这里写图片描述
点击Switch back to the simple version.
这里写图片描述
勾选web,然后下载该项目。
下载完成后解压,通过import-import-maven-exist maven projects导入
这里写图片描述
导入完成后目录结构如下:
这里写图片描述
创建目录,创建TestController,用于测试SpringMVC是否成功。

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
   

    @RequestMapping("test")
    public String test() {
        return "Hello springboot!";
    }
}

这里写图片描述

启动项目:打开ZyApplication.java类,右键Run as-java Application,出现Started ZyApplication in 3.508 seconds (JVM running for 3.964)表示成功。当然项目不一样名称就不一样,不必纠结。
打开浏览器:http://localhost:8080/test,成功访问。
这里写图片描述
这里你会发现和以往访问不一样,因为不用输入项目名,怎么使用http://ip:port/projectName方式访问呢?

带项目名访问

将application.properties修改成application.yml,当然你用properties也可以,这里我使用yml文件。内容如下:

server: 
    servlet: 
        context-path: /springboot

这里写图片描述
重启,再次访问:http://localhost:8080/springboot/test,访问成功。
这里写图片描述
那么问题来了,每次改重启好麻烦,能热部署吗?能。

热部署支持

打开pom.xml文件,添加热部署支持

<!-- 热部署支持. -->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <optional>true</optional>
 </dependency>

这里写图片描述
打开application.yml,添加热部署配置

spring:
    devtools: 
        restart: 
            enabled: true

这里写图片描述
再次重启,重启完你再修改java文件你会发现项目会自动部署而不用重启。

使其支持jsp

打开pom.xml,添加相关jar包
这里写图片描述
打开application.yml,添加jsp相关配置,并创建相关目录及test.jsp。
这里写图片描述
修改TestController为如下内容,使其跳到对应的test.jsp,并在test.jsp中取出test的值显示。

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
   

    @RequestMapping("test")
    public String test(Model model) {
        model.addAttribute("test", "jsp测试");
        return "contest/test";
    }
}

创建test.jsp,路径如下方图片所示。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
    ${test}
</body>
</html>

这里写图片描述
打开浏览器,访问http://localhost:8080/springboot/test,成功显示。(如果这里提示404,请手动重新启动即可)
这里写图片描述

加入mybatis

创建数据库springboot,执行下面的sql语句

DROP TABLE IF EXISTS `sys_admin`;

CREATE TABLE `sys_admin` (
  `id` varchar(64) NOT NULL COMMENT 'PK',
  `username` varchar(50) NOT NULL COMMENT '用户名',
  `password` varchar(64) NOT NULL COMMENT '密码',
  `status` int(1) NOT NULL COMMENT '状态',
  `salt` varchar(64) NOT NULL COMMENT '加密字符',
  `head_img` text COMMENT '头像',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `sys_admin` */

insert  into `sys_admin`(`id`,`username`,`password`,`status`,`salt`,`head_img`,`create_time`) values 
('1','root','root',1,'1',NULL,'2018-07-17 15:18:56');

打开pom.xml,添加mybatis和mysql的jar包,如下内容

<!-- springboot mybatis -->
<!-- 该包是mybatis为springboot提供的包 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>

<!-- mysql -->
<!-- mysql这个包不用写版本,springboot会自动查找 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- druid连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值