展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

spring boot纯注解开发模板

简介

  • spring boot纯注解开发模板

  • 创建项目

  • pom.xml导入所需依赖

点击查看源码
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 自动打包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!-- maven编译 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

  • 将application文件的后缀改为yml,配置服务器端口和数据库连接池、视图解析、驼峰命名自动映射、日志记录
点击查看源码
# 端口
server:
  port: 8080

# 配置数据源
spring:
  application:
    name: springboot01   # 项目名
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot?characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: root
  mvc: # 视图解析
    view:
      suffix: ".html"

# 开启驼峰命名自动映射
mybatis:
  configuration:
    map-underscore-to-camel-case: true

# 配置日志记录
logging:
  level:
    com:
      chnq:
        springboot01: debug

  • 启动类:Springboot01Application
  • 控制层新建一个RouterController测试路由跳转
点击查看源码
@Controller
public class RouterController {
    /**
     * 转发到 /login路由
     */
    @RequestMapping("/register")
    public String hello(){
        return "forward:/login";
    }

    /**
     * 访问静态资源中名称为login的文件
     */
    @RequestMapping("/login")
    public String hello1(){
        return "login";
    }

    /**
     * 重定向到index.html
     */
    @GetMapping("/index")
    public String hello2(){
        return "redirect:/index.html";
    }

    /**
     * 返回的是数据
     */
    @GetMapping("/goIndex")
    @ResponseBody
    public String hello3(){
        return "index";
    }

}

业务编写

  • mapper层:UserMapper,接口+注解的方式,构建器处理复杂sql
  • model层:User,创建项目时导入依赖lombok,使用@Geter+@Setter注解,表示自动生成getter和setter方法
  • service层:UserService类,标注为业务层,注入mapper层对象,处理业务逻辑
  • 控制器:UserController类,注入业务层对象,处理请求跳转
  • 工具类:RespResult类,响应给前端的数据

测试

下载

posted @ 2021-05-30 10:21  DogLeftover  阅读(138)  评论(0编辑  收藏  举报