赞
踩
工具及环境准备: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,添加如下配置
- # 数据库连接地址
- spring.datasource.url=jdbc:mysql://localhost:3306/db_test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
- # 数据库用户名
- spring.datasource.username=root
- # 数据库密码
- spring.datasource.password=root
- # 数据库驱动
- spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
-
- # 扫描mapper接口对应的xml文件
- mybatis.mapper-locations=classpath*:mapper/*.xml
- # 使用实体类简称,可在xml文件中省略写实体类全路径
- mybatis.type-aliases-package=com.example.demo.entity
- # 开启实体类对应表中带下划线字段名自动转为驼峰形式命名
- mybatis.configuration.map-underscore-to-camel-case=true
3、引入lomok和swaager2依赖(简化开发和调试)
<!-- Swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency> <!-- Lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
4、新建swagger2配置类
- package com.example.demo;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- import springfox.documentation.builders.ApiInfoBuilder;
- import springfox.documentation.builders.PathSelectors;
- import springfox.documentation.builders.RequestHandlerSelectors;
- import springfox.documentation.service.ApiInfo;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
-
- @Configuration
- @EnableSwagger2
- public class Swagger2 {
-
- /**
- * 添加swagger2基本配置,如扫描控制器所在的包等
- */
- @Bean
- public Docket createRestApi() {
- return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
- .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
- .paths(PathSelectors.any()).build();
- }
-
- /**
- * 构建api文档基本信息
- */
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- // 设置页面标题
- .title("swagger2自动构建的api接口文档")
- // 设置页面描述
- .description("欢迎访问api接口文档,这里是描述信息")
- // 定义版本号
- .version("1.0").build();
- }
- }
5、安装lombok插件,使得IDEA可以识别并支持lombok注解
6、数据库建表
- USE db_test;
- DROP TABLE IF EXISTS `tb_user`;
- CREATE TABLE `tb_user` (
- `id` int(0) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
- `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
- `id_no` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
- `mobile` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
- PRIMARY KEY (`id`) USING BTREE
- ) 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等方法
- package com.example.demo.entity;
-
- import io.swagger.annotations.ApiModelProperty;
- import lombok.Data;
-
- import java.io.Serializable;
-
- @Data
- public class User implements Serializable {
-
- private static final long serialVersionUID = -1247079980166680265L;
-
- private Integer id;
- @ApiModelProperty(value = "姓名")
- private String name;
- @ApiModelProperty(value = "证件号")
- private String idNo;
- @ApiModelProperty(value = "手机号")
- private String mobile;
- }
Dao层:接口UserMapper
- package com.example.demo.dao;
-
- import com.example.demo.entity.User;
- import org.apache.ibatis.annotations.Mapper;
-
- import java.util.List;
-
- @Mapper
- public interface UserMapper {
-
- /**
- * 新增用户
- *
- * @param user
- * @return
- */
- Integer addUser(User user);
-
- /**
- * 删除用户
- *
- * @param id
- * @return
- */
- Integer deleteUserById(Integer id);
-
- /**
- * 修改用户
- *
- * @param user
- * @return
- */
- Integer updateUserById(User user);
-
- /**
- * 查询用户
- *
- * @param user
- * @return
- */
- List<User> getUser(User user);
-
- }
接口对应的xml文件 :UserMapper.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.example.demo.dao.UserMapper">
-
- <insert id="addUser" parameterType="User">
- insert into
- tb_user (name,id_no, mobile)
- value (#{name},#{idNo},#{mobile})
- </insert>
-
- <delete id="deleteUserById" parameterType="Integer">
- delete from tb_user
- where id = #{id}
- </delete>
-
- <update id="updateUserById" parameterType="User">
- update tb_user
- <set>
- <if test="name != null and name != '' ">name = #{name},</if>
- <if test="idNo != null and idNo != '' ">id_no = #{idNo},</if>
- <if test="mobile != null and mobile != '' ">mobile = #{mobile}</if>
- </set>
- where id = #{id}
- </update>
-
- <select id="getUser" parameterType="User" resultType="User">
- select *
- from tb_user
- <where>
- <if test="id != null ">
- id = #{id}
- </if>
- <if test="name != null and name != '' ">
- and name = #{name}
- </if>
- <if test="idNo != null and idNo != '' ">
- and id_no = #{idNo}
- </if>
- <if test="mobile != null and mobile != '' ">
- and mobile = #{mobile}
- </if>
- </where>
- </select>
-
- </mapper>
service层:
- package com.example.demo.service;
-
- import com.example.demo.dao.UserMapper;
- import com.example.demo.entity.User;
- import org.springframework.stereotype.Service;
-
- import javax.annotation.Resource;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * 用户操作 服务类
- */
- @Service
- public class UserService {
-
- @Resource
- UserMapper userMapper;
-
- /**
- * 新增用户
- *
- * @param user
- * @return
- */
- public Map addUser(User user) {
- Map<String, Object> resultMap = new HashMap(1);
- if (userMapper.addUser(user) > 0) {
- resultMap.put("returnMsg", "新增用户成功!");
- } else {
- resultMap.put("returnMsg", "新增用户失败!");
- }
- return resultMap;
- }
-
- /**
- * 根据id删除用户
- *
- * @param id
- * @return
- */
- public Map deleteUserById(Integer id) {
- Map<String, Object> resultMap = new HashMap(1);
- if (userMapper.deleteUserById(id) > 0) {
- resultMap.put("returnMsg", "删除用户成功!");
- } else {
- resultMap.put("returnMsg", "删除用户失败!");
- }
- return resultMap;
- }
-
- /**
- * 根据id修改用户
- *
- * @param user
- * @param id
- * @return
- */
- public Map updateUserById(User user, Integer id) {
- Map<String, Object> resultMap = new HashMap(1);
- user.setId(id);
- if (userMapper.updateUserById(user) > 0) {
- resultMap.put("returnMsg", "修改用户成功!");
- } else {
- resultMap.put("returnMsg", "修改用户失败!");
- }
- return resultMap;
- }
-
- /**
- * 根据姓名查询用户
- *
- * @param name
- * @return
- */
- public List<User> getUserByName(String name) {
- User user = new User();
- user.setName(name);
- return userMapper.getUser(user);
- }
- }
controller层:
- package com.example.demo.controller;
-
- import com.example.demo.entity.User;
- import com.example.demo.service.UserService;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.List;
- import java.util.Map;
-
- @Api(tags = "用户相关操作接口", value = "UserController")
- @RestController
- @RequestMapping("/user")
- public class UserController {
-
- @Autowired
- UserService userService;
-
- @ApiOperation(value = "新增用户", notes = "新增用户记录到数据库")
- @PutMapping("/addUser")
- public Map addUser(@RequestBody User user) {
- return userService.addUser(user);
- }
-
- @ApiOperation(value = "根据id删除用户", notes = "根据id删除用户记录")
- @DeleteMapping("/deleteUserById")
- public Map deleteUser(@RequestParam Integer id) {
- return userService.deleteUserById(id);
- }
-
- @ApiOperation(value = "根据id修改用户", notes = "根据id修改用户记录")
- @PostMapping("/updateUserById")
- public Map updateUser(@RequestBody User user, @RequestParam Integer id) {
- return userService.updateUserById(user, id);
- }
-
- @ApiOperation(value = "根据姓名查询用户", notes = "根据姓名查询用户记录")
- @GetMapping("/getUserByName")
- public List<User> getUserByName(@RequestParam String name) {
- return userService.getUserByName(name);
- }
-
- }
8、完整目录结构及项目完整pom文件
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.2.4.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example</groupId>
- <artifactId>demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>demo</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
- <dependencies>
- <!-- Spring Web -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!-- Mybatis -->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.1.1</version>
- </dependency>
- <!-- Mysql -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <!-- Swagger2 -->
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>2.4.0</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>2.4.0</version>
- </dependency>
- <!-- Lombok -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>org.junit.vintage</groupId>
- <artifactId>junit-vintage-engine</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
9、本地启动项目后,访问swagger2自动生成的api接口文档:http://localhost:8080/swagger-ui.html#
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。