使用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.如何包装返回统一结构结果数据? 首先要弄清楚为什么要 ...
随机推荐
- hibnet 自动生成表格
在pom文件中添加相关的依赖 在application.yml文件中添加配置信息 新建实体类Boy 项目运行,mysql就产生了一个boy表. 需要注意的是:当配置文件ddl-auto: create ...
- BZOJ1975 [Sdoi2010]魔法猪学院 k短路
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1975 题意概括 给出一个无向图,让你走不同的路径,从1到n,路径长度之和不超过E,求最大路径条数. ...
- Service Fabric SfDevCluster目录从默认的C盘移动
管理员权限打开Powershell CD\ 回车 CD "C:\Program Files\Microsoft SDKs\Service Fabric\ClusterSetup" ...
- Java BigDecimal详解,提供了丰富的四舍五入规则
java.math.BigDecimal类提供用于算术,刻度操作,舍入,比较,哈希算法和格式转换操作. toString()方法提供BigDecimal的规范表示.它使用户可以完全控制舍入行为. 提供 ...
- hdu-2043解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2043 题意解析:目的是判断一串密码是否安全,条件是满足1.大写字母:A,B,C...Z; 2.小写 ...
- Java 之 Web前端(五)
1.过滤器 a.定义:是一个中间组件,用于拦截源数据和目的数据之间的消息,并过滤二者之间传递的数据 b.步骤: ①建class继承Filter实现抽象方法 public class EncodingF ...
- [ 高危 ] mt SQL注入
RANK 28 金币 28 不是很核心的系统,但是这个检测方法挺新鲜的 数据包 POST /XXXpital HTTP/1.1Content-Length: 96Content-Type: a ...
- checkbox jquery操作总结
$('input[name="myCheckbox"]').prop('checked','true'); // 全选 $('input[name="myCheckbox ...
- notepad++ 注释
在用notepad++进行代码编辑的过程中,总感觉还是有keil那样可以进行多行注释的快捷方式方便,其实notepad++也可以进行单行.多行.区块注释和取消注释的....... 快捷键如下: 单行. ...
- groupadd 创建组
groupadd 创建组 1 注意 :root用户才有权使用这个命令 2 groupadd -g 744 cjh 指定组ID号 3 在/etc/passwd 产生一个 组ID GID gpasswd ...