SpringBoot筆記(六):SpringBoot開發Maven多模塊項目

程序員社區 2022-01-07 15:22:35 阅读数:584

springboot springboot maven

前言

       該Demo使用SpringBoot,基於注解方式來開發基於Maven管理的多模塊項目。僅僅算是一個簡單的模板,方便大家參考學習的同時,也方便自己後續的直接拿來上手。

       該項目用到:SpringBoot(2.1.5.RELEASE)、SpringMVC、MyBatis、Druid連接池、Maven、MySQL、Thymeleaf等。開發工具使用的是:Intellij IDEA

項目介紹

        項目主要分為5個模塊,分別是:①report-dao   ②report-pojo   ③report-service   ④report-utils   ⑤report-web

        ①通過maven<profile>標簽,將該項目分為了develop、test、pre-release、release四個環境,方便打包上線時不同環境數據庫參數不同而修改的情况;多數據源配置在application.yml中,通過@[email protected]來獲取<profile>配置的數據

        ②從架構方向考慮,pom.xml盡量做到了最優。可以通過Maven命令:mvn dependency:tree 來查看依賴關系

        ③引入MyBatis自動生成代碼插件;

        ④引入多個數據源,通過druid連接池來管理;

        ⑤*.html後綴管理,目前通過@RequestMapping("xxx.html")來唯一確定綴的,使用yml或者Config.java來配置,雖然滿足了後綴管理,但是靜態資源無法加載,目前沒找到好的辦法,後續繼續研究,你們有什麼方法嗎?我們可以一起研討,謝謝;

        ⑥SpringBoot不建議使用JSP來開發,推薦使用Thymeleaf、FreeMarker等。本項目配置支持Thymeleaf開發;

        ⑦項目添加Intellij IDEA熱部署支持,滿足開發者實時調試。如需配置,可參考:3.Springboot項目在Intellij IDEA中實現熱部署 

        ⑧還有AOP,事務,Redis等其他常用的,後續還會陸續加入使用

        彼此之間的依賴關系:web依賴service,service依賴dao,dao依賴pojo和until,因為Maven依賴傳遞,所以web,service等也會間接依賴pojo和until模塊。如下為結構圖:

POM設計

       為了盡量降低各個模塊彼此之間的聯系,所有父pom使用了<dependencyManagement>來管理項目中使用到的大部分jar包。

       如果你不知道該標簽是用來幹嘛的呢,那麼這裏就簡單來說一下,主要有兩點:

       ①該標簽主要從來管理jar包,在父pom中定義的該標簽,你會發現好多沒用的jar包都在父pom中定義了,太繁瑣占空間。切記僅僅只是一個聲明罷了,此處並不會使用到這些jar包。

       ②父pom使用該標簽,直接聲明版本號。在子pom中使用時,則不需要再聲明版本號,僅僅<grupId>和<artifactedId>即可。它會自動使用過父pom使用的版本號。方便以後技術版本的統一變更,這就是為什麼要用該標簽。

        總結:就是方便統一管理。

<!-- 1.父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.demo</groupId> <artifactId>bi-report</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>report-dao</module> <module>report-service</module> <module>report-pojo</module> <module>report-web</module> <module>report-utils</module> </modules> <!-- 父pom對很多jar都進行了聲明,僅僅只是聲明而已,方便後續的管理--> <properties> <springboot.version>2.1.5.RELEASE</springboot.version> <springboot.log4j.version>1.3.8.RELEASE</springboot.log4j.version> <springboot.mybatis.version>1.3.0</springboot.mybatis.version> <mysql.connector.version>5.1.34</mysql.connector.version> <druid.version>1.1.9</druid.version> <junit.version>3.8.1</junit.version> <nekohtml.version>1.9.18</nekohtml.version> <commons.lang3.version>3.4</commons.lang3.version> </properties> <dependencyManagement> <dependencies> <!-- 為什麼此處使用spring-boot-dependencies?答:輔助dependencyManagement,方便管理 --> <!-- 請參考:https://blog.csdn.net/lzb348110175/article/details/90345925 --> <!-- 添加springboot支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${springboot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 添加springboot 熱部署依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>${springboot.version}</version> <optional>true</optional> </dependency> <!-- springboot整合mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${springboot.mybatis.version}</version> </dependency> <!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector.version}</version> </dependency> <!--springboot整合druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!--springboot整合log4j--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> <version>${springboot.log4j.version}</version> </dependency> <!--junit測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 不嚴厲H5--> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>${nekohtml.version}</version> </dependency> <!-- common-lang3工具包--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons.lang3.version}</version> </dependency> </dependencies> </dependencyManagement></project>
<!-- report-web的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> <artifactId>bi-report</artifactId> <groupId>com.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.demo</groupId> <artifactId>report-web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>report-web</name> <description>Divided Module Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <!-- 如下jar包,在web工程會用到,用不到的一律不依賴,做到最優 --> <dependencies> <!--springboot整合web模塊--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- 排除自帶的logging包,換用log4j,做到項目最優--> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 單元測試依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- web層依賴service--> <dependency> <groupId>com.demo</groupId> <artifactId>report-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- springboot添加thymeleaf支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- springboot添加熱部署支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <!-- profiles標簽的使用,方便在開發不同環境之間來回切換 --> <profiles> <!-- 開發環境 --> <profile> <id>develop</id> <properties> <master.driver.className>com.mysql.jdbc.Driver</master.driver.className> <master.jdbc.url>jdbc:mysql://192.168.91.91:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true</master.jdbc.url> <master.username>root</master.username> <master.password>111111</master.password> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <!-- 測試環境 --> <profile> <id>test</id> <properties> <master.driver.className>com.mysql.jdbc.Driver</master.driver.className> <master.jdbc.url>jdbc:mysql://192.168.91.92:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true</master.jdbc.url> <master.username>root</master.username> <master.password>222222</master.password> </properties> </profile> <!-- 預發布環境 --> <profile> <id>pre-release</id> <properties> <master.driver.className>com.mysql.jdbc.Driver</master.driver.className> <master.jdbc.url>jdbc:mysql://192.168.91.93:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true</master.jdbc.url> <master.username>root</master.username> <master.password>333333</master.password> </properties> </profile> <!-- 線上環境 --> <profile> <id>release</id> <properties> <master.driver.className>com.mysql.jdbc.Driver</master.driver.className> <master.jdbc.url>jdbc:mysql://192.168.91.94:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true</master.jdbc.url> <master.username>root</master.username> <master.password>444444</master.password> </properties> </profile> </profiles> <build> <!--環境切換必須配置:如果不設置resource 會導致application.yaml中的@@找不到pom文件中的配置--> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <!-- 解决maven install時提示找不到web.xml文件异常 --> <!-- 异常信息:Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project report-web: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1] --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build></project>
<!-- report-service的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"> <parent> <artifactId>bi-report</artifactId> <groupId>com.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>report-service</artifactId> <!-- service層依賴utils,本來可以不再service層依賴的,但是基於接口開發,dao層完全用不到utils工具包,pojo也用不到,所以在service層依賴是最合適的。通過Maven依賴傳遞,web層也會變向依賴utils工具包 --> <dependencies> <!--依賴dao、utils--> <dependency> <groupId>com.demo</groupId> <artifactId>report-dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.demo</groupId> <artifactId>report-utils</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies></project>
<!-- report-dao的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"> <parent> <artifactId>bi-report</artifactId> <groupId>com.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>report-dao</artifactId> <!-- dao層需要與數據庫進行交互,所以在dao層才依賴jdbc、mybatis、druid、mysql驅動,而不是每一個模塊都依賴這些,對那些模塊也沒用的。log4j日志包每個模塊會用到(pojo除外),所以在dao層依賴,其他模塊通過依賴傳遞來獲取。--> <dependencies> <!--dao層依賴pojo--> <dependency> <groupId>com.demo</groupId> <artifactId>report-pojo</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--springboot整合jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--springboot整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- springboot整合log4j --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency> <!--springboot整合druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <!--mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <!-- 該插件也是只在dao層用到,所以在此處引入即可 --> <!-- mybatis generator 自動生成代碼插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build></project>
<!-- report-pojo的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"> <parent> <artifactId>bi-report</artifactId> <groupId>com.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>report-pojo</artifactId></project>
<!-- report-utils的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"> <parent> <artifactId>bi-report</artifactId> <groupId>com.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>report-utils</artifactId> <dependencies> <!--commons-lang3工具包--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies></project>

具體代碼實現部分(部分)

package com.report.controller;import com.report.pojo.User;import com.report.service.inter.IUserService;import com.report.utils.StringUtil;import com.report.vo.Constants;import com.report.vo.ResultData;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import javax.annotation.Resource;import java.util.List;/** * web模塊Controller */@[email protected]("user")public class UserController{ @Resource(name="userService") private IUserService userService; /** * 獲取用戶信息 */ @RequestMapping("queryUser.htm") @ResponseBody public ResultData<List<User>> queryUser(){ ResultData<List<User>> result = new ResultData<List<User>>(); List<User> data = null; try{ data = userService.queryUser(); }catch (Exception e){ result.setCode(Constants.DATA_QUERY_EXCEPTION); result.setMessage(Constants.DATA_QUERY_EXCEPTION_MSG); } result.setData(data); return result; } /** * 獲取用戶信息[直接返回到頁面] * @param view * @return */ @RequestMapping("getUser.htm") public ModelAndView getUser(ModelAndView view){ view.setViewName("index"); //調用report-utils中的方法 String nm = StringUtil.firstAlphabetUpperCase("Mr.Liu"); System.out.println(nm); List<User> data = userService.queryUser(); view.addObject("userList",data); return view; }}
package com.report.service.inter;import com.report.pojo.User;import java.util.List;/** * 用戶Service接口層 */public interface IUserService { /** * 查詢用戶信息 */ List<User> queryUser();}
package com.report.service.impl;import com.report.dao.master.UserMapper;import com.report.pojo.User;import com.report.pojo.UserExample;import com.report.service.inter.IUserService;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;/** * 用戶Service實現類 */@Service("userService")public class UserServiceImpl implements IUserService { @Resource(name = "userDao") private UserMapper userDao; /** * 查詢用戶信息 */ public List<User> queryUser() { UserExample userEx = new UserExample(); userEx.createCriteria().andIdIsNotNull(); return userDao.selectByExample(userEx); }}
package com.report.dao.master;import com.report.pojo.User;import com.report.pojo.UserExample;import java.util.List;import org.apache.ibatis.annotations.Param;import org.springframework.stereotype.Repository;/** * dao層 */@Repository("userDao")public interface UserMapper { int countByExample(UserExample example); int deleteByExample(UserExample example); int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); List<User> selectByExample(UserExample example); User selectByPrimaryKey(Integer id); int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example); int updateByExample(@Param("record") User record, @Param("example") UserExample example); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record);}
<!-- xmlSql部分(generator自動生成的Sql) --><?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.report.dao.master.UserMapper" > <resultMap id="BaseResultMap" type="com.report.pojo.User" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="username" property="username" jdbcType="VARCHAR" /> <result column="age" property="age" jdbcType="INTEGER" /> <result column="address" property="address" jdbcType="VARCHAR" /> </resultMap> <sql id="Example_Where_Clause" > <where > <foreach collection="oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Update_By_Example_Where_Clause" > <where > <foreach collection="example.oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Base_Column_List" > id, username, age, address </sql> <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.report.pojo.UserExample" > select <if test="distinct" > distinct </if> 'true' as QUERYID, <include refid="Base_Column_List" /> from user <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null" > order by ${orderByClause} </if> </select> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from user where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from user where id = #{id,jdbcType=INTEGER} </delete> <delete id="deleteByExample" parameterType="com.report.pojo.UserExample" > delete from user <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </delete> <insert id="insert" parameterType="com.report.pojo.User" > insert into user (id, username, age, address) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{address,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="com.report.pojo.User" > insert into user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="username != null" > username, </if> <if test="age != null" > age, </if> <if test="address != null" > address, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="username != null" > #{username,jdbcType=VARCHAR}, </if> <if test="age != null" > #{age,jdbcType=INTEGER}, </if> <if test="address != null" > #{address,jdbcType=VARCHAR}, </if> </trim> </insert> <select id="countByExample" parameterType="com.report.pojo.UserExample" resultType="java.lang.Integer" > select count(*) from user <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </select> <update id="updateByExampleSelective" parameterType="map" > update user <set > <if test="record.id != null" > id = #{record.id,jdbcType=INTEGER}, </if> <if test="record.username != null" > username = #{record.username,jdbcType=VARCHAR}, </if> <if test="record.age != null" > age = #{record.age,jdbcType=INTEGER}, </if> <if test="record.address != null" > address = #{record.address,jdbcType=VARCHAR}, </if> </set> <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExample" parameterType="map" > update user set id = #{record.id,jdbcType=INTEGER}, username = #{record.username,jdbcType=VARCHAR}, age = #{record.age,jdbcType=INTEGER}, address = #{record.address,jdbcType=VARCHAR} <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByPrimaryKeySelective" parameterType="com.report.pojo.User" > update user <set > <if test="username != null" > username = #{username,jdbcType=VARCHAR}, </if> <if test="age != null" > age = #{age,jdbcType=INTEGER}, </if> <if test="address != null" > address = #{address,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.report.pojo.User" > update user set username = #{username,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER}, address = #{address,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update></mapper>
<!-- View層:Index.html --><!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Index頁面</title> <link href="../static/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <link href="../static/plugins/dataTables/css/dataTables.bootstrap.css" rel="stylesheet"> <link href="../static/css/style.min.css" rel="stylesheet"> <script src="../static/js/jquery/jquery.min.js"></script> <script src="../static/plugins/bootstrap/js/bootstrap.min.js"></script> <script src="../static/plugins/dataTables/js/jquery.dataTables.js"></script> <script src="../static/plugins/dataTables/js/dataTables.bootstrap.js"></script> </head> <body> <!-- 循環遍曆 --> <div class="wrapper wrapper-content"> <table class="table table-striped table-bordered table-hover dataTables-example"> <thead> <tr> <th>id</th> <th>name</th> <th>age</th> <th>address</th> </tr> </thead> <tbody> <tr th:each="user:${userList}"> <td th:text="${user.id}"></td> <td th:text="${user.username}"></td> <td th:text="${user.age}"></td> <td th:text="${user.address}"></td> </tr> </tbody> </table> </div> </body> <script> $(document).ready(function () { $("table").dataTable(); }); </script></html>

 實現效果

  ①接口開發

 ②錶現層展示

 如有需要Demo實例,有不懂的地方等,可聯系我。demo僅作參考使用

 如有錯誤的地方,也請您能够幫我指出,我們一起學習,一起進步,謝謝大家。

版权声明:本文为[程序員社區]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201071522346575.html