首页 > 其他分享 >SpringBootWeb项目的简单搭建

SpringBootWeb项目的简单搭建

时间:2022-12-07 10:57:03浏览次数:58  
标签:mapper 简单 boot SpringBootWeb org import com public 搭建

Spring Boot 搭建Web项目


目录

      版本及环境
      Maven配置
      数据库的连接
      静态资源的访问

版本及环境

项目 版本 描述or选用原因 是否必须
JDK 1.8 1.8版本经久不衰,可SpringBoot4不支持 yes
Maven 3.8.3 一个项目构建和包管理工具,构建SpringBoot需要3.5+ yes
springBoot 2.4.13 网上的教程和对应问题的解决办法较多,兼容1.6 yes
mybatis 3.4.0 一个功能强大的数据持久框架,支持动态sql,结果集映射 和存储过程的好东西 yes
thymeleaf 2.4.13 Thymeleaf是一款用于渲染XML/HTML5内容的模板引擎 yes

maven配置文件


点击查看代码
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <groupId>org.example</groupId>
    <artifactId>myBlog</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.13</version>
    </parent>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                    <include>**/*</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
    <dependencies>
        <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>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>
    <description/>
    <developers>
        <developer/>
    </developers>
    <licenses>
        <license/>
    </licenses>
    <scm>
        <url/>
    </scm>
    <url/>
</project>

资源过滤的配置是必选的,否则会出现Mybatis Sqlsession注入失败的错误 以及application.yml文件无法导出、静态资源无法导出、页面访问404、数据库无法连接、mapper无法映射等问题

项目目录结构
image

cat中的目录结构
image

resources目录结构
image

总目录结构

image

目录说明

目录 说明
resources 用于存放配置文件和网页资源文件
mapper mybatis的mapper.xml存放位置
static 用于存放静态资源,如css,js,html等,springboot的默认静态目录的命名
templates 用于存放视图,比如html或者jsp等
controller 控制层
entity 存放pojo
mapper mybatis的dao层接口和mapper.xml文件存放地

文件说明:application.yml是springboot配置文件,用于写一些配置,比如mybatis,数据源,模板引擎的配置

项目搭建:
image
idea创建普通项目和编写maven配置文件已经完成,application.yml文件的编写

点击查看代码
server:
  port: 8080
spring:
  datasource:
    username: root
    password:
    url: jdbc:mysql://localhost:3306/catblog?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  mapper-locations: classspath:mapper/*.xml
  type-aliases-package: com.example.cat.home.entity
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql
thymeleaf:
    cache: false
freemarker:
    cache: false
devtools:
    restart:
      enabled: true
      additional-paths: src/main/java
      exclude: static/**
其中mybatis中的mapper-location是mybatis的XXXmapper.xml文件的存放地点,主要用来实现dao层中的查询接口。在java中的mapper包中。创建一个UserMapper类。 代码如下
点击查看代码
package com.example.cat.mapper;

import com.example.cat.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserMapper {
    List<User> AD();
}

@Repository和@Mapper注解是二选一的,但必须选择一个,否则无法自动装配sqlsession,在springboot启动类中要加入注解@MapperScan("com.example.cat.mapper"),这个注解用于扫描mapper的所在的包,告诉spring那些mapper是需要自动装配的。

Controller的编写 只需要注解@Controller注解在类上便可以把类变成Controller类,需要返回json,则要用RestController注解,该注解是两个注解的合并,这两个注解分别是@ResponseBody和@Controller。 为方便测试,在主类加上@RestController,并测试数据库是否搭建成功 测试代码: ``
点击查看代码
package com.example;


import com.example.cat.entity.User;
import com.example.cat.mapper.UserMapper;
import com.google.gson.Gson;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@SpringBootApplication
@MapperScan("com.example.cat.mapper")
public class MyApplication {

    UserMapper userMapper ;
   @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    @RequestMapping("/")
    List<User>  SS (){

        Gson gson = new Gson();
        System.out.println(gson.toJson(userMapper.AD()));
        return userMapper.AD();
    }
    public static void main(String[] args) {

        SpringApplication.run(MyApplication.class,args);
    }
}

运行结果:
控制台
image
浏览器返回
image

dao层对应的代码
UserMapper

点击查看代码
package com.example.cat.mapper;

import com.example.cat.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;


@Mapper
public interface UserMapper {
    List<User> AD();
}

对应配置文件的编写
点击查看代码
<?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">
<!--第 1 步绑定 mapper接口全类名-->
<mapper namespace="com.example.cat.mapper.UserMapper">
    <!-- 'id' 就是mapper接口里面的方法名  'resultType' 返回类型(也要是全类名)   -->

    <select id="AD" resultType="com.example.cat.entity.User">
        SELECT * FROM  c_user
    </select>



</mapper>

数据库表
image

对应表的javaBean

点击查看代码
public class User {
    Integer id;
    String UserName;
    String UserPsw;
    String UserPhone;

    public User() {
    }

    public User(Integer id, String userName, String userPsw, String userPhone) {
        this.id = id;
        UserName = userName;
        UserPsw = userPsw;
        UserPhone = userPhone;
    }

    public Integer getId() {
        return id;
    }

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

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public String getUserPsw() {
        return UserPsw;
    }

    public void setUserPsw(String userPsw) {
        UserPsw = userPsw;
    }

    public String getUserPhone() {
        return UserPhone;
    }

    public void setUserPhone(String userPhone) {
        UserPhone = userPhone;
    }
}

静态资源的访问
需要直接通过浏览器访问而不走Controller。需要把静态资源放在static目录下,访问时候的路径为/CatCC.html 带目录为/js/ada.js

标签:mapper,简单,boot,SpringBootWeb,org,import,com,public,搭建
From: https://www.cnblogs.com/aoCat/p/16961752.html

相关文章