【Spring Boot&&Spring Cloud系列】使用Intellij构建Spring Boot和Mybatis项目
一、创建项目
1、File->New->Project->spring initializer

2、勾选Web SQL Template Engines



3、项目生成之后,点击add as maven project 这时候会自动下载jar包
4、项目生成的目录结构

其中DemoApplication.java是项目主入口,编辑run/debug configuration即可运行
5、在生成的项目中新建自己需要的包
controller
entity
mapper
service
util
resources下的static文件夹下新建
css
fonts
img
js
resources下templates下新建需要的html文件
6、修改application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/dev
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#页面热加载
spring.thymeleaf.cache=false
二、代码编写
HelloWorld.controller.java
package com.slp.controller; import com.slp.entity.User;
import com.slp.service.IRegService;
import com.slp.util.GenerateKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; /**
* Created by sangliping on 2017/8/15.
* @Controller注解返回指定页面,也就是template文件夹下的页面
* @RestController相当于@Controller和@ResponseBody
* springboot默认会缓存templates下的文件,如果html页面修改后,看不到修改后的效果则修改热部署为false
*/
@Controller
@EnableAutoConfiguration
public class HelloWorldController {
@Autowired
private IRegService regService;
@RequestMapping("/")
String home(){
return "index";
} @RequestMapping("/reg")
@ResponseBody
Boolean reg(@RequestParam("loginPwd")String loginNum,@RequestParam("userId")String userId){
System.out.println("进入注册页面");
User user = regService.findUserById(userId);//根据用户id查询用户
if(StringUtils.isEmpty(user)){//如果用户不存在则进行注册
String pwd = this.createMD5(loginNum);
System.out.println(userId+"==="+loginNum);
String id = GenerateKeyUtil.getCharAndNumr(20);
regService.regUser(userId,pwd,id);
}
return true;
} private String createMD5(String loginNum){
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
md.update(loginNum.getBytes());
}catch (NoSuchAlgorithmException e){
e.printStackTrace();
}
return new BigInteger(1,md.digest()).toString();
} @RequestMapping("/register")
private String register(){
System.out.println("进入注册页面"); return "login";
}
}
User.java
package com.slp.entity; /**
* Created by sangliping on 2017/8/15.
*/
public class User {
private String id;
private String userId;
private String pwd; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getUserId() {
return userId;
} public void setUserId(String userId) {
this.userId = userId;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} @Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", userId='" + userId + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
UserMapper.java
package com.slp.mapper; import com.slp.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; /**
* Created by sangliping on 2017/8/15.
*/
@Mapper
public interface UserMapper {
@Select("select * from users where userId = #{userId}")
User findUserByUserId(@Param("userId") String userId); @Insert("insert into users(id,userId,pwd) values(#{id},#{userId},#{pwd})")
boolean insertUsers (@Param("userId")String userId,@Param("pwd")String pwd,@Param("id")String id);
}
IRegService.java
package com.slp.service; import com.slp.entity.User; /**
* Created by sangliping on 2017/8/15.
*/
public interface IRegService {
boolean regUser(String userId,String pwd,String id);
User findUserById(String userId);
}
RegService.java
package com.slp.service; import com.slp.entity.User;
import com.slp.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* Created by sangliping on 2017/8/15.
*/
@Service()
public class RegService implements IRegService{
@Autowired
private UserMapper userMapper; @Override
public boolean regUser(String userId, String pwd,String id) {
boolean flag = userMapper.insertUsers(userId,pwd,id);
return flag;
} @Override
public User findUserById(String userId) {
User user = userMapper.findUserByUserId(userId);
return user;
}
}
GenerateKeyUtil.java
package com.slp.util; import java.util.Random; /**
* Created by sangliping on 2017/8/15.
*/
public class GenerateKeyUtil {
public static String getCharAndNumr(int length) {
String val = "";
Random random = new Random();
for (int i = 0; i < length; i++) {
// 输出字母还是数字
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// 字符串
if ("char".equalsIgnoreCase(charOrNum)) {
// 取得大写字母还是小写字母
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (choice + random.nextInt(26));
} else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
}
页面index.html
<!DOCTYPE html>
<html xmlns="http://www.thymeleaf.org">
<head> <meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>登陆页面</title>
<!-- CSS -->
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:400,100,300,500"/>
<link rel="stylesheet" href="/css/bootstrap.min.css"/>
<link rel="stylesheet" href="/css/font-awesome.min.css"/>
<link rel="stylesheet" href="/css/form-elements.css"/>
<link rel="stylesheet" href="/css/style.css"/> <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]--> <!-- Favicon and touch icons -->
<link rel="shortcut icon" href="/img/favicon.png"/>
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/img/apple-touch-icon-144-precomposed.png"/>
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/img/apple-touch-icon-114-precomposed.png"/>
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/img/apple-touch-icon-72-precomposed.png"/>
<link rel="apple-touch-icon-precomposed" href="/img/apple-touch-icon-57-precomposed.png"/> </head> <body> <!-- Top content -->
<div class="top-content"> <div class="inner-bg">
<div class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-2 text">
<h3>没有账户请先<a href="http://localhost:8080/register">注册</a> </h3>
<div class="description">
<p> </p>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-sm-offset-3 form-box">
<div class="form-top">
<div class="form-top-left">
<h3>登 陆</h3>
<p>输入用户名和密码:</p>
</div>
<div class="form-top-right">
<i class="fa fa-key"></i>
</div>
</div>
<div class="form-bottom">
<form role="form" action="" method="post" class="login-form">
<div class="form-group">
<label class="sr-only" for="form-username">用户名</label>
<input type="text" name="form-username" placeholder="请输入用户名" class="form-username form-control" id="form-username"/>
</div>
<div class="form-group">
<label class="sr-only" for="form-password">密 码</label>
<input type="password" name="form-password" placeholder="请输入密码" class="form-password form-control" id="form-password"/>
</div>
<button type="submit" class="btn">登 陆!</button>
</form>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-sm-offset-3 social-login">
<h3>或者使用第三方账户登陆:</h3>
<div class="social-login-buttons">
<a class="btn btn-link-1 btn-link-1-facebook" href="#">
<i class="fa fa-facebook"></i> Facebook
</a>
<a class="btn btn-link-1 btn-link-1-twitter" href="#">
<i class="fa fa-twitter"></i> Twitter
</a>
<a class="btn btn-link-1 btn-link-1-google-plus" href="#">
<i class="fa fa-google-plus"></i> Google Plus
</a>
</div>
</div>
</div>
</div>
</div> </div> <!-- Javascript -->
<script src="/js/jquery-1.11.1.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/jquery.backstretch.min.js"></script>
<script src="/js/scripts.js"></script> <!--[if lt IE 10]>
<script src="/js/placeholder.js"></script>
<![endif]--> </body>
</html>
启动预览

到此为止使用Intellij创建Spring boot+mybatis的简单的项目就完成了
表结构:

【Spring Boot&&Spring Cloud系列】使用Intellij构建Spring Boot和Mybatis项目的更多相关文章
- Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...
- Spring实战第五章学习笔记————构建Spring Web应用程序
Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...
- spring boot系列01--快速构建spring boot项目
最近的项目用spring boot 框架 借此学习了一下 这里做一下总结记录 非常便利的一个框架 它的优缺点我就不在这背书了 想了解的可以自行度娘谷歌 说一下要写什么吧 其实还真不是很清楚,只是想记录 ...
- 通过IntelliJ IDEA创建maven+springmvc+mybatis项目
第一个springmvc+mybatis项目,通过学习极客学院视频(视频案例通过eclipse搭建,网址为http://www.jikexueyuan.com/course/1430.html),发现 ...
- 【Spring Cloud 系列】 二、Spring Cloud Eureka 的第一印象
Eureka : 翻译翻译,找到了!(惊讶语气) Spring CLoud 中的 Spring Cloud Eureka,用于 分布式项目中的服务治理.是对Netflix 套件中的Eureka 的二次 ...
- Spring Boot 2.0系列文章(七):SpringApplication 深入探索
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...
- SpringCloud核心教程 | 第一篇: 使用Intellij中的Spring Initializr来快速构建Spring Cloud工程
spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...
- SpringCloud核心教程 | 第二篇: 使用Intellij中的maven来快速构建Spring Cloud工程
spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...
- spring boot / cloud (三) 集成springfox-swagger2构建在线API文档
spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...
随机推荐
- physical------Collider 组件参考
Collider 组件参考 点击 属性检查器 下面的 添加组件 按钮,然后从 添加碰撞组件 中选择需要的 Collider 组件,即可添加 Collider组件到节点上. Collider 组件属性 ...
- 【设计和开发一套简单自己主动化UI框架】
!有兴趣的朋友请直接移步Github,本帖子已经不做更新,框架的详细的实现已经做了优化和代码整理,本文仅仅介绍了详细的设计思路! 目标:编写一个简单通用UI框架用于管理页面和完毕导航跳转 终于的实现效 ...
- c# mvc 获取 HtmlHelper 表达式值和时间格式化 去边框
/// <summary> /// 返回没有边框的只读的TextBox标签 /// </summary> /// <typeparam name="TModel ...
- ASP.NET MVC Castle Windsor 教程
一.[转]ASP.NET MVC中使用Castle Windsor 二.[转]Castle Windsor之组件注册 平常用Inject比较多,今天接触到了Castle Windsor.本篇就来体验其 ...
- C# listview控件右击导出数据到txt文本
private void 导出成功点击ToolStripMenuItem_Click(object sender, EventArgs e) { ) { MessageBox.Show("列 ...
- php + crontab 执行定时任务
1.yii2中的console <?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yi ...
- java使用javax.mail进行免费的邮件发送
1. 建议发送方使用阿里云邮箱https://mail.aliyun.com/,阿里云默认是开启个人邮箱pop3.smtp协议的,所以无需在阿里云邮箱里设置,pop3.smtp的密码默认邮箱登录密码, ...
- 关于form表单onsubmi提交
表单允许客户端的用户以标准格式向服务器提交数据.表单的创建者为了收集所需数据,使用了各种控件设计表单如 INPUT 或 SELECT.查看表单的用户只需填充数据并单击提交按钮即可向服务器发送数据.服务 ...
- cocos2d - 翻转两个Sprite
用两个图片初始化两个CCSprite,一样的大小,重叠在一起,分别叫做 foregroundNode 和 backgroundNode . - (void)flipover { if (isFlipi ...
- 如何换网页IP代理
如何换网页IP代理 | 浏览:21 | 更新:2014-08-31 13:46 1 2 3 4 5 6 7 分步阅读 如何设置网页IP代理的步骤:现在就看下图如何换网页IP和清理浏览器cookie,[ ...