不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2020-11-20 16:01:32  修改时间:2024-04-14 10:32:04  分类:Java框架/系统  编辑

1、新建一个Spring Initializr项目

2、创建项目的文件结构以及jdk的版本

3、选择项目所需要的依赖

4、修改项目名,finish完成

5、新建好的pom.xml文件

<?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>
    <groupId>com.wenjianbao</groupId>
    <artifactId>bus-server</artifactId>
    <version>0.0.1</version>
    <name>bus-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.4.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!--MySQL++++++++++++++++++++++++++++++++++++-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--/MySQL++++++++++++++++++++++++++++++++++++-->
        
        <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>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.4.RELEASE</version>
                <configuration>
                    <mainClass>com.wenjianbao.busserver.BusServerApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

6、修改配置文件

本文不使用application.properties文件,而使用更加简洁的application.yml文件。

将resource文件夹下原有的application.properties文件删除,创建application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为application.properties),本文创建了两个yml文件(application.ymlapplication-dev.yml),分别来看一下内容:

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

# 应用服务 WEB 访问端口
server:
  port: 8080

# 应用名称
spring:
  application:
    name: bus-server


  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/wjb_bus?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: 123456


mybatis:
  mapper-locations: classpath:mapping/*Mapping.xml
  type-aliases-package: com.wenjianbao.busserver.entity

logging:
  level:
    com:
      wenjianbao:
        busserver:
          mapper: debug

两个文件的意思是:

在项目中配置多套环境的配置方法。
因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行如application.yml

笔记:在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:

application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

7、开始创建 实体类 实现业务流程

创建包controller、entity、mapper、service。resources下创建mapping文件夹,用于写sql语句,也可以用注解的方式直接写在mapper文件里。下面直接贴代码

数据库表结构(之前小项目的表,直接拿来用)

CREATE TABLE `user`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
  `enterpriseId` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '所属企业ID',
  `userName` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '用户名',
  `password` char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '加密后的密码',
  `salt` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '密码随机盐',
  `mobile` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '手机号',
  `addTime` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '添加时间',
  `updateTime` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '修改时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;

entity.java

package com.wenjianbao.busserver.entity;

/**
 * 用户表实体
 */
public class User {
    private Integer id;
    private Integer enterpriseId;
    private String userName;
    private String password;
    private String salt;
    private String mobile;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(Integer enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Override
    public String toString() {
        return "User{" +
            "id=" + id +
            "enterpriseId=" + enterpriseId +
            ", userName='" + userName + '\'' +
            ", password='" + password + '\'' +
            ", mobile='" + mobile + '\'' +
            '}';
    }
}

UserMapping.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.wenjianbao.busserver.mapper.UserMapper">

    <resultMap id="BaseResultMap" type="com.wenjianbao.busserver.entity.User">
        <result column="id" jdbcType="INTEGER" property="id"/>
        <result column="enterpriseId" jdbcType="VARCHAR" property="enterpriseId"/>
        <result column="userName" jdbcType="VARCHAR" property="userName"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
        <result column="salt" jdbcType="VARCHAR" property="salt"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
    </resultMap>

    <select id="Sel" resultType="com.wenjianbao.busserver.entity.User">
        select * from user where id = #{id}
    </select>

</mapper>

UserMapper.java

package com.wenjianbao.busserver.mapper;

import com.wenjianbao.busserver.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {
    User Sel(int id);
}

UserService.java

package com.wenjianbao.busserver.service;

import com.wenjianbao.busserver.entity.User;
import com.wenjianbao.busserver.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public User Sel(int id) {
        return userMapper.Sel(id);
    }
}

UserController.java

package com.wenjianbao.busserver.controller;

import com.wenjianbao.busserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/testBoot")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("getUser/{id}")
    public String getUser(@PathVariable int id) {
        return userService.Sel(id).toString();
    }
}

8、最终框架结构

9、完成以上,下面在启动类里加上注解用于给出需要扫描的mapper文件路径

@MapperScan("com.wenjianbao.busserver.mapper")

参考代码如下:

package com.wenjianbao.busserver;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.wenjianbao.busserver.mapper")
@SpringBootApplication
public class BusServerApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(BusServerApplication.class, args);
    }

}

10、最后启动看效果

浏览器输入地址:http://localhost:8080/testBoot/getUser/1

输出:

 

 

参考:https://blog.csdn.net/iku5200/article/details/82856621