• springboot+Mybatis项目初始化


    1.准备数据库

    2.环境搭建

    image.png
    步骤:

    1. 准备数据库表(dept、emp)
    2. 创建springboot工程,引入对应的起步依赖(web、mybatis、mysql驱动、lombok)
    3. 配置文件application.properties中引入mybatis的配置信息,准备对应的实体类
    4. 准备对应的Mapper、Service(接口、实现类)、Controller基础结构

    (1)idea构建项目的初始化

    创建一个SpringBoot工程,选择引入对应的起步依赖(web、mybatis、mysql驱动、lombok) (版本选择2.7.5版本,可以创建完毕之后,在pom.xml文件中更改版本号)
    image.png
    image.png

    (2)目录结构

    image.png

    (3)application.properties配置文件

    第3步:配置文件application.properties中引入mybatis的配置信息,准备对应的实体类

    • application.properties (直接把之前项目中的复制过来)
    #数据库连接
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/tlias
    spring.datasource.username=root
    spring.datasource.password=1234
    
    #开启mybatis的日志输出
    mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
    
    #开启数据库表字段 到 实体类属性的驼峰映射
    mybatis.configuration.map-underscore-to-camel-case=true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.代码结构

    实体类(opjo)

    /*部门类*/
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Dept {
        private Integer id;
        private String name;
        private LocalDateTime createTime;
        private LocalDateTime updateTime;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    /*员工类*/
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Emp {
        private Integer id;
        private String username;
        private String password;
        private String name;
        private Short gender;
        private String image;
        private Short job;
        private LocalDate entrydate;
        private Integer deptId;
        private LocalDateTime createTime;
        private LocalDateTime updateTime;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    准备对应的Mapper、Service(接口、实现类)、Controller基础结构

    数据访问层(Mapper):

    • DeptMapper
    package com.itheima.mapper;
    import org.apache.ibatis.annotations.Mapper;
    
    @Mapper
    public interface DeptMapper {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • EmpMapper
    package com.itheima.mapper;
    import org.apache.ibatis.annotations.Mapper;
    
    @Mapper
    public interface EmpMapper {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    业务层(Service):

    • DeptService
    package com.itheima.service;
    
    //部门业务规则
    public interface DeptService {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • DeptServiceImpl
    package com.itheima.service.impl;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Service;
    
    //部门业务实现类
    @Slf4j
    @Service
    public class DeptServiceImpl implements DeptService {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • EmpService
    package com.itheima.service;
    
    //员工业务规则
    public interface EmpService {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • EmpServiceImpl
    package com.itheima.service.impl;
    import com.itheima.service.EmpService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Service;
    
    //员工业务实现类
    @Slf4j
    @Service
    public class EmpServiceImpl implements EmpService {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    控制层(Controller):

    • DeptController
    package com.itheima.controller;
    import org.springframework.web.bind.annotation.RestController;
    
    //部门管理控制器
    @RestController
    public class DeptController {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • EmpController
    package com.itheima.controller;
    import org.springframework.web.bind.annotation.RestController;
    
    //员工管理控制器
    @RestController
    public class EmpController {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    项目工程结构:

    image.png

  • 相关阅读:
    JDK中字体的高度信息ascent/descent/leading是怎么计算的
    python 异步线程 实现 异步生产 同步通信
    python使用pytest接口自动化测试的使用
    使用java解析hashMap
    Kubernetes (K8S) 1.24.3 For Ubuntu 安装
    python基础之面向对象
    SQL优化
    Python实现BS架构Flask的Web学生管理信息系统
    数字化转型与制造企业绿色创新质量——基于供需双侧机制的再检验(2011-2022年)
    pip常用源使用
  • 原文地址:https://blog.csdn.net/Azbtt/article/details/131142814