1.项目背景
2.技术选择
3.环境搭建
1.创建空项目
创建health_parent父文件用来控制依赖,类型为quickStart
打包方式为,pom:用在父级工程或聚合工程中,用来做jar包的版本控制,必须指明这个聚合工程的打包方式为pom。
依赖:
<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.example</groupId><artifactId>HAMAS2.0</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>health_parent</artifactId><packaging>pom</packaging><name>health_parent</name><url>http://maven.apache.org</url><modules><module>health_common</module><module>health_mobile</module><module>health_backend</module></modules><properties><!--JDK的编译环境 JDK17--><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--Spring框架6版本,JDK必须17或者更高--><spring.version>6.0.9</spring.version><!--MyBatis的版本--><mybatis.version>3.5.5</mybatis.version><!--MyBatis和Spring框架的整合jar包--><mybatis-spring.version>3.0.3</mybatis-spring.version><!-- <mybatis.paginator.version>1.2.15</mybatis.paginator.version>--><!--MyBatis分页助手--><pagehelper.version>4.1.4</pagehelper.version><mysql.version>8.0.33</mysql.version><druid.version>1.1.10</druid.version><commons-fileupload.version>1.3.1</commons-fileupload.version><!--Spring security安全认证框架--><spring.security.version>6.0.8</spring.security.version><jedis.version>2.7.0</jedis.version><!--Apache POI 读写Excel jar--><poi.version>3.17</poi.version><!--Spring的定时器 jar包--><quartz.version>2.2.1</quartz.version><!--Json序列化和反序列化的jar--><jackson.version>2.17.2</jackson.version><servlet.version>5.0.0</servlet.version><log.version>1.2.17</log.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>${mysql.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis-spring.version}</version></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>${pagehelper.version}</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>${commons-fileupload.version}</version></dependency><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>${quartz.version}</version></dependency><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz-jobs</artifactId><version>${quartz.version}</version></dependency><!--上传文件的第三方服务器 jar--><dependency><groupId>com.qiniu</groupId><artifactId>qiniu-java-sdk</artifactId><version>7.2.0</version></dependency><!--POI报表--><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>${poi.version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi.version}</version></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>${jedis.version}</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.7.0</version></dependency><!-- 安全框架 --><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId><version>${spring.security.version}</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId><version>${spring.security.version}</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-taglibs</artifactId><version>${spring.security.version}</version></dependency><!--第三方的验证码,随机数字放在图片中,展示图片--><dependency><groupId>com.github.penggle</groupId><artifactId>kaptcha</artifactId><version>2.3.2</version><exclusions><exclusion><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId></exclusion></exclusions></dependency><!--读取xml文件--><dependency><groupId>dom4j</groupId><artifactId>dom4j</artifactId><version>1.6.1</version></dependency><dependency><groupId>xml-apis</groupId><artifactId>xml-apis</artifactId><version>1.4.01</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId><version>${servlet.version}</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log.version}</version></dependency></dependencies></dependencyManagement><build><plugins><!--Maven的插件:打war包插件--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.3.2</version></plugin></plugins></build>
</project>
web.xml
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app><display-name>Archetype Created Web Application</display-name><!--配置SpringMVC的请求控制--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--添加初始化数据,读取spring的配置文件--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-*.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>
</web-app>
创建health_common,里面放置各种静态公共类,类型为java或者quiclstart例如pojo,返回值类,工具类等;
打包方式为:jar,工程的默认打包方式,打包成jar用作jar包使用。存放一些其他工程都会使用的类,工具类。我们可以在其他工程的pom文件中去引用它。
依赖:
<?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.example</groupId><artifactId>health_parent</artifactId><version>1.0-SNAPSHOT</version></parent><groupId>com.home</groupId><artifactId>health_common</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId></dependency><!-- Mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId></dependency><!-- MySql --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency><!-- 连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId></dependency><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-dysmsapi</artifactId><version>1.0.0</version></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency><dependency><groupId>com.qiniu</groupId><artifactId>qiniu-java-sdk</artifactId></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-taglibs</artifactId></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.10</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency></dependencies>
</project>
创建health_backend,即企业后端,使用webapp模板创建maven项目模块;
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.example</groupId><artifactId>health_parent</artifactId><version>1.0-SNAPSHOT</version></parent><groupId>com.home</groupId><artifactId>health_backend</artifactId><packaging>war</packaging><name>health_backend Maven Webapp</name><url>https://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version></dependency><dependency><groupId>com.home</groupId><artifactId>health_common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId><scope>provided</scope></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies><build><finalName>health_backend</finalName></build>
</project>
创建health_mobil,即手机端,使用webapp模板创建maven项目模块;
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.example</groupId><artifactId>health_parent</artifactId><version>1.0-SNAPSHOT</version></parent><groupId>com.home</groupId><artifactId>health_mobile</artifactId><packaging>war</packaging><name>health_mobile Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version></dependency><dependency><groupId>com.home</groupId><artifactId>health_common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId><scope>provided</scope></dependency></dependencies><build><finalName>health_mobile</finalName></build>
</project>
打包方式为: war,将会打包成war,发布在服务器上,如网站或服务。用户可以通过浏览器直接访问,或者是通过发布服务被别的工程调用;
2.配置文件
在health_backend的resources中配置多个配置文件
1.controller层对应的spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
"><!--包扫描 controller--><context:component-scan base-package="com.home.controller"/><!-- 用 Spring MVC 的注解驱动功能 --><mvc:annotation-driven><!-- 设置路径匹配模式为“后缀匹配”--><mvc:path-matching suffix-pattern="true"/></mvc:annotation-driven>
</beans>
2.Services层对应的spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
"><!--配置包扫描,service层--><context:component-scan base-package="com.home.service"/><!--配置事务管理--><bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"><!--注入连接池--><property name="dataSource" ref="dataSource"/></bean><!--开启事务的注解驱动--><tx:annotation-driven transaction-manager="transactionManager"/></beans>
3.Dao/mapper层的spring-mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置和MyBatis整合相关和数据库相关--><!--配置druid连接池--><bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/db1"/><property name="username" value="root"/><property name="password" value="123456"/></bean><!--配置整合类 MyBatis和Spring整合 SqlSessionFactoryBean --><bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactoryBean"><!--注入连接池对象--><property name="dataSource" ref="dataSource"/><!--配置批量包扫描 数据表和Java类对应关系--><property name="typeAliasesPackage" value="com.home.pojo"/><!--配置MyBatis的主配置文件--><property name="configLocation" value="classpath:SqlMapConfig.xml"/></bean><!--配置包扫描,扫描MyBatis的接口包mapper--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer"><property name="basePackage" value="com.home.mapper"/><!--注入SqlSessionFactoryBean整个对象--><property name="sqlSessionFactoryBeanName" value="sessionFactoryBean"/></bean>
</beans>
4.开启数据库日志
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><setting name="logImpl" value="LOG4J"/></settings><plugins><!-- com.github.pagehelper 为 PageHelper 类所在包名 --><plugin interceptor="com.github.pagehelper.PageHelper"><property name="dialect" value="mysql"/></plugin></plugins>
</configuration>
5.Mybatis核心配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><setting name="logImpl" value="LOG4J"/></settings><plugins><!-- com.github.pagehelper 为 PageHelper 类所在包名 --><plugin interceptor="com.github.pagehelper.PageHelper"><property name="dialect" value="mysql"/></plugin></plugins>
</configuration>
3.在common中导入pojo和返回值等共用类
项目中规定返回值类型,为判断成功标识flag、带回数据data、和message提示信息;
package com.home.entiry;
import java.io.Serializable;
public class Result implements Serializable {private boolean flag;//执行结果,true为执行成功 false为执行失败private String message;//返回提示信息,主要用于页面提示信息private Object data;//返回数据public Result(boolean flag, String message) {this.flag = flag;this.message = message;}public Result(boolean flag, String message, Object data) {this.flag = flag;this.message = message;this.data = data;}public boolean isFlag() {return flag;}public void setFlag(boolean flag) {this.flag = flag;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}
}
注:类似html、js、css、图片等静态资源,放到health_backend工程中
4.将配置好的文件上传至gitee
配置好环境后,上传环境以免损坏,再次开发时,使用新建分支进行开发,保证原项目不损坏,
分支项目测试完成没问题即可合并分支;