使用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.如何包装返回统一结构结果数据? 首先要弄清楚为什么要 ...
随机推荐
- python之https爬虫出现 SSL: CERTIFICATE_VERIFY_FAILED (同时打开fiddler就会出现)
1.参考 Py 坑之 CERTIFICATE_VERIFY_FAILED Python 升级到 2.7.9 之后引入了一个新特性,当你urllib.urlopen一个 https 的时候,会验证一次 ...
- django源码分析
原文网址 https://www.jianshu.com/p/17d78b52c732?utm_campaign=maleskine&utm_content=note&utm_medi ...
- Kafka/Zookeeper集群的实现(二)
[root@kafkazk1 ~]# wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12. ...
- HBase的概述和安装部署
一.HBase概述 1.HBase是Hadoop数据库,是一个分布式.可扩展的大数据存储. HBase是用于对大数据进行随机.实时读写访问的非关系型数据库,它的目标托管非常大的表——数十亿行N百万列. ...
- javascript监听数组变化
, ]; ); , ]; ); , ]; ); ]; , ]; ); ); ); break; } if (inserted) { ob.observeArray(inserted); } // 通知 ...
- POJ 2352 Stars【树状数组】
<题目链接> 题目大意: 题目给出n个点,这些点按照y坐标的升序,若y相同,则按照x的升序顺序输入,问,在这些点中,左下角的点的数量分别在0~n-1的点分别有多少个,写出它们的对应点数. ...
- POJ 3169 Layout 【差分约束】+【spfa】
<题目链接> 题目大意: 一些母牛按序号排成一条直线.有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没有最大距离输出-1,如果1.n之间距离任意就 ...
- python系统性能模块笔记
内存信息psutil.cpu_times() 使用cpu_times方法获取cpu完整信息,需要显示所有逻辑cpu信息(指定变量percpu=True)psutil.cpu_ti ...
- react那些事儿
一.参考链接https://reactjs.org/http://react-china.org/https://doc.react-china.org/https://hulufei.gitbook ...
- 南阳236----心急的C小加
#include<cstdio> #include<algorithm> #define inf 1<<30 using namespace std; typede ...