核心技术
- 基础框架:SSM
- 分布式框架:SSM+Dubbo+zookeeper
- 图床:七牛云
- 后台权限控制:spring-security
- 后台管理模版:Thymeleaf
- 前端框架:Vue+axios
- JDK版本:1.8
搭建环境
模块规划
模块 |
描述 |
shf |
项目根目录,用来管理子模块 |
shf/common-util |
公共类模块 |
shf/model |
实体类模块 |
shf/web-admin |
后台管理系统 |
搭建项目结构
- 创建项目根目录shf,删除src目录只保留pom.xml
- 创建common-util、model、web-admin子模块,
整体项目结构:
shf ├── common-util ├── model ├── pom.xml └── web-admin
|
配置依赖关系
shf
编辑shf/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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.acaiblog</groupId> <artifactId>shf</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>common-util</module> <module>model</module> <module>web-admin</module> </modules>
<properties> <java.version>1.8</java.version> <spring.version>5.2.7.RELEASE</spring.version> <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version> <pagehelper.version>4.1.4</pagehelper.version> <servlet-api.version>2.5</servlet-api.version> <fastjson.version>1.2.29</fastjson.version> <mybatis.version>3.4.5</mybatis.version> <mybatis.spring.version>1.3.1</mybatis.spring.version> <mysql.version>8.0.18</mysql.version> <druid.version>1.1.12</druid.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <slf4j-version>1.7.30</slf4j-version> <logback-version>1.2.3</logback-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-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-test</artifactId> <version>${spring.version}</version> </dependency>
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>${thymeleaf.version}</version> </dependency>
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.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>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j-version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback-version}</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
</project>
|
common-util
编辑common-util/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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.acaiblog</groupId> <artifactId>shf</artifactId> <version>1.0-SNAPSHOT</version> </parent>
<artifactId>common-util</artifactId>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <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-tx</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency>
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency>
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> </dependencies> </project>
|
web-admin
编辑web-admin/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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.acaiblog</groupId> <artifactId>shf</artifactId> <version>1.0-SNAPSHOT</version> </parent>
<artifactId>web-admin</artifactId> <packaging>war</packaging>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.acaiblog</groupId> <artifactId>common-util</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.acaiblog</groupId> <artifactId>model</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.15.v20190215</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webAppConfig> <contextPath>/</contextPath> </webAppConfig> <httpConnector> <port>8000</port> </httpConnector> </configuration> </plugin> </plugins> </build>
</project>
|
配置SSM环境
主要操作web-admin子模块
在resources中添加日志配置shf/web-admin/src/main/resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false">
<property name="LOG_HOME" value="logs" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> </appender>
<root level="DEBUG"> <appender-ref ref="STDOUT" /> </root> </configuration>
|
在resources中添加mybatis配置文件shf/web-admin/src/main/resources/mybatis-config.xml
<?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="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
|
在resource/spring目录添加spring dao配置文件shf/web-admin/src/main/resources/spring/spring-dao.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" xmlns:spring-mybatis="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="username" value="root" /> <property name="password" value="root" /> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/db_house?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.atguigu.entity"></property> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations"> <array> <value>classpath:mapper/*.xml</value> </array> </property> </bean> <spring-mybatis:scan base-package="com.atguigu.dao"></spring-mybatis:scan>
</beans>
|
在resource/spring目录添加spring service配置文件shf/web-admin/src/main/resources/spring/spring-service.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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.atguigu.service" ></context:component-scan>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
<tx:annotation-driven transaction-manager="transactionManager"/> </beans>
|
在resources/spring目录中添加controller顶层配置文件shf/web-admin/src/main/resources/spring/spring-mvc.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.atguigu.controller" />
<mvc:default-servlet-handler/> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/WEB-INF/templates/"></property> <property name="suffix" value=".html"></property> <property name="characterEncoding" value="UTF-8"></property> <property name="cacheable" value="false"></property>
<property name="templateMode" value="LEGACYHTML5"></property> </bean> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="characterEncoding" value="UTF-8"></property> <property name="templateEngine" ref="templateEngine"/> </bean> <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver"></property> </bean> </beans>
|
创建文件shf/web-admin/src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>web</display-name>
<filter> <filter-name>encode</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encode</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
|
测试
创建house数据库,并执行脚本: db_house.sql
model模块
添加BaseEntity
实体类
package com.acaiblog.entiry;
import java.io.Serializable;
public class BaseEntity implements Serializable { private Long id; private Date createTime; private Date updateTime; private Integer isDeleted;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public Date getCreateTime() { return createTime; }
public void setCreateTime(Date createTime) { this.createTime = createTime; }
public Date getUpdateTime() { return updateTime; }
public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; }
public Integer getIsDeleted() { return isDeleted; }
public void setIsDeleted(Integer isDeleted) { this.isDeleted = isDeleted; } }
|
添加实体类Role
package com.acaiblog.entiry;
public class Role extends BaseEntity { private static final long serialVersionUID = 1L;
private String roleName; private String roleCode; private String description;
public void setRoleName(String value) { this.roleName = value; }
public String getRoleName() { return this.roleName; }
public void setRoleCode(String value) { this.roleCode = value; }
public String getRoleCode() { return this.roleCode; }
public void setDescription(String value) { this.description = value; }
public String getDescription() { return this.description; } }
|
web-admin模块
添加查询接口
package com.acaiblog.dao;
import com.acaiblog.entiry.Role;
import java.util.List;
public interface RoleDao { List<Role> findAll(); }
|
创建映射文件shf/web-admin/src/main/resources/mapper/RoleDao.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.dao.RoleDao">
<sql id="columns"> select id,role_name,role_code,description,create_time,update_time,is_deleted </sql>
<select id="findAll" resultType="role"> <include refid="column"></include> from acl_role where is_deleted = 0 </select>
</mapper>
|
在shf/web-admin/src/main/java/com/acaiblog/service/RoleService.java
添加service
package com.acaiblog.Service; import java.util.List; import com.acaiblog.entiry.Role;
public interface RoleService { List<Role> findAll(); }
|
在shf/web-admin/src/main/java/com/acaiblog/service/impl/RoleServiceImpl.java
添加service实现
package com.acaiblog.service.impl;
import com.acaiblog.Service.RoleService; import com.acaiblog.dao.RoleDao; import com.acaiblog.entiry.Role; import org.springframework.beans.factory.annotation.Autowired; import java.util.List;
public class RoleServiceImpl implements RoleService { @Autowired private RoleDao roleDao;
@Override public List<Role> findAll() { return roleDao.findAll(); } }
|
在添加RoleController
package com.acaiblog.controller;
import com.acaiblog.Service.RoleService; import com.acaiblog.entiry.Role; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List; import java.util.Map;
@Controller @RequestMapping("/role") public class RoleController { @Autowired private RoleService roleService; @RequestMapping public String index(Map map){
List<Role> roleList = roleService.findAll();
map.put("list",roleList);
return "role/index"; } }
|
创建角色shf/web-admin/src/main/webapp/WEB-INF/templates/role/index.html
页面
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table> <tr th:each="item,it : ${list}"> <td class="text-center" th:text="${it.count}">11</td> <td th:text="${item.roleName}">22</td> <td th:text="${item.roleCode}">33</td> <td th:text="${item.description}">33</td> <td th:text="${#dates.format(item.createTime,'yyyy-MM-dd HH:mm:ss')}" >33</td> </tr> </table> </body> </html>
|