简单项目【springboot】1

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: 简单项目【springboot】1

借鉴狂神springboot项目

第一步 设计数据表

  • department
create table department(
    id int,
    departmentName varchar(10)
)
  • employee
create table employee
(
    id            int auto_increment
        primary key,
    last_name     varchar(20) not null,
    email         varchar(20) not null,
    gender        int         not null,
    department_id int         references department(id),
    birth         datetime    not null
);
  • 初始数据
insert into department(id, departmentName) values
 (101,'教学部'),
(102,'市场部'),
(103,'教研部'),
 (104,'运营部'),
 (105,'后勤部')
insert into employee(last_name, email, gender, department_id, birth) values
('AA','A123456@qq.com',0,101,'2019-1-1 00:00:00'),
 ('BB','B123456@qq.com',1,102,'2019-1-1 00:00:00'),
 ('CC','C123456@qq.com',0,103,'2019-1-1 00:00:00'),
 ('DD','D123456@qq.com',1,104,'2019-1-1 00:00:00'),
 ('EE','E123456@qq.com',0,105,'2019-1-1 00:00:00')




第二步 建Springboot项目



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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kuang</groupId>
    <artifactId>springboot-05-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-05-project</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <!--  druid      -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
        <!--   log4j      -->
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
        <!--thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </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>
        </plugins>
    </build>
</project>

application.properties

# 关闭模板引擎的缓存
spring.thymeleaf.cache=false
# 我们的配置文件放在的真实位置
spring.messages.basename=i18n.login
#json格式化全局配置
spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd
spring.jackson.default-property-inclusion=NON_NULL
# 日期格式化
spring.mvc.date-format=yyyy-MM-dd
# 整合mybatis
# 驼峰
mybatis.configuration.mapUnderscoreToCamelCase=true 
#mybatis.type-aliases-package=com.kuang.pojo
#mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

application.yml

spring:
  datasource:
    username: root
    password: root
    # serverTimezone=UTC解决时区的报错
    url: jdbc:mysql://localhost:3306/kuang_boot?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

测试1

    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads() throws SQLException {
        //查看默认数据源
        System.out.println(dataSource.getClass());
        //获得数据库连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        //关闭
        connection.close();
    }

pojo

  • Department类
package com.kuang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Department {
    private Integer id;
    private String departmentName;
}
  • Employee类
package com.kuang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@NoArgsConstructor
@Data
public class Employee {
    private Integer id;
    private String lastName;
    private String email;
    private Integer gender;//0 女 1男
    private Integer departmentId;
    private Date birth;
    public Employee(String lastName, String email, Integer gender, Integer departmentId,Date birth) {
        this.lastName = lastName;
        this.email = email;
        this.gender = gender;
        this.departmentId=departmentId;
        this.birth=birth;
    }
}

mapper

  • DepartmentMapper
package com.kuang.mapper;
import com.kuang.pojo.Department;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.Collection;
@Mapper
@Repository
public interface DepartmentMapper {
    //获得所有部门信息
    @Select("select * from department")
    Collection<Department> getDepartments();
    //通过id得到部门
    @Select("select * from department where id=#{id}")
    Department getDepartmentById(Integer id);
}
  • EmployeeMapper
package com.kuang.mapper;
import com.kuang.pojo.Employee;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface EmployeeMapper {
    //增加一个员工
    @Insert("insert into employee(last_name, email, gender, department_id,birth) value (#{lastName},#{email},#{gender},#{departmentId},#{birth})")
    void save(Employee employee);
    //查询全部员工信息
    @Select("select * from employee")
    List<Employee> getAll();
    //通过id得到员工
    @Select(" select * from employee where id = #{id}")
    Employee getEmployeeById(Integer id);
    @Update("update employee set last_name=#{lastName},email=#{email},gender=#{gender},department_id=#{departmentId},birth=#{birth} where id=#{id}")
    int updateEmp(Employee employee);
    //删除员工id
    @Delete("delete  from employee where id=#{id}")
    void delete(Integer id);
}
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
5天前
|
Java
java springboot 8080端口号冲突时 修改当前项目端口号
java springboot 8080端口号冲突时 修改当前项目端口号
7 0
|
6天前
|
Java Maven 开发工具
IDEA使用Spring Initializr流畅的创建springboot项目
IDEA使用Spring Initializr流畅的创建springboot项目
24 0
|
1天前
|
Java 数据库连接 数据库
大事件后端项目05-----springboot整合mybatis
大事件后端项目05-----springboot整合mybatis
大事件后端项目05-----springboot整合mybatis
|
4天前
|
Java Maven 容器
springBoot项目导入外部jar包
springBoot项目导入外部jar包
11 4
|
10天前
|
消息中间件 Java Kafka
集成Kafka到Spring Boot项目中的步骤和配置
集成Kafka到Spring Boot项目中的步骤和配置
43 7
|
6天前
|
Java Maven
springboot项目打jar包后,如何部署到服务器
springboot项目打jar包后,如何部署到服务器
18 1
|
6天前
|
Java 程序员
浅浅纪念花一个月完成Springboot+Mybatis+Springmvc+Vue2+elementUI的前后端交互入门项目
浅浅纪念花一个月完成Springboot+Mybatis+Springmvc+Vue2+elementUI的前后端交互入门项目
19 1
|
10天前
|
监控 前端开发 Java
五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
|
1天前
|
Java Maven
大事件后端项目02----springboot工程创建
大事件后端项目02----springboot工程创建
|
1天前
|
前端开发 Java 网络架构
大事件后端项目01-----SpringBoot快速入门
大事件后端项目01-----SpringBoot快速入门