使用idea+springboot+Mybatis搭建web项目
使用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项目的更多相关文章
- idea+springboot+Mybatis搭建web项目
使用idea+springboot+Mybatis搭建一个简单的web项目. 首先新建一个项目: 在这里选择Maven项目也可以,但是IDEA为我们提供了一种更方便快捷的创建方法,即Spring In ...
- springboot+mybatis搭建web项目
使用idea+springboot+Mybatis搭建一个简单的web项目. 首先新建一个项目: 在这里选择Maven项目也可以,但是IDEA为我们提供了一种更方便快捷的创建方法,即Spring In ...
- springboot +mybatis 搭建完整项目
springboot + mybatis搭建完整项目 1.springboot整合mybatis注解版 转:https://blog.csdn.net/u013187139/article/detai ...
- Spring-Boot快速搭建web项目详细总结
最近在学习Spring Boot 相关的技术,刚接触就有种相见恨晚的感觉,因为用spring boot进行项目的搭建是在太方便了,我们往往只需要很简单的几步,便可完成一个spring MVC项目的搭建 ...
- IDEA + SpringBoot + Java搭建Web项目
打开IDEA,选择Spring Initializr,默认配置,点击Next  添写GAV.(group.Artifact.Version)  选择Spring Boot版本,这里选2.1.4稳定 ...
- springboot+mybatis 非web项目构建
https://blog.csdn.net/wlittlefive/article/details/86157134 https://blog.csdn.net/ththcc/article/deta ...
- SpringBoot+Mybatis多模块项目搭建教程
一.前言 框架为SpringBoot+Mybatis,本篇主要记录了在IDEA中搭建SpringBoot多模块项目的过程. 1.开发工具及系统环境 IDE:IntelliJ IDEA 2018.2 系 ...
- springBoot 搭建web项目(前后端分离,附项目源代码地址)
springBoot 搭建web项目(前后端分离,附项目源代码地址) 概述 该项目包含springBoot-example-ui 和 springBoot-example,分别为前端与后端,前后端 ...
- Spring Boot搭建Web项目常用功能
搭建WEB项目过程中,哪些点需要注意: 1.技术选型: 前端:freemarker.vue 后端:spring boot.spring mvc 2.如何包装返回统一结构结果数据? 首先要弄清楚为什么要 ...
随机推荐
- [转]xshell使用技巧
https://yq.aliyun.com/articles/44721 xshell是我用过的最好用的ssh客户端工具,没有之一.这个软件完全免费,简单易用,可以满足通过ssh管理linux vps ...
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL
SelectNodes,SelectSingleNode总是返回NULL 原文地址:http://www.cnblogs.com/linlf03/archive/2011/11/30/2268705. ...
- 元素滚动到底部或顶部时阻止body滚动
移动端的弹窗内容有滚动条,滚动到底部或顶部时或影响弹窗下的body滚动,某些浏览器滚动到顶部时不松手就触发了刷新页面的情况,如果不需要这样的默认体验,就需要加一下判断了. var startX,sta ...
- php json_encode转换中文乱码
$arr = ["a"=>'范德萨似懂非懂']; echo json_encode($arr,JSON_UNESCAPED_UNICODE);
- 020100——00002_OS库
OS 库中文文档:https://yiyibooks.cn/xx/python_352/library/os.html OS 库英文文档:https://docs.python.org/3/libra ...
- day33 网络编程之线程,并发以及selectors模块io多路复用
io多路复用 selectors模块 概要: 并发编程需要掌握的知识点: 开启进程/线程 生产者消费者模型!!! GIL全局解释器锁(进程与线程的区别和应用场景) 进程池线程池 IO模型(理论) 1 ...
- day22 模块最后的补充。包。
前一天内容复习: # def func(): # a # # def main(): # func() # # try: # main() # except: # pass # raise NameE ...
- vue回调函数无法更改model的值
data:{ isUpload:true, } 点击上传函数: getFile(event) { //选择图片 let eventId = event.target.id; let type= tes ...
- 线段树-hdu3397
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题目描述: 题目大意:给我们一串二进制串,需要我们对其进行以下操作: 1.输入0,a,b,将a, ...
- python 配置导入方式
许多连接,如 from setting import redis_config pool= redis.ConnectionPool(**redis_config) r=redis.Redis(con ...