【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文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...
随机推荐
- Python MySQL(MySQLdb)
From: http://www.yiibai.com/python/python_mysql.html Python标准的数据库接口的Python DB-API(包括Python操作MySQL).大 ...
- CentOS 7 打开关闭FirewallD防火墙端口命令
CentOS 7 使用firewalld代替了原来的iptables,使用方法如下: >>>关闭防火墙 systemctl stop firewalld.service ...
- Docker命令之 exec
docker exec :在运行的容器中执行命令 docker exec [OPTIONS] CONTAINER COMMAND [ARG...] OPTIONS说明: -d :分离模式: 在后台运行 ...
- 转 linux 权限
发布系统架构图简化如下: 管理员通过Jenkins调用“发布程序(代号varian,以下简称varian)”,发布程序会进行一系列的初始化操作,完成后生成Docker镜像上传到Docker仓库,容器集 ...
- Android中利用C++处理Bitmap对象
相信有些Android&图像算法开发者和我一样,遇到过这样的状况:要对Bitmap对象做一些密集计算(例如逐像素的滤波),但是在java层写循环代码来逐像素操作明显是不现实的,因为Java代码 ...
- 利用neon技术对矩阵旋转进行加速(2)
上次介绍的是顺时针旋转90度,最近用到了180度和270度,在这里记录一下. 1.利用neon技术将矩阵顺时针旋转180度: 顺时针旋转180度比顺时针旋转90度容易很多,如下图 A1 A2 A3 A ...
- asp.net网页中添加年月日时分秒星期。
html代码如下: 现在是<span id="TimeSpan"></span> <script type="text/javascript ...
- Hibernate的七种映射关系之基本映射
说到关系,在这个世界无处不在,我们必须以某个关系的节点存在在这个世界网中.比如父子关系,师生关系,上下属关系甚至是危险关系.数据也是一样的,它的存在必为某其他节点做准备. Hibernate有七种映射 ...
- Visual Code 调用Chrome 浏览HTML
Code 使用快捷键:Ctrl+Shit+B 然后再Task.json,替换以下: { "version": "0.1.0", "command&qu ...
- mysql压缩版的安装教程
1. 首先创建 my.ini,在mysql解压目录下的bin文件夹中新建一个名为 my.ini 的文件,内容为 [client] port=3306 default-character-set ...