搭建 SpringBoot 项目(前端页面 + 数据库 + 实现源码)

Posted Yan Yang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建 SpringBoot 项目(前端页面 + 数据库 + 实现源码)相关的知识,希望对你有一定的参考价值。

SpringBoot 项目整合

一、项目准备

1.1 快速创建 SpringBoot 项目

博客地址:快速搭建 SpringBoot 项目(看完 【 1. 快速创建 SpringBoot 项目】 就可以回到这里了)

1.2 项目结构图如下

在这里插入图片描述

1.3 数据表结构图如下

在这里插入图片描述

CREATE TABLE `employee` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `admin` bit(1) DEFAULT b'0',
  `dept_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

1.4 运行结果

在这里插入图片描述


二、项目实现

1. 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 http://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.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.yy</groupId>
    <artifactId>springboot-crud</artifactId>
    <version>1.0.1</version>
    <packaging>jar</packaging>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- SpringBoot热部署插件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

        <!--mybatis集成到SpringBoot中的依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <!-- 支持使用 Spring AOP 和 AspectJ 进行切面编程。 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!-- SpringBoot集成FreeMarker的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!-- 配置分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <!-- SpringBoot 打包插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!-- MyBatis 逆向工程插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>false</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.45</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

2. application.properties 配置文件

# 修改端口号
server.port=80

# mysql 4要素
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:///springboot?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=admin

#设置SQL打印日志
logging.level.com.yy.mapper=trace

#一般我们会做3个配置,其余默认即可
#暴露session对象的属性
spring.freemarker.expose-session-attributes=true
#配置为传统模式,空值自动处理
spring.freemarker.settings.classic_compatible=true
#重新指定模板文件后缀 springboot 2.2.x 后 默认后缀为 .ftlh
spring.freemarker.suffix=.ftl

3. APP 启动类

package com.yy;

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

@SpringBootApplication
// mybatis 接口扫描注解
@MapperScan("com.yy.mapper")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}


4. EmployeeController 控制器

package com.yy.controller;

import com.github.pagehelper.PageInfo;
import com.yy.domain.Employee;
import com.yy.qo.EmployeeQueryObject;
import com.yy.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")EmployeeQueryObject qo) {
    	// mybatis 的分页插件,qo 是页面传过来的模糊条件查询参数以及当前页和每页条数
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "employee/list";
    }
}

5. IEmployeeService 接口

package com.yy.service;

import com.github.pagehelper.PageInfo;
import com.yy.domain.Employee;
import com.yy.qo.EmployeeQueryObject;

import java.util.List;

public interface IEmployeeService {
    int delete(Long id);

    int add(Employee record);

    Employee selectById(Long id);

    List<Employee> selectAll();

    int update(Employee record);

    PageInfo<Employee> query(EmployeeQueryObject qo);
}

6. EmployeeServiceImpl 接口

package com.yy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yy.domain.Employee;
import com.yy.mapper.EmployeeMapper;
import com.yy.qo.EmployeeQueryObject;
import com.yy.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Override
    public int delete(Long id) {
        return 0;
    }

    @Override
    public int add(Employee record) {
        return 0;
    }

    @Override
    public Employee selectById(Long id) {
        return null;
    }

    @Override
    public List<Employee> selectAll() {
        return null;
    }

    @Override
    public int update(Employee record) {
        return 0;
    }

    @Override
    public PageInfo<Employee> query(EmployeeQueryObject qo) {
    	// 使用 mybatis 分页插件,传入当前页和每页条数
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> list = employeeMapper.selectForList(qo);
        return new PageInfo<>(list);
    }
}

7. EmployeeMapper 接口

package com.yy.mapper;

import com.yy.domain.Employee;
import com.yy.qo.EmployeeQueryObject;

import java.util.List;

public interface EmployeeMapper {
    int deleteByPrimaryKey(Long id);

    int insert(Employee record);

    Employee selectByPrimaryKey(Long id);

    List<Employee> selectAll();

    int updateByPrimaryKey(Employee record);

    List<Employee> selectForList(EmployeeQueryObject qo);
}

8. EmployeeMapper 接口

<?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.yy.mapper.EmployeeMapper" >
  <resultMap id="BaseResultMap" type="com.yy.domain.Employee" >
    <id column="id" property="id" />
    <result column="username" property="username" />
    <result column="name" property="name" />
    <result column="password" property="password" />
    <result column="email" property="email" />
    <result column="age" property="age" />
    <result column="admin" property="admin" />
    <result column="dept_id" property="deptId" />
  </resultMap>
  <delete id="deleteByPrimaryKey" >
    delete from employee
    where id = #{id}
  </delete>
  <insert id="insert" useGeneratedKeys="true" keyProperty="id" >
    insert into employee (username, name, password, email, age, admin, dept_id)
    values (#{username}, #{name}, #{password}, #{email}, #{age}, #{admin}, #{deptId})
  </insert>
  <update id="updateByPrimaryKey" >
    update employee
    set username = #{username},
      name = #{name},
      password = #{password},
      email &

以上是关于搭建 SpringBoot 项目(前端页面 + 数据库 + 实现源码)的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot技术栈搭建个人博客项目准备

springBoot项目搭建细节笔记

WeChair项目Alpha冲刺(2/10)

SpringBoot IDEA 搭建 springboot 项目 + jsp 页面模板

SpringBoot + Spring Cloud +Vue 管理系统前端搭建(二visual studio code开发前端项目)

springBoot 搭建web项目(前后端分离,附项目源代码地址)