使用idea+springboot+Mybatis搭建web项目

springboot的优势之一就是快速搭建项目,省去了自己导入jar包和配置xml的时间,使用非常方便。

1、创建项目project,然后选择Spring initializr,点击下一步 

2、按图示进行勾选,点击下一步,给项目起个名字,点击确定。

3、项目生成有,点击add as maven project,idea 会自动下载jar包,时间比较长 

4、项目生成后格式如下图所示: 
 
其中DemoApplication.java是项目主入口,通过run/debug configuration进行配置,就可运行,因为集成了tomcat,所以该项目只需启动一遍即可,不用每次修改代码后重启项目,但是修改代码后需重新编译下,新代码才会生效。 
 
5、生成的项目中,resources文件夹下,static文件夹下存放静态文件,比如css、js、html和图片等 
templates下存放html文件,controller默认访问该文件夹下的html文件。 
这个在application.properties配置文件中是可以修改的。 
6、在application.properties中配置数据库信息

spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = 1234
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#页面热加载
spring.thymeleaf.cache = false

创建一个controller类:helloworld

@Controller
@EnableAutoConfiguration
public class HelloWorld {
@Autowired
private IRegService regService;
@RequestMapping("/")
String home() {
return "index";
}
@RequestMapping("/reg")
@ResponseBody
Boolean reg(@RequestParam("loginPwd") String loginNum, @RequestParam("userId") String userId ){
String pwd = creatMD5(loginNum);
System.out.println(userId+":"+loginNum);
regService.regUser(userId,pwd);
return true;
}
private String creatMD5(String loginNum){
// 生成一个MD5加密计算摘要
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
md.update(loginNum.getBytes()); } catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return new BigInteger(1, md.digest()).toString(16);
}
}

user类:

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;
}
}

新建一个mapper接口

public interface UserMapper {
@Select("select * from users where userId = #{userId}")
User findUserByUserid(@Param("userId") String userId);
@Insert("insert into users (userId,pwd) values (#{userId},#{pwd})")
boolean insertUsers (@Param("userId") String userId,@Param("pwd") String pwd);
}

service接口及实现:

public interface IRegService {
boolean regUser(String uerId,String pwd);
}
@Service()
public class RegService implements IRegService {
@Autowired
private UserMapper userMapper;
@Override
public boolean regUser(String uerId, String pwd) { Boolean flag;
try {
flag = userMapper.insertUsers(uerId,pwd);
}catch (Exception e){
return false;
}
return flag;
}
}

最后在主类名上添加mapperscan包扫描:

@SpringBootApplication
@MapperScan("com.example.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

这是项目最后的包结构:

 
注意点:1、DemoApplication类跟controller包等平行 
2、@controller注解返回指定页面,本文中即返回index页面,也就是templates文件夹下的index.html 
3、如果需要返回json字符串、xml等,需要在有@controller类下相关的方法上加上注解@responsebody 
4、@restcontroller注解的功能等同于@controller和@responsebody 
有问题请留言,一起讨论。 
5、springboot默认缓存templates下的文件,如果html页面修改后,看不到修改的效果,设置spring.thymeleaf.cache = false即可

转自:http://blog.csdn.net/alantuling_jt/article/details/54893383

使用idea+springboot+Mybatis搭建web项目的更多相关文章

  1. idea+springboot+Mybatis搭建web项目

    使用idea+springboot+Mybatis搭建一个简单的web项目. 首先新建一个项目: 在这里选择Maven项目也可以,但是IDEA为我们提供了一种更方便快捷的创建方法,即Spring In ...

  2. springboot+mybatis搭建web项目

    使用idea+springboot+Mybatis搭建一个简单的web项目. 首先新建一个项目: 在这里选择Maven项目也可以,但是IDEA为我们提供了一种更方便快捷的创建方法,即Spring In ...

  3. springboot +mybatis 搭建完整项目

    springboot + mybatis搭建完整项目 1.springboot整合mybatis注解版 转:https://blog.csdn.net/u013187139/article/detai ...

  4. Spring-Boot快速搭建web项目详细总结

    最近在学习Spring Boot 相关的技术,刚接触就有种相见恨晚的感觉,因为用spring boot进行项目的搭建是在太方便了,我们往往只需要很简单的几步,便可完成一个spring MVC项目的搭建 ...

  5. IDEA + SpringBoot + Java搭建Web项目

    打开IDEA,选择Spring Initializr,默认配置,点击Next  添写GAV.(group.Artifact.Version)  选择Spring Boot版本,这里选2.1.4稳定 ...

  6. springboot+mybatis 非web项目构建

    https://blog.csdn.net/wlittlefive/article/details/86157134 https://blog.csdn.net/ththcc/article/deta ...

  7. SpringBoot+Mybatis多模块项目搭建教程

    一.前言 框架为SpringBoot+Mybatis,本篇主要记录了在IDEA中搭建SpringBoot多模块项目的过程. 1.开发工具及系统环境 IDE:IntelliJ IDEA 2018.2 系 ...

  8. springBoot 搭建web项目(前后端分离,附项目源代码地址)

    springBoot 搭建web项目(前后端分离,附项目源代码地址)   概述 该项目包含springBoot-example-ui 和 springBoot-example,分别为前端与后端,前后端 ...

  9. Spring Boot搭建Web项目常用功能

    搭建WEB项目过程中,哪些点需要注意: 1.技术选型: 前端:freemarker.vue 后端:spring boot.spring mvc 2.如何包装返回统一结构结果数据? 首先要弄清楚为什么要 ...

随机推荐

  1. 10个财务工作中常用的 Excel 万能公式

    1.多条件判断公式 =IF(AND(条件1,条件2...条件n),同时满足条件返回的值,不满足条件返回的值) =IF(OR(条件1,条件2...条件n),同时满足任一条件返回的值,不满足条件返回的值) ...

  2. NLS_CHARACTERSET和NLS_NCHAR_CHARACTERSET

    The NLS_CHARACTERSET is used for CHAR, VARCHAR2, LONG and CLOB columns;The NLS_NCHAR_CHARACTERSET is ...

  3. net core体系-API-1Ocelot-(1)概要

    从这篇开始探讨Ocelot,Ocelot是一个.NET API网关,仅适用于.NET Core,用于.NET面向微服务/服务的架构中.当客户端(web站点.ios. app 等)访问web api时, ...

  4. eclipse启动web应用 报错

    错误:The origin server did not find a current representation for the target resource or is not willing ...

  5. 五种UML工具

    ProcessOn是一款在线流程图软件(https://www.processon.com/),能够高效的完成在线流程图.UI原型图.UML以及组织图,网络拓扑图的构建.该软件最大的特点是,无需安装, ...

  6. js读取cookie,并利用encrypt和decrypt加密和解密方法

    以下为支持encrypt和decrypt加密和解密方法 eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a ...

  7. box-shadow阴影 三面显示

    想弄个只显示三面的阴影效果,网上一搜没有解决根本问题,最后还是在css3演示里面找到方法http://www.css88.com/tool/css3Preview/Box-Shadow.html 我把 ...

  8. position的定义

    position的属性有哪些 static :静态属性,默认布局 absolute:绝对定位:除了static之外的以其他属性定位,除此之外相对于body, fixed:固定定位:脱离文档流,滚动条不 ...

  9. APP开发,微信第三方登录的介绍

    去年做了一阵APP相关的开发,经常遇到第三方登陆的需求,比如微信.微博.fb的第三方登陆等等,其实主要的流程都大同小异,这里就以微信为例来介绍,希望对大家有帮助. 微信开放平台(open.weixin ...

  10. Android视图动画集合AndoridViewAnimations

    Android视图动画集合AndoridViewAnimations Android视图动画是针对视图对象的动画效果,包括对象平移.旋转.缩放和渐变四种类型.通过组合这四种类型,可以创建出无数种动画效 ...