使用idea+springboot+Mybatis搭建web项目
来源:互联网 发布:php判断是否点击submit 编辑:程序博客网 时间:2024/05/16 18:01
使用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/testspring.datasource.username = rootspring.datasource.password = 1234spring.datasource.driverClassName = com.mysql.jdbc.Driver#页面热加载spring.thymeleaf.cache = false
创建一个controller类:helloworld
@Controller@EnableAutoConfigurationpublic 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即可
- 使用idea+springboot+Mybatis搭建web项目
- 使用idea+springboot+Mybatis搭建web项目
- IDEA搭建SpringBoot+Maven+Mybatis+MySQL-Web项目流程
- IDEA下用springboot搭建web项目
- 使用idea搭建springboot的maven项目
- IDEA上搭建maven项目整合springboot+mybatis+shiro
- IDEA上面搭建一个SpringBoot的web-mvc项目
- IDEA上面搭建一个SpringBoot的web-mvc项目
- IDEA上面搭建一个SpringBoot的web-mvc项目
- idea来搭建Springboot项目
- springboot+mybatis+redis+thymeleaf Web项目搭建 开箱即用
- 使用IDEA搭建SpringBoot项目且整合mongoDB和mysql
- 搭建SpringBoot-MyBatis-MyBatisGenerator项目
- idea使用gradle搭建SpringBoot
- 使用idea快速搭建springboot
- idea搭建web项目
- IDEA搭建Web项目
- 使用IDEA创建SpringBoot项目
- 重新认识java(七) ---- final 关键字
- ARC总结
- Xcode8 制作.a静态库和存放xib和图片的.bundle
- 简易rest接口调试工具
- Expandablelistview 简单使用
- 使用idea+springboot+Mybatis搭建web项目
- WebP 探寻之路
- Java并发编程:如何创建线程?
- P1094 纪念品分组
- 类的静态成员
- protobuf引入不同包下的proto文件
- oj中用到的一些数学上的算法
- xposed源码编译与集成
- JavaScript之window.console