使用Spring boot 快速创建项目

古城微笑少年丶 2022-01-28 00:35 474阅读 0赞

Spring boot 是整合了框架,以快速开发为目的,大大节省了开发效率。

以下是创建项目。利用STS 4.0软件使用时必须安装eclipse 和 sts 4.0 。方可打开sts4.0

创建好的项目然后建立项目结构

20190522194541677.png

建立application.yml文件配置

  1. server:
  2. port: 8888
  3. spring:
  4. datasource:
  5. url: jdbc:sqlserver://10.10.10.2:1433;databaseName=scmis000
  6. username: sc
  7. password: redone
  8. dbcp2:
  9. driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
  10. initial-size: 5
  11. min-idle: 10
  12. max-wait-millis: 10000

系统生成的spring boot 主类

  1. package com.xukai;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  6. //mapperscan扫描。这样的话就不用一个个配置了。
  7. @MapperScan("com.xukai.mapper")
  8. @SpringBootApplication
  9. public class XmHelperApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(XmHelperApplication.class, args);
  12. }
  13. }

用mybatis-generator-core-1.3.7.jar 来逆向生成数据库mapper和po类

直接用注解的形式写代码。方便快捷。

  1. package com.xukai.mapper;
  2. import com.xukai.po.Personnel;
  3. import java.util.List;
  4. import org.apache.ibatis.annotations.Select;
  5. public interface PersonnelMapper {
  6. @Select("SELECT * FROM personnel WHERE PersonnelId = #{id}")
  7. Personnel getPersonnelById(String id);
  8. @Select("select * from personnel")
  9. List<Personnel> selectAll();
  10. @Select("select Password from OperPw where PersonId = #{id}")
  11. String getPWDByID(String id);
  12. }

这是控制器

  1. package com.xukai.controller;
  2. import java.rmi.RemoteException;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.RequestBody;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import com.fasterxml.jackson.core.JsonProcessingException;
  11. import com.fasterxml.jackson.databind.ObjectMapper;
  12. import com.xukai.mapper.PersonnelMapper;
  13. import com.xukai.po.Personnel;
  14. //这个注解是不返回页面,而是返回json数据
  15. @RestController
  16. public class LoginController {
  17. @Autowired
  18. private PersonnelMapper personnelMapper;
  19. @RequestMapping(value = "login", method = RequestMethod.POST)
  20. public String helloWorld(@RequestBody Map<String, String> maps) throws JsonProcessingException, RemoteException {
  21. ObjectMapper objectMapper = new ObjectMapper();
  22. Map<String, String> map = new HashMap<>();
  23. String resultString;
  24. String username = maps.get("username");
  25. String password = maps.get("password");
  26. //查询是否已经关联
  27. Personnel personnel = personnelMapper.getPersonnelById(username);
  28. if (personnel == null) {
  29. map.put("success", "0");
  30. map.put("reason", "无此账号!");
  31. resultString = objectMapper.writeValueAsString(map);
  32. } else {
  33. String encryptPWD = personnelMapper.getPWDByID(personnel.getPersonnelid());
  34. personnel.setEncryptPWD(encryptPWD);
  35. personnel.setPassword(pwd);
  36. //判断密码是否正确
  37. if (password.equals(pwd)) {
  38. resultString = objectMapper.writeValueAsString(personnel);
  39. } else {
  40. map.put("success", "0");
  41. map.put("reason", "密码不正确!");
  42. resultString = objectMapper.writeValueAsString(map);
  43. }
  44. }
  45. return resultString;
  46. }
  47. }

OK。项目配置完了。快吧? 而且不用tomcat就可以直接运行。

发表评论

表情:
评论列表 (有 0 条评论,474人围观)

还没有评论,来说两句吧...

相关阅读