当前位置:   article > 正文

IDEA快速搭建SpringBoot后端项目(ssm)_idear新建springboot后端项目

idear新建springboot后端项目

工具及环境准备:Jdk1.8、Maven3.3.9、Idea2018.2

第一步、快速生成springboot的demo项目

通过Idea的Spring Initializr功能生成demo项目(需要连外网才行)

1、创建新项目

2、模板选Spring Initializr,选SDK(即JDK),点击“Next”

3、项目设置默认即可,直接点击“Next”

4、项目依赖选择,Web分组:spring web,SQL分组:mybatis、mysql,点击“Next”

5、项目名和项目存放路径默认即可,直接点击“Finish”

注意:也可以利用官网在线生成demo项目,下载到本地后导入开发工具,如eclipse等

1、进入项目生成页面,项目参数可默认,依赖选Web即可,生成demo压缩包并下载到本地

官网demo生成地址:https://start.spring.io/

2、解压到某一目录下,再导入Eclipse或者Idea

 

第二步、在demo项目基础上扩展成ssm后端项目

1、在对应位置建立基本结构,entity、dao、service、controller、mapper

2、修改springboot配置文件application.properties,添加如下配置

  1. # 数据库连接地址
  2. spring.datasource.url=jdbc:mysql://localhost:3306/db_test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
  3. # 数据库用户名
  4. spring.datasource.username=root
  5. # 数据库密码
  6. spring.datasource.password=root
  7. # 数据库驱动
  8. spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
  9. # 扫描mapper接口对应的xml文件
  10. mybatis.mapper-locations=classpath*:mapper/*.xml
  11. # 使用实体类简称,可在xml文件中省略写实体类全路径
  12. mybatis.type-aliases-package=com.example.demo.entity
  13. # 开启实体类对应表中带下划线字段名自动转为驼峰形式命名
  14. mybatis.configuration.map-underscore-to-camel-case=true

3、引入lomok和swaager2依赖(简化开发和调试)

  1. <!-- Swagger2 -->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.4.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>io.springfox</groupId>
  9. <artifactId>springfox-swagger-ui</artifactId>
  10. <version>2.4.0</version>
  11. </dependency>
  12. <!-- Lombok -->
  13. <dependency>
  14. <groupId>org.projectlombok</groupId>
  15. <artifactId>lombok</artifactId>
  16. </dependency>

4、新建swagger2配置类

  1. package com.example.demo;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.builders.PathSelectors;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.ApiInfo;
  8. import springfox.documentation.spi.DocumentationType;
  9. import springfox.documentation.spring.web.plugins.Docket;
  10. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  11. @Configuration
  12. @EnableSwagger2
  13. public class Swagger2 {
  14. /**
  15. * 添加swagger2基本配置,如扫描控制器所在的包等
  16. */
  17. @Bean
  18. public Docket createRestApi() {
  19. return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
  20. .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
  21. .paths(PathSelectors.any()).build();
  22. }
  23. /**
  24. * 构建api文档基本信息
  25. */
  26. private ApiInfo apiInfo() {
  27. return new ApiInfoBuilder()
  28. // 设置页面标题
  29. .title("swagger2自动构建的api接口文档")
  30. // 设置页面描述
  31. .description("欢迎访问api接口文档,这里是描述信息")
  32. // 定义版本号
  33. .version("1.0").build();
  34. }
  35. }

 5、安装lombok插件,使得IDEA可以识别并支持lombok注解 

 6、数据库建表

  1. USE db_test;
  2. DROP TABLE IF EXISTS `tb_user`;
  3. CREATE TABLE `tb_user` (
  4. `id` int(0) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
  5. `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  6. `id_no` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  7. `mobile` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  8. PRIMARY KEY (`id`) USING BTREE
  9. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

7、新增实体类、增删改查mapper接口及xml文件、service、controller

实体类:User。

注意:如果lombok的@Data注解被Idea识别成功,则属性名(如:id,name等)应该是与关键字一样的紫色;

若显示为灰色,则说明lombok注解没有被Idea正确识别,在其他地方将无法使用lombok自动生成的get/set等方法

  1. package com.example.demo.entity;
  2. import io.swagger.annotations.ApiModelProperty;
  3. import lombok.Data;
  4. import java.io.Serializable;
  5. @Data
  6. public class User implements Serializable {
  7. private static final long serialVersionUID = -1247079980166680265L;
  8. private Integer id;
  9. @ApiModelProperty(value = "姓名")
  10. private String name;
  11. @ApiModelProperty(value = "证件号")
  12. private String idNo;
  13. @ApiModelProperty(value = "手机号")
  14. private String mobile;
  15. }

Dao层:接口UserMapper

  1. package com.example.demo.dao;
  2. import com.example.demo.entity.User;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import java.util.List;
  5. @Mapper
  6. public interface UserMapper {
  7. /**
  8. * 新增用户
  9. *
  10. * @param user
  11. * @return
  12. */
  13. Integer addUser(User user);
  14. /**
  15. * 删除用户
  16. *
  17. * @param id
  18. * @return
  19. */
  20. Integer deleteUserById(Integer id);
  21. /**
  22. * 修改用户
  23. *
  24. * @param user
  25. * @return
  26. */
  27. Integer updateUserById(User user);
  28. /**
  29. * 查询用户
  30. *
  31. * @param user
  32. * @return
  33. */
  34. List<User> getUser(User user);
  35. }

接口对应的xml文件 :UserMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.demo.dao.UserMapper">
  4. <insert id="addUser" parameterType="User">
  5. insert into
  6. tb_user (name,id_no, mobile)
  7. value (#{name},#{idNo},#{mobile})
  8. </insert>
  9. <delete id="deleteUserById" parameterType="Integer">
  10. delete from tb_user
  11. where id = #{id}
  12. </delete>
  13. <update id="updateUserById" parameterType="User">
  14. update tb_user
  15. <set>
  16. <if test="name != null and name != '' ">name = #{name},</if>
  17. <if test="idNo != null and idNo != '' ">id_no = #{idNo},</if>
  18. <if test="mobile != null and mobile != '' ">mobile = #{mobile}</if>
  19. </set>
  20. where id = #{id}
  21. </update>
  22. <select id="getUser" parameterType="User" resultType="User">
  23. select *
  24. from tb_user
  25. <where>
  26. <if test="id != null ">
  27. id = #{id}
  28. </if>
  29. <if test="name != null and name != '' ">
  30. and name = #{name}
  31. </if>
  32. <if test="idNo != null and idNo != '' ">
  33. and id_no = #{idNo}
  34. </if>
  35. <if test="mobile != null and mobile != '' ">
  36. and mobile = #{mobile}
  37. </if>
  38. </where>
  39. </select>
  40. </mapper>

service层:

  1. package com.example.demo.service;
  2. import com.example.demo.dao.UserMapper;
  3. import com.example.demo.entity.User;
  4. import org.springframework.stereotype.Service;
  5. import javax.annotation.Resource;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. /**
  10. * 用户操作 服务类
  11. */
  12. @Service
  13. public class UserService {
  14. @Resource
  15. UserMapper userMapper;
  16. /**
  17. * 新增用户
  18. *
  19. * @param user
  20. * @return
  21. */
  22. public Map addUser(User user) {
  23. Map<String, Object> resultMap = new HashMap(1);
  24. if (userMapper.addUser(user) > 0) {
  25. resultMap.put("returnMsg", "新增用户成功!");
  26. } else {
  27. resultMap.put("returnMsg", "新增用户失败!");
  28. }
  29. return resultMap;
  30. }
  31. /**
  32. * 根据id删除用户
  33. *
  34. * @param id
  35. * @return
  36. */
  37. public Map deleteUserById(Integer id) {
  38. Map<String, Object> resultMap = new HashMap(1);
  39. if (userMapper.deleteUserById(id) > 0) {
  40. resultMap.put("returnMsg", "删除用户成功!");
  41. } else {
  42. resultMap.put("returnMsg", "删除用户失败!");
  43. }
  44. return resultMap;
  45. }
  46. /**
  47. * 根据id修改用户
  48. *
  49. * @param user
  50. * @param id
  51. * @return
  52. */
  53. public Map updateUserById(User user, Integer id) {
  54. Map<String, Object> resultMap = new HashMap(1);
  55. user.setId(id);
  56. if (userMapper.updateUserById(user) > 0) {
  57. resultMap.put("returnMsg", "修改用户成功!");
  58. } else {
  59. resultMap.put("returnMsg", "修改用户失败!");
  60. }
  61. return resultMap;
  62. }
  63. /**
  64. * 根据姓名查询用户
  65. *
  66. * @param name
  67. * @return
  68. */
  69. public List<User> getUserByName(String name) {
  70. User user = new User();
  71. user.setName(name);
  72. return userMapper.getUser(user);
  73. }
  74. }

controller层:

  1. package com.example.demo.controller;
  2. import com.example.demo.entity.User;
  3. import com.example.demo.service.UserService;
  4. import io.swagger.annotations.Api;
  5. import io.swagger.annotations.ApiOperation;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.*;
  8. import java.util.List;
  9. import java.util.Map;
  10. @Api(tags = "用户相关操作接口", value = "UserController")
  11. @RestController
  12. @RequestMapping("/user")
  13. public class UserController {
  14. @Autowired
  15. UserService userService;
  16. @ApiOperation(value = "新增用户", notes = "新增用户记录到数据库")
  17. @PutMapping("/addUser")
  18. public Map addUser(@RequestBody User user) {
  19. return userService.addUser(user);
  20. }
  21. @ApiOperation(value = "根据id删除用户", notes = "根据id删除用户记录")
  22. @DeleteMapping("/deleteUserById")
  23. public Map deleteUser(@RequestParam Integer id) {
  24. return userService.deleteUserById(id);
  25. }
  26. @ApiOperation(value = "根据id修改用户", notes = "根据id修改用户记录")
  27. @PostMapping("/updateUserById")
  28. public Map updateUser(@RequestBody User user, @RequestParam Integer id) {
  29. return userService.updateUserById(user, id);
  30. }
  31. @ApiOperation(value = "根据姓名查询用户", notes = "根据姓名查询用户记录")
  32. @GetMapping("/getUserByName")
  33. public List<User> getUserByName(@RequestParam String name) {
  34. return userService.getUserByName(name);
  35. }
  36. }

8、完整目录结构及项目完整pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.2.4.RELEASE</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.example</groupId>
  13. <artifactId>demo</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>demo</name>
  16. <description>Demo project for Spring Boot</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <!-- Spring Web -->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <!-- Mybatis -->
  27. <dependency>
  28. <groupId>org.mybatis.spring.boot</groupId>
  29. <artifactId>mybatis-spring-boot-starter</artifactId>
  30. <version>2.1.1</version>
  31. </dependency>
  32. <!-- Mysql -->
  33. <dependency>
  34. <groupId>mysql</groupId>
  35. <artifactId>mysql-connector-java</artifactId>
  36. <scope>runtime</scope>
  37. </dependency>
  38. <!-- Swagger2 -->
  39. <dependency>
  40. <groupId>io.springfox</groupId>
  41. <artifactId>springfox-swagger2</artifactId>
  42. <version>2.4.0</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>io.springfox</groupId>
  46. <artifactId>springfox-swagger-ui</artifactId>
  47. <version>2.4.0</version>
  48. </dependency>
  49. <!-- Lombok -->
  50. <dependency>
  51. <groupId>org.projectlombok</groupId>
  52. <artifactId>lombok</artifactId>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-test</artifactId>
  57. <scope>test</scope>
  58. <exclusions>
  59. <exclusion>
  60. <groupId>org.junit.vintage</groupId>
  61. <artifactId>junit-vintage-engine</artifactId>
  62. </exclusion>
  63. </exclusions>
  64. </dependency>
  65. </dependencies>
  66. <build>
  67. <plugins>
  68. <plugin>
  69. <groupId>org.springframework.boot</groupId>
  70. <artifactId>spring-boot-maven-plugin</artifactId>
  71. </plugin>
  72. </plugins>
  73. </build>
  74. </project>

9、本地启动项目后,访问swagger2自动生成的api接口文档:http://localhost:8080/swagger-ui.html#

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/584650
推荐阅读
相关标签
  

闽ICP备14008679号