springboot+mybatis+thymeleaf+ajax+JQuery 技术整合搭建项目开发环境__02_技术落地(技术选型)
目前各类公司开始采用前后端分离模式开发
这种开发模式的好处:分工明确,职责分明,可以提供更好的系统使用体验。
后端追求的是:三高(高并发,高可用,高性能),安全,存储,业务等等。
前端追求的是:页面表现,速度流畅,兼容性,用户体验等等。
通过将整个开发团队分成前后端,让两边的工程师更加专注各自的领域,独立治理,然后构建出一个全栈式的精益求精的开发队伍
上面的介绍只是说明下本整合框架支持前后端分离。
这里我主要记录下后端的架构搭建————》
这里的整合开发框架 是基于SpringBoot整合其它模块而成的,那么每个模块选择的技术如下:
- SpringBoot版本选择目前较新的2.0.5.RELEASE版本
- 持久化框架选择 Mybatis
- 页面模板引擎选择 thymeleaf
- 前台框架选择 Bootstrap/Vue
- 后台框架选择 AdminLTE
- 数据库选择 Mysql
- 数据库版本管理选择 Flyway
- 前后端接口调用方式 RESTful
- 数据格式 JSON
- 前端事件请求 AJAX
- 页面的事件控制与样式处理 JQuery
代码分包(工程目录基础结构)
首先确定本整合框架名为springboot-backendframe
基础包名为:com.company.frame
主要分成3个模块,即用户管理及权限相关模块、具体业务相关模块和公共模块。为此这3个模块分别所属的包为auth、biz和common,即:
com.company.frame.auth
com.company.frame.biz
com.company.frame.common
然后每个模块下都是有本模块的controller、service、dao和domain,所以本工程包的结构如下:
根据前面所确定的技术,我们首先从整合springboot ,mybatis开始:
那么工程的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>
<groupId>com.cheri</groupId>
<artifactId>spring-boot-ajax</artifactId>
<version>1.0-SNAPSHOT</version>
<!--第一步 配置父依赖版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<!--第二步 配置默认属性 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!--第三步 当前工程的依赖配置 -->
<dependencies>
<!-- This is a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier>
</dependency>
<!-- 整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- springboot 整合 mybatis pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- springboot 整合 redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--使用cache缓存(不使用缓存时,每次访问都会连接数据库)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--整合 JPA-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--整合 activemq 消息中间件-->
<!-- activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<!-- 单元测试相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<!--API工具 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
<!--第四步 项目构建的配置:常用插件 -->
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<!-- 如果没有该项配置,devtools不会起作用,即应用不会restart -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceExcludes>src/main/resources/**</warSourceExcludes>
<warName>spring-boot-ajax</warName>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置 application.yml:
#热部署生效
spring.devtools.restart.enabled=true
# thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.mode=html5
spring.thymeleaf.servlet.content-type=text/html
# 数据源
server.port=8080
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.company.frame.auth.domain
# showSql
logging.level.com.company.frame.auth.dao=debug
UserMapper.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.company.frame.auth.dao.UserMapper">
<resultMap id="BaseResultMap" type="com.company.frame.auth.domain.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="age" jdbcType="VARCHAR" property="age" />
</resultMap>
<select id="selectUserById" resultType="com.company.frame.auth.domain.User">
select *
from user where id = #{id}
</select>
<select id="login" resultType="User">
SELECT * FROM user where name = #{param1} and password = #{param2}
</select>
<insert id="register" parameterType="User">
INSERT INTO user (name , password) VALUES (#{name}, #{password})
</insert>
<select id="selectAllUser" resultType="com.company.frame.auth.domain.User">
SELECT * FROM user
</select>
<update id="updateUser" parameterType="com.company.frame.auth.domain.User">
update user
<trim prefix="set" suffixOverrides=",">
<if test="user.name != null and user.name != '' ">
name = #{user.name},
</if>
<if test="user.password != null and user.password != ''">
password = #{user.password},
</if>
<if test="user.age != 0">
age = #{user.age}
</if>
</trim>
where id = #{user.id}
</update>
<delete id="deleteUserById" parameterType="int">
delete from user where id = #{id}
</delete>
</mapper>
执行clean 后 compile 没报错,项目构建成功》
简单的做下测测,看看整合成功了吗?
package com.company.frame;
import com.company.frame.auth.dao.UserMapper;
import com.company.frame.auth.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootBackendframeApplicationTests {
@Autowired
UserMapper userMapper;
@Test
public void contextLoads() {
}
@Test
public void mapperTest01() {
User user = userMapper.findUserById(44);
System.out.println(user);
}
}
Sringboot与mybatis整合成功了——》