从零搭建自己的SpringBoot后台框架(一)

秒速五厘米 2023-08-17 15:29 185阅读 0赞

项目简介

框架简介

框架为springboot+mybatis项目,支持多数据源;整合通用mapper;整合json Web Token加密;支持aop记录用户操作日志;整合代码生成插件,自动生成增删改查等基础代码;微信支付;发送邮件;图片压缩水印;支持动态定时任务;统一异常处理;请求结果的封装等;

框架结构

format_png

aop文件夹中是自定义注解用于记录用户操作日志

configurer文件夹中是一些配置文件,如mybatis分页插件的配置等

constant文件夹中是一些常量的定义,如微信支付常量,发送短信需要的一些常量等

generic文件夹中是自定义一些顶级通用接口

ret文件夹中是自定义请求结果格式和枚举请求码

startupRunner文件夹中是当服务器启动成功后执行的方法

tasks文件中为定时任务

test中CodeGenerstor为代码生产器

template中为生成代码的模板

一:通过idea工具构建基础框架

  1. 打开idea,左上角File→New→Project,看到如下图的页面

format_png 1

  1. 点击Next,配置如下图format_png 2
  2. 点击Next,配置如下图,这里我们选择数据库MySQL和持久层框架MyBatisformat_png 3
  3. 点击Next,选择工作目录,点击Finish,开始构建
  1. 创建完成后,项目目录结构如下

format_png 4

DemoApplicttion.java为项目的启动类

application.properties为项目配置文件

pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和licenses,以及其他所有的项目相关因素,是项目级别的配置文件(说人话:项目所需jar包的管理)

二:配置数据库信息

在application.properties文件中添加如下数据库配置

  1. spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false
  2. spring.datasource.username=数据库用户名
  3. spring.datasource.password=数据库密码
  4. spring.datasource.driverClassName=com.mysql.jdbc.Driver

三:创建数据库UserInfo表

  1. CREATE TABLE `user_info` (
  2. `id` int(32) NOT NULL AUTO_INCREMENT,
  3. `user_name` varchar(255) DEFAULT NULL,
  4. PRIMARY KEY (`id`)
  5. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

四:创建项目基本目录结构

format_png 5

Model

  1. package com.example.demo.model;
  2. import javax.persistence.Column;
  3. import javax.persistence.Id;
  4. /**
  5. * @author phubing
  6. * @Description:
  7. * @time 2019/4/18 11:55
  8. */
  9. public class UserInfo {
  10. /**
  11. * 主键
  12. */
  13. @Id
  14. private String id;
  15. /**
  16. * 用户名
  17. */
  18. @Column(name = "user_name")
  19. private String userName;
  20. private String password;
  21. public String getId() {
  22. return id;
  23. }
  24. public void setId(String id) {
  25. this.id = id;
  26. }
  27. public String getUserName() {
  28. return userName;
  29. }
  30. public void setUserName(String userName) {
  31. this.userName = userName;
  32. }
  33. public String getPassword() {
  34. return password;
  35. }
  36. public void setPassword(String password) {
  37. this.password = password;
  38. }
  39. }

Mapper

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.demo.dao.UserInfoMapper">
  4. <resultMap id="BaseResultMap" type="com.example.demo.model.UserInfo">
  5. <id column="id" jdbcType="INTEGER" property="id"/>
  6. <result column="user_name" jdbcType="VARCHAR" property="userName"/>
  7. </resultMap>
  8. <sql id="Base_Column_List">
  9. id,user_name
  10. </sql>
  11. <select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  12. select
  13. <include refid="Base_Column_List"/>
  14. from user_info
  15. where id = #{id,jdbcType=VARCHAR}
  16. </select>
  17. </mapper>

Dao

  1. package com.example.demo.dao;
  2. import com.example.demo.model.UserInfo;
  3. import org.apache.ibatis.annotations.Param;
  4. /**
  5. * @author phubing
  6. * @Description:
  7. * @time 2019/4/18 11:55
  8. */
  9. public interface UserInfoMapper {
  10. UserInfo selectById(@Param("id") Integer id);
  11. }

Service

  1. package com.example.demo.service;
  2. import com.example.demo.model.UserInfo;
  3. /**
  4. * @author phubing
  5. * @Description:
  6. * @time 2019/4/18 11:55
  7. */
  8. public interface UserInfoService {
  9. UserInfo selectById(Integer id);
  10. }

ServiceImpl

  1. package com.example.demo.service.impl;
  2. import com.example.demo.dao.UserInfoMapper;
  3. import com.example.demo.model.UserInfo;
  4. import com.example.demo.service.UserInfoService;
  5. import org.springframework.stereotype.Service;
  6. import javax.annotation.Resource;
  7. /**
  8. * @author phubing
  9. * @Description:
  10. * @time 2019/4/18 11:55
  11. */
  12. @Service
  13. public class UserInfoServiceImpl implements UserInfoService{
  14. @Resource
  15. private UserInfoMapper userInfoMapper;
  16. public UserInfo selectById(Integer id){
  17. return userInfoMapper.selectById(id);
  18. }
  19. }

Controller

  1. package com.example.demo.controller;
  2. import com.example.demo.model.UserInfo;
  3. import com.example.demo.service.UserInfoService;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import javax.annotation.Resource;
  8. /**
  9. * @author phubing
  10. * @Description:
  11. * @time 2019/4/18 11:55
  12. */
  13. @RestController
  14. @RequestMapping("userInfo")
  15. public class UserInfoController {
  16. @Resource
  17. private UserInfoService userInfoService;
  18. @PostMapping("/hello")
  19. public String hello(){
  20. return "hello SpringBoot";
  21. }
  22. @PostMapping("/selectById")
  23. public UserInfo selectById(Integer id){
  24. return userInfoService.selectById(id);
  25. }
  26. }

Controller中@RestController注解的作用

@RestController是由@Controller和@ResponseBody组成,表示该类是controller和返回的结果为JSON数据,不是页面路径。

重点看一下configurer中的MyBatisConfigurer.java

  1. package com.example.demo.core.configurer;
  2. import org.apache.ibatis.session.SqlSessionFactory;
  3. import org.mybatis.spring.SqlSessionFactoryBean;
  4. import org.mybatis.spring.mapper.MapperScannerConfigurer;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  8. import org.springframework.core.io.support.ResourcePatternResolver;
  9. import javax.sql.DataSource;
  10. /**
  11. * @author phubing
  12. * @Description:
  13. * @time 2019/4/18 11:55
  14. */
  15. @Configuration
  16. public class MybatisConfigurer {
  17. @Bean
  18. public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
  19. SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
  20. factory.setDataSource(dataSource);
  21. factory.setTypeAliasesPackage("com.example.demo.model");
  22. // 添加XML目录
  23. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  24. factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
  25. return factory.getObject();
  26. }
  27. @Bean
  28. public MapperScannerConfigurer mapperScannerConfigurer() {
  29. MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
  30. mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
  31. mapperScannerConfigurer.setBasePackage("com.example.demo.dao");
  32. return mapperScannerConfigurer;
  33. }
  34. }

@Configuration表示该文件是一个配置文件

@Bean表示该方法是一个传统xml配置文件中的

其中factory.setTypeAliasesPackage(“com.example.demo.model”)表示项目中model的存储路径;

factory.setMapperLocations(resolver.getResources(“classpath:mapper/*.xml”));表示mapper.xml存储路径;

mapperScannerConfigurer.setBasePackage(“com.example.demo.dao”);表示dao层的存储路径

五:运行项目

找到DemoApplication,右键,选择run DemoApplication

控制台打印如下信息即为启动成功

format_png 6

六:测试接口

打开postman(Google浏览器插件,可以去应用商店下载),输入

format_png 7

注意修改自己的 IP ;出现以下结构数据即访问成功

  1. {
  2. "id": 1,
  3. "userName": "1"
  4. }

#

发表评论

表情:
评论列表 (有 0 条评论,185人围观)

还没有评论,来说两句吧...

相关阅读