当前位置:   article > 正文

SpringBoot基础流程_基于springboot框架的web软件系统的基本工作(业务)流程

基于springboot框架的web软件系统的基本工作(业务)流程

Spring Boot是一个全新的Java软件开发框架,其设计目的是用来简化Spring项目的初始搭建以及开发过程,并为后面的Spring Cloud 微服务实践提供更加便利条件。该框架使用了特定的注解方式来进行配置,从而使开发人员不再需要大量的xml配置。不再需要大量的手动依赖管理。Spring Boot基于快速构建理念,通过约定大于配置,开箱即用的方式,希望能够在蓬勃发展的快速应用开发领域成为其领导者。

SpringBoot Web服务基本流程为:

  1. 数据库建表,建立DO
  2. MyBatis配置Dao层的Mapper类,配置对应的xml将sql与Dao进行映
  3. Service层建立需要控制的Model实体,调用Dao层的接口提供服务
  4. Controller层调用Service层提供的数据服务,必要的话创建VO仅返回给前端有限的信息

整体目录架构为:

启动类

通常以项目名称为启动类,通过SpringApplication.run(Application.class, args); 方式启动SpringBoot应用程序

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. @SpringBootApplication
  4. public class MuddleApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(MuddleApplication.class, args);
  7. }
  8. }
  9. 复制代码

SpringBoot会自动启动配置的相应的服务,如内置的Tomcat

配置文件

SpringBoot支持一系列组件的配置,只需要在resource目录下编写配置文件即可,默认的配置文件为application.properties

  1. # 应用服务 WEB 访问端口
  2. server.port=8080
  3. # MyBatis配置
  4. # 指定xml映射路径,对应为resources/mapping/*.xml
  5. mybatis.mapper-locations = classpath:mapping/*.xml
  6. # 数据库url
  7. spring.datasource.url=jdbc:mysql://localhost:3306/miaosha?characterEncoding=utf8&serverTimezone=UTC
  8. # 数据库用户名
  9. spring.datasource.username=root
  10. # 数据库密码
  11. spring.datasource.password=123456
  12. # 数据库驱动
  13. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  14. # 使用druid
  15. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  16. 复制代码

控制层Controller

使用@Controller标注,提供针对url的处理与返回的具体信息,调用服务层Service提供的服务接口

  1. 在主应用程序上添加@RestController 注解,添加控制层控制,表明提供Restful API服务
  2. 在控制层API的类上添加@Controller注解,表明这是一个控制层的Bean,系统启动时会自动实例化该类
  3. 在控制层API(@Controller)的方法上添加@RequestMapping 注解,用于将请求与处理方法一一对应
  1. @SpringBootApplication
  2. @RestController
  3. public class MuddleApplication {
  4. @RequestMapping
  5. public String home(){
  6. return "hello";
  7. }
  8. public static void main(String[] args) {
  9. SpringApplication.run(MuddleApplication.class, args);
  10. }
  11. }
  12. 复制代码

访问指定路径会自动映射url并访问对应方法提供的service

针对具体的业务对应的url需要单独的Controller返回信息,控制层通过调用服务返回结果信息

  1. @Controller
  2. @RequestMapping("/user")
  3. public class UserController {
  4. @Autowired
  5. private UserService userService;
  6. @RequestMapping("/get")
  7. @ResponseBody
  8. public UserModel getUser(@RequestParam(name = "id") Integer id) {
  9. UserModel userModel = userService.getUserById(id);
  10. return userModel;
  11. }
  12. }
  13. 复制代码

之后访问指定url即可获取返回信息

  1. http://localhost:8080/user/get?id=1
  2. {"id":1,"name":"第一个用户","gender":1,"age":30,"telphone":"13521234859","registerMode":"byphone","thirdPartyId":"","encrptPassword":"ddlsjfjfjfjlf"}
  3. 复制代码

在更细一层的企业级代码中,通常不会直接返回给前端数据Model,Controller层还需要做一层VO,仅返回前端需要展示的数据即可

用户显示信息实体

  1. public class UserVO {
  2. private Integer id;
  3. private String name;
  4. private Byte gender;
  5. private Integer age;
  6. private String telphone;
  7. }
  8. 复制代码

对应的Controller做一层转换即可

  1. @Controller
  2. @RequestMapping("/user")
  3. public class UserController {
  4. @Autowired
  5. private UserService userService;
  6. @RequestMapping("/get")
  7. @ResponseBody
  8. public UserVO getUser(@RequestParam(name = "id") Integer id) {
  9. UserModel userModel = userService.getUserById(id);
  10. return convertFromModel(userModel);
  11. }
  12. private UserVO convertFromModel(UserModel userModel) {
  13. if (userModel == null) {
  14. return null;
  15. }
  16. UserVO userVO = new UserVO();
  17. BeanUtils.copyProperties(userModel, userVO);
  18. return userVO;
  19. }
  20. }
  21. 复制代码

服务层Service

使用@Service标注,服务层包含服务接口与其具体的实现impl,通常提供的就是对数据实体的增删查改服务

在服务接口中定义服务API

  1. public interface UserService {
  2. void getUserById(Integer id);
  3. }
  4. 复制代码

在对应的impl中实现服务接口

  1. @Service
  2. public class UserServiceImpl implements UserService {
  3. @Autowired
  4. private UserDOMapper userDOMapperl;
  5. @Override
  6. public void getUserById(Integer id) {
  7. UserDO userDo = userDOMapperl.selectByPrimaryKey(id);
  8. }
  9. }
  10. 复制代码

在企业级代码中,与数据库表对应的实体DO不能透传给前端,前端返回的信息实体应该单独定义,也就是与系统交互的Model,该Model可以是多个数据库表信息组合而成的数据模型

服务层操控的数据实体

  1. public class UserModel {
  2. private Integer id;
  3. private String name;
  4. private Byte gender;
  5. private Integer age;
  6. private String telphone;
  7. private String registerMode;
  8. private String thirdPartyId;
  9. private String encrptPassword;
  10. // getter与setter
  11. }
  12. 复制代码

该Model可以由多个DO组合而成,因此需要定义单独的方法将从数据库中查询出来的DO组装成Model并返回,此时用户服务接口为

  1. public interface UserService {
  2. UserModel getUserById(Integer id);
  3. }
  4. 复制代码

服务实现为

  1. @Service
  2. public class UserServiceImpl implements UserService {
  3. @Autowired
  4. private UserDOMapper userDOMapperl;
  5. @Autowired
  6. private UserPasswordDOMapper userPasswordDOMapper;
  7. /**
  8. * 返回用户信息
  9. *
  10. * @param id 用户id
  11. * @return 用户信息与密码
  12. */
  13. @Override
  14. public UserModel getUserById(Integer id) {
  15. UserDO userDo = userDOMapperl.selectByPrimaryKey(id);
  16. if (userDo == null) {
  17. return null;
  18. }
  19. // 获取用户密码
  20. UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDo.getId());
  21. return convertFromDataObject(userDo, userPasswordDO);
  22. }
  23. private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) {
  24. if (userDO == null) {
  25. return null;
  26. }
  27. UserModel userModel = new UserModel();
  28. BeanUtils.copyProperties(userDO, userModel);
  29. if (userPasswordDO != null) {
  30. userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
  31. }
  32. return userModel;
  33. }
  34. }
  35. 复制代码

该服务的API就可以交给Controller层使用

持久层DAO

配合持久层框架使用,如常用的MyBatis,以MyBatis为例

  1. 首先创建与数据库表对应的数据实体DO,
  2. 然后创建与每个DO对应的mapper类,提供增删查改,在DAO目录下存放
  3. 创建与每个mapper对应的xml文件,配置sql语句信息,在mapper目录下存放

用户信息实体:

  1. public class UserPasswordDO {
  2. private Integer id;
  3. private String encrptPassword;
  4. private Integer userId;
  5. // getter与setter
  6. }
  7. 复制代码

对应的dao,dao中的方法与mapper.xml中的id相对应

  1. @Repository
  2. public interface UserPasswordDOMapper {
  3. int deleteByPrimaryKey(Integer id);
  4. int insert(UserPasswordDO row);
  5. int insertSelective(UserPasswordDO row);
  6. UserPasswordDO selectByPrimaryKey(Integer id);
  7. UserPasswordDO selectByUserId(Integer userId);
  8. int updateByPrimaryKeySelective(UserPasswordDO row);
  9. int updateByPrimaryKey(UserPasswordDO row);
  10. }
  11. 复制代码

对应的mapper.xml

  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="lsl.learning.muddle.dao.UserPasswordDOMapper">
  4. <resultMap id="BaseResultMap" type="lsl.learning.muddle.dataobject.UserPasswordDO">
  5. <id column="id" jdbcType="INTEGER" property="id" />
  6. <result column="encrpt_password" jdbcType="VARCHAR" property="encrptPassword" />
  7. <result column="user_id" jdbcType="INTEGER" property="userId" />
  8. </resultMap>
  9. <sql id="Base_Column_List">
  10. id, encrpt_password, user_id
  11. </sql>
  12. <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  13. select
  14. <include refid="Base_Column_List" />
  15. from user_password
  16. where id = #{id,jdbcType=INTEGER}
  17. </select>
  18. <!--通过user id查询-->
  19. <select id="selectByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  20. select
  21. <include refid="Base_Column_List" />
  22. from user_password
  23. where user_id = #{userId,jdbcType=INTEGER}
  24. </select>
  25. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  26. delete from user_password
  27. where id = #{id,jdbcType=INTEGER}
  28. </delete>
  29. <insert id="insert" parameterType="lsl.learning.muddle.dataobject.UserPasswordDO">
  30. insert into user_password (id, encrpt_password, user_id
  31. )
  32. values (#{id,jdbcType=INTEGER}, #{encrptPassword,jdbcType=VARCHAR}, #{userId,jdbcType=INTEGER}
  33. )
  34. </insert>
  35. <insert id="insertSelective" parameterType="lsl.learning.muddle.dataobject.UserPasswordDO">
  36. insert into user_password
  37. <trim prefix="(" suffix=")" suffixOverrides=",">
  38. <if test="id != null">
  39. id,
  40. </if>
  41. <if test="encrptPassword != null">
  42. encrpt_password,
  43. </if>
  44. <if test="userId != null">
  45. user_id,
  46. </if>
  47. </trim>
  48. <trim prefix="values (" suffix=")" suffixOverrides=",">
  49. <if test="id != null">
  50. #{id,jdbcType=INTEGER},
  51. </if>
  52. <if test="encrptPassword != null">
  53. #{encrptPassword,jdbcType=VARCHAR},
  54. </if>
  55. <if test="userId != null">
  56. #{userId,jdbcType=INTEGER},
  57. </if>
  58. </trim>
  59. </insert>
  60. <update id="updateByPrimaryKeySelective" parameterType="lsl.learning.muddle.dataobject.UserPasswordDO">
  61. update user_password
  62. <set>
  63. <if test="encrptPassword != null">
  64. encrpt_password = #{encrptPassword,jdbcType=VARCHAR},
  65. </if>
  66. <if test="userId != null">
  67. user_id = #{userId,jdbcType=INTEGER},
  68. </if>
  69. </set>
  70. where id = #{id,jdbcType=INTEGER}
  71. </update>
  72. <update id="updateByPrimaryKey" parameterType="lsl.learning.muddle.dataobject.UserPasswordDO">
  73. update user_password
  74. set encrpt_password = #{encrptPassword,jdbcType=VARCHAR},
  75. user_id = #{userId,jdbcType=INTEGER}
  76. where id = #{id,jdbcType=INTEGER}
  77. </update>
  78. </mapper>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/57618
推荐阅读
相关标签
  

闽ICP备14008679号