使用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.如何包装返回统一结构结果数据? 首先要弄清楚为什么要 ...
随机推荐
- MySQl 查询性能优化相关
0. 1.参考 提升网站访问速度的 SQL 查询优化技巧 缓存一切数据,读取内存而不是硬盘IO 如果你的服务器默认情况下没有使用MySQL查询缓存,那么你应该开启缓存.开启缓存意味着MySQL 会把所 ...
- Nginx 拒接服务漏洞(CVE-2016-0747)整改
Nginx的拒绝服务漏洞主要影响版本为1.10.3之前的版本,为不影响原有nginx的使用,且为避免修改其它配置文件,可以通过编译nginx最新版本的执行文件去替换旧的执行文件,文中的场景为由ngin ...
- 经典的XSS案例
在做安全审计的时候,通过常用的<script>alert(1)</script>无法发现该XSS
- JMeter非GUI模式下日志介绍
Creating summariser <summary> Created the tree successfully using /opt/JMeter/TestPlan/test.jm ...
- spark操作Kudu之写 - 使用DataFrame API
在通过DataFrame API编写时,目前只支持一种模式“append”.尚未实现的“覆盖”模式 import org.apache.kudu.spark.kudu._ import org.apa ...
- Python学习(九) —— 正则表达式与re模块
一.正则表达式 定义:正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. 正则表达 ...
- 非root用户下实现SSH免密码登录
1.创建公钥.公钥 ssh-keygen -t rsa 无视它出来的任何提示,欢快的一路回车到底吧. 2.把公钥 id_rsa.pub 复制到远程机器的 /home/username/.ssh目录 并 ...
- Lock为线程上锁,防止数据混乱
用法: 先实例化 lock = threading.Lock() 1. lock.acquire() 上锁 需上锁代码 lock.release() 解锁 2. with lock: 上下两种方式都 ...
- Knn:Knn实现对150朵共三种花的实例的萼片长度、宽,花瓣长、宽数据统计,根据一朵新花的四个特征来预测其种类
from sklearn import neighbors from sklearn import datasets knn = neighbors.KNeighborsClassifier() ir ...
- Codeforces 1096D Easy Problem 【DP】
<题目链接> 题目大意: 给你一个字符串,每个字符有权值,问现在删除字符串中的字符使其中没有"hard"的最小代价是多少. 解题分析: 用DP来求解: 转 ...