实战——Spring中提供的数据库操作API

一、使用步骤

只需要在pom中引入spring的依赖jar包,即可。

1 注入依赖

NamedParameterJdbcTemplate
在这里插入图片描述

2 操作使用(3步骤)

public List<EcssDepartment> getDepartmentsByLongDepIds(List<Long> deptSet) {
        //1.构建sql
        String sql = " SELECT department.n_id id, department.c_ec_code ecCode, department.c_name name, department.c_code code, " +
                " department.n_parent_id parentId ,department.n_sort sort " +
                " FROM t_ecss_department department WHERE  " +
                " department.n_id IN (:depIds) " +
                " AND department.n_last_update_type <> :lastUpdateType ";
        //2.构建sql中的占位符参数
        MapSqlParameterSource sqlParam = new MapSqlParameterSource();
        sqlParam.addValue("depIds", deptSet);
        sqlParam.addValue("lastUpdateType",EcssDepartment.ENUM_LAST_UPDATE_TYPE.DELETETYPE.value);

        //3.执行sql的API 参数1:sql 参数2:sql参数 参数3:映射关系对象
        List<EcssDepartment> departmentList = namedJdbcTemplate.query(sql, sqlParam, BeanPropertyRowMapper.newInstance(EcssDepartment.class));
        return departmentList;
    }

3 映射关系对象:RowMapper

1、若select 的字段(可以起别名) 跟 实体列保持一致,则可以使用:
BeanPropertyRowMapper.newInstance(EcssDepartment.class) ,它的返回值 就是一个 RowMapper接口的实现类对象。

2、也可以自定义映射关系对象:

在这里插入图片描述

二、SpringJdbcTemplate实操演示

/**
 *
 * 基础jdbc操作类,提供基于标准sql的数据库操作方法
 *
 */
@Component
public class JdbcOperator implements InitializingBean {

    public Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private DataSource dataSource;

    @Autowired
    public NamedParameterJdbcTemplate namedJdbcTemplate;

    private DbName dbName;

    //数据库类型
    private enum DbName{
        MYSQL,ORACLE,H2
    }

    @Override
    public void afterPropertiesSet() {
        String dialect = Hibernates.getDialect(dataSource);
        if (MySQL5InnoDBDialect.class.getName().equals(dialect)) {
            dbName = DbName.MYSQL;
        } else if (Oracle10gDialect.class.getName().equals(dialect)){
            dbName = DbName.ORACLE;
        } else if (H2Dialect.class.getName().equals(dialect)) {
            dbName = DbName.H2;
        }

        Assert.notNull(dbName, "不支持的数据库类型:" + dialect);

        logger.debug("当前系统使用的数据库为:{}", dbName);
    }


    /**
     *
     * @Title: queryForPage
     * @Description: 不带参数的分页查询
     * @param sql
     * @param pageSize 每页条数
     * @param pageNo 当前页码
     * @return Pagination 分页信息,其中包含有数据列表:List<Map<String, Object>>
     */
    public Pagination<Map<String, Object>> queryForPage(String sql, Integer pageSize, Integer pageNo) {
        return queryForPage(sql, pageSize, pageNo, new MapSqlParameterSource());
    }

    /**
     *
     * 不带参数的分布查询
     * @param sql
     * @param pageSize
     * @param pageNo
     * @param mapper
     * @return Pagination 分页信息,其中包含有数据列表:List<T>
     */
    public <T> Pagination<T> queryForPage(String sql, Integer pageSize, Integer pageNo, RowMapper<T> mapper) {
        return queryForPage(sql, pageSize, pageNo, null, mapper);
    }

    /**
     * 分页查询,程序根据连接对象自动区分数据库类型,调用时不必区分数据库类型(仅支持oralce、mysql)
     * @Title: queryForPage
     * @Description: 带参数的分页查询
     * @param sql
     * @param pageSize 每页条数
     * @param pageNo 当前页码
     * @param paramSource 查询条件参数
     * @return Pagination 分页信息,其中包含有数据列表:List<Map<String, Object>>
     */
    public Pagination<Map<String, Object>> queryForPage(String sql, Integer pageSize, Integer pageNo, MapSqlParameterSource paramSource) {
        switch (dbName) {
            case MYSQL:
                return queryForPageMysql(sql, pageSize, pageNo, paramSource);
            case ORACLE:
                return queryForPageOracle(sql, pageSize, pageNo, paramSource);
            case H2:
                return queryForPageMysql(sql, pageSize, pageNo, paramSource);
            default:
                throw new UnsupportedDatabaseException("不支持的数据库类型:" + dbName);
        }
    }

    /**
     *
     * 分页查询,程序根据连接对象自动区分数据库类型,调用时不必区分数据库类型(仅支持oralce、mysql)
     * @param sql
     * @param pageSize
     * @param pageNo
     * @param paramSource
     * @param mapper
     * @return Pagination 分页信息,其中包含有数据列表:List<T>
     */
    public <T> Pagination<T> queryForPage(String sql, Integer pageSize, Integer pageNo, MapSqlParameterSource paramSource, RowMapper<T> mapper) {
        switch (dbName) {
            case MYSQL:
                return queryForPageMysql(sql, pageSize, pageNo, paramSource, mapper);
            case ORACLE:
                return queryForPageOracle(sql, pageSize, pageNo, paramSource, mapper);
            case H2:
                return queryForPageMysql(sql, pageSize, pageNo, paramSource, mapper);
            default:
                throw new UnsupportedDatabaseException("不支持的数据库类型:" + dbName);
        }
    }

    /**
     * mysql分页查询
     * @Description:
     * @author: 周玉杰
     * @date: 2013-12-23 下午3:41:28
     * @param sql
     * @param pageSize
     * @param pageNo
     * @param paramSource
     * @return
     */
    private Pagination<Map<String, Object>> queryForPageMysql(String sql, Integer pageSize, Integer pageNo, MapSqlParameterSource paramSource){
        int totalCount = 0;
        if(null != paramSource){
            totalCount = namedJdbcTemplate.queryForObject(getRowCountSql(sql), paramSource, Integer.class);
        }

        Pagination<Map<String, Object>> p = new Pagination<Map<String, Object>>(pageNo, pageSize, totalCount);
        if (totalCount < 1) {
            p.setList(new ArrayList<Map<String, Object>>());
            return p;
        }

        sql += " limit :offset, :rows";
        if(paramSource == null) {
            paramSource = new MapSqlParameterSource();
        }
        paramSource.addValue("offset", p.getFirstResult());
        paramSource.addValue("rows", p.getPageSize());

        p.setList(namedJdbcTemplate.queryForList(sql, paramSource));
        return p;
    }

    /**
     *
     * 基于mysql的分页查询
     * @param sql
     * @param pageSize
     * @param pageNo
     * @param paramSource
     * @param mapper
     * @return
     */
    private <T> Pagination<T> queryForPageMysql(String sql, Integer pageSize, Integer pageNo, MapSqlParameterSource paramSource, RowMapper<T> mapper){
        int totalCount = 0;
        if(null != paramSource){
            totalCount = namedJdbcTemplate.queryForObject(getRowCountSql(sql), paramSource, Integer.class);
        }
        Pagination<T> p = new Pagination<T>(pageNo, pageSize, totalCount);
        if (totalCount < 1) {
            p.setList(new ArrayList<T>());
            return p;
        }

        sql += " limit :offset, :rows";
        if(paramSource == null) {
            paramSource = new MapSqlParameterSource();
        }
        paramSource.addValue("offset", p.getFirstResult());
        paramSource.addValue("rows", p.getPageSize());

        p.setList(namedJdbcTemplate.query(sql, paramSource, mapper));
        return p;
    }

    /**
     * oracle分页查询
     * @Description:
     * @author: 周玉杰
     * @date: 2013-12-23 下午3:41:32
     * @param sql
     * @param pageSize
     * @param pageNo
     * @param paramSource
     * @return
     */
    private Pagination<Map<String, Object>> queryForPageOracle(String sql, Integer pageSize, Integer pageNo, MapSqlParameterSource paramSource){
        int totalCount = 0;
        if(null != paramSource){
            totalCount = namedJdbcTemplate.queryForObject(getRowCountSql(sql), paramSource, Integer.class);
        }
        Pagination<Map<String, Object>> p = new Pagination<Map<String, Object>>(pageNo, pageSize, totalCount);
        if (totalCount < 1) {
            p.setList(new ArrayList<Map<String, Object>>());
            return p;
        }
        sql =  "SELECT * FROM (SELECT pagedTable.*, ROWNUM AS myRownum FROM (" +
                sql + ") pagedTable WHERE ROWNUM<= :offset" +
                " ) WHERE myRownum>= :rows";
        if(paramSource == null) {
            paramSource = new MapSqlParameterSource();
        }
        paramSource.addValue("offset", p.getFirstResult() + p.getPageSize());
        paramSource.addValue("rows", p.getFirstResult() + 1);
        p.setList(namedJdbcTemplate.queryForList(sql, paramSource));
        return p;
    }

    /**
     *
     * 基于oracle的分页查询
     * @param sql
     * @param pageSize
     * @param pageNo
     * @param paramSource
     * @param mapper
     * @return
     */
    private <T> Pagination<T> queryForPageOracle(String sql, Integer pageSize, Integer pageNo, MapSqlParameterSource paramSource, RowMapper<T> mapper){
        int totalCount = 0;
        if(null != paramSource){
            totalCount = namedJdbcTemplate.queryForObject(getRowCountSql(sql), paramSource, Integer.class);
        }
        Pagination<T> p = new Pagination<T>(pageNo, pageSize, totalCount);
        if (totalCount < 1) {
            p.setList(new ArrayList<T>());
            return p;
        }
        sql =  "SELECT * FROM (SELECT pagedTable.*, ROWNUM AS myRownum FROM (" +
                sql + ") pagedTable WHERE ROWNUM<= :offset" +
                " ) WHERE myRownum>= :rows";
        if(paramSource == null) {
            paramSource = new MapSqlParameterSource();
        }
        paramSource.addValue("offset", p.getFirstResult() + p.getPageSize());
        paramSource.addValue("rows", p.getFirstResult() + 1);
        p.setList(namedJdbcTemplate.query(sql, paramSource, mapper));
        return p;
    }

    /**
     *
     * @Title: getRowCountSql
     * @Description: 获得查询数据库记录数的sql语句
     * @param sql
     * @return String
     */
    private String getRowCountSql(String sql) {
        int fromIndex = sql.toLowerCase().indexOf("from");
        String rowCountSql = sql.substring(fromIndex);

        int index = rowCountSql.toLowerCase().indexOf("order by");
        if (index > 0) {
            rowCountSql = rowCountSql.substring(0, index);
        }
        return "SELECT COUNT(*)" + rowCountSql;
    }

    /**
     *
     * @Description:执行sql语句执行数据删除操作
     * @author: 任瑞修
     * @date: 2013-11-15 下午2:20:02
     * @param sql 要执行的删除语句
     * @param sqlParams 条件
     * @return int 影响的记录数
     */
    public int deleteBySql(String sql, SqlParameterSource sqlParams) {
        return this.namedJdbcTemplate.update(sql, sqlParams);
    }

    /**
     *
     * 执行sql语句执行数据批量删除操作
     * @param sql 要执行的删除语句
     * @param sqlParams 条件
     * @return int[] 影响的记录数
     */
    public int[] deleteBySql(String sql, SqlParameterSource... sqlParams) {
        if(ArrayUtils.isEmpty(sqlParams)) {
            sqlParams = ArrayUtils.add(sqlParams, null);
        }
        return namedJdbcTemplate.batchUpdate(sql, sqlParams);
    }

    /**
     *
     * @Description:根据指定条件执行对指定数据表的数据删除操作
     * @author: 任瑞修
     * @date: 2013-11-19 上午9:35:10
     * @param tableName 要删除数据的表名
     * @param condition 条件(where后的表达式)
     * @return int 影响的记录数
     */
    public int deleteByCondition(String tableName, Condition condition) {
        StringBuffer sql = new StringBuffer("DELETE FROM ");
        sql.append(tableName).append(" WHERE ").append(condition.toSqlString());
        return deleteBySql(sql.toString(), new MapSqlParameterSource(condition.getParams()));
    }

    /**
     *
     * @Title: executeSql
     * @Description: 执行指定的sql语句
     * @param sql
     * @param sqlParam void
     * @return int 影响的记录数
     */
    public int executeSql(String sql, SqlParameterSource sqlParam) {
        return this.namedJdbcTemplate.update(sql, sqlParam);
    }

    /**
     *
     * @Title: queryForList
     * @Description: 执行指定的SQL语句查询数据
     * @param sql
     * @param sqlParam
     * @return List<Map<String,Object>>
     */
    public List<Map<String, Object>> queryForList(String sql, SqlParameterSource sqlParam) {
        return namedJdbcTemplate.queryForList(sql, sqlParam);
    }

    /**
     *
     * @Title: queryForList
     * @Description: 执行指定的SQL语句查询数据
     * @param sql
     * @param sqlParam
     * @param mapper
     * @return List<T>
     */
    public <T> List<T> queryForList(String sql, SqlParameterSource sqlParam, RowMapper<T> mapper) {
        return namedJdbcTemplate.query(sql, sqlParam, mapper);
    }

    /**
     *
     * @Title: queryForObject
     * @Description: 执行指定的SQL语句查询数据
     * @param sql
     * @param sqlParam
     * @param mapper
     * @return T
     */
    public <T> T queryForObject(String sql, SqlParameterSource sqlParam, RowMapper<T> mapper) {
        return namedJdbcTemplate.queryForObject(sql, sqlParam, mapper);
    }

    /**
     *
     * @Description:执行指定的SQL语句查询数据
     * @author: 任瑞修
     * @date: 2013-11-6 上午11:40:38
     * @param sql
     * @param sqlParam
     * @return
     */
    public Map<String, Object> queryForMap(String sql, SqlParameterSource sqlParam) {
        return namedJdbcTemplate.queryForMap(sql, sqlParam);
    }

    /**
     *
     * @Title: queryForInt
     * @Description: 执行指定的SQL语句查询结果,多用于count等
     * @param sql
     * @param sqlParam
     * @return int
     */
    public int queryForInt(String sql, SqlParameterSource sqlParam) {
        return namedJdbcTemplate.queryForObject(sql, sqlParam, Integer.class);
    }

    /**
     *
     * @Description:执行指定的SQL语句查询结果
     * @author: 任瑞修
     * @date: 2013-11-12 上午8:45:07
     * @param sql
     * @param sqlParam
     * @return long
     */
    public long queryForLong(String sql, SqlParameterSource sqlParam) {
        return namedJdbcTemplate.queryForObject(sql, sqlParam, Long.class);
    }

    /**
     *
     * @Description:执行指定的sql批量添加或更新数据
     * @author: 任瑞修
     * @date: 2013-11-6 下午5:17:50
     * @param sql
     * @param sqlParams
     * @return int[] 影响的记录数
     */
    public int[] addOrUpdate(String sql, SqlParameterSource... sqlParams) {
        if(ArrayUtils.isEmpty(sqlParams)) {
            sqlParams = ArrayUtils.add(sqlParams, null);
        }
        return namedJdbcTemplate.batchUpdate(sql, sqlParams);
    }

    /**
     *
     * @Description:按指定条件统计记录数
     * @author: 任瑞修
     * @date: 2013-11-19 上午10:46:45
     * @param tableName 要执行记录数统计的表名
     * @param condition 条件
     * @return 记录数
     */
    public int countByCondition(String tableName, Condition condition) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT COUNT(1) FROM ").append(tableName).append(" WHERE ").append(condition.toSqlString());
        return queryForInt(sql.toString(), new MapSqlParameterSource(condition.getParams()));
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本篇文章是 Spring Boot 实践之十三 9 Spring Boot综合项目实战——个人博客系统管理模块 的续篇,本次将介绍如何实现个人博客系统的拓展模块。 在实际开发,我们常常需要对系统进行扩展,添加一些新的功能模块。为了不影响原有代码的结构和功能,我们可以将这些新功能模块独立成为一个子模块,然后通过配置文件等方式将其与原有系统进行整合。 本文将以一个个人博客系统为例,介绍如何实现博客的拓展模块,具体包括以下几个方面: 1. 拓展模块的设计和实现 2. 拓展模块的集成和配置 3. 拓展模块的使用示例 ## 1. 拓展模块的设计和实现 在本例,我们将实现一个博客系统的拓展模块,该模块主要提供以下两个功能: 1. 统计博客文章的阅读量并显示 2. 在博客页面添加底部的版权声明 ### 1.1 统计博客文章的阅读量并显示 首先,我们需要在数据库添加一个字段来存储博客文章的阅读量。在本例,我们在 `blog` 表添加 `read_count` 字段来存储阅读量。 ```sql ALTER TABLE `blog` ADD COLUMN `read_count` INT NOT NULL DEFAULT 0 COMMENT '阅读量' AFTER `update_time`; ``` 接下来,在博客文章页面添加一个阅读量的显示。我们可以在博客文章详情页面的右侧添加一个阅读量的区域,显示该文章的阅读量。具体的实现方式为: 1. 在博客文章详情页面添加一个阅读量的区域。 2. 在加载博客文章详情页面时,通过 AJAX 请求统计该文章的阅读量,并更新阅读量区域的显示。 具体的代码实现如下: 在博客文章详情页面添加一个阅读量的区域: ```html <div class="blog-sidebar-item"> <div class="blog-sidebar-title">阅读量</div> <div class="blog-sidebar-content"> <span id="read-count">0</span> </div> </div> ``` 在加载博客文章详情页面时,通过 AJAX 请求统计该文章的阅读量,并更新阅读量区域的显示。具体的实现方式为: ```javascript $(function () { // 统计阅读量 var blogId = $("#blogId").val(); $.ajax({ url: "/blog/read/" + blogId, type: "POST", success: function (result) { if (result && result.success) { $("#read-count").text(result.data); } else { $("#read-count").text(0); } } }); }); ``` 在服务器端,我们需要实现一个接口来统计博客文章的阅读量,并将其保存到数据库。具体的实现方式为: ```java @RestController @RequestMapping("/blog") public class BlogController { ... /** * 统计博客文章的阅读量 * * @param blogId 博客文章ID * @return 统计结果 */ @PostMapping("/read/{blogId}") public Result<Integer> readBlog(@PathVariable("blogId") Long blogId) { int readCount = blogService.readBlog(blogId); return Result.success(readCount); } ... } ``` 在 `BlogService` 实现 `readBlog` 方法: ```java @Service public class BlogServiceImpl implements BlogService { ... /** * 统计博客文章的阅读量 * * @param blogId 博客文章ID * @return 统计结果 */ @Override public int readBlog(Long blogId) { Blog blog = blogMapper.selectByPrimaryKey(blogId); if (blog != null) { int readCount = blog.getReadCount() + 1; blog.setReadCount(readCount); blogMapper.updateByPrimaryKeySelective(blog); return readCount; } return 0; } ... } ``` ### 1.2 在博客页面添加底部的版权声明 接下来,我们将在博客页面底部添加一个版权声明。具体的实现方式为: 1. 在博客页面底部添加一个版权声明的区域。 2. 在加载博客页面时,通过 AJAX 请求获取版权声明的内容,并更新版权声明区域的显示。 具体的代码实现如下: 在博客页面底部添加一个版权声明的区域: ```html <div class="blog-footer"> <div><span id="copyright"> 版权声明:本博客所有文章均为作者原创或转载,未经授权禁止转载。 </span></div> </div> ``` 在加载博客页面时,通过 AJAX 请求获取版权声明的内容,并更新版权声明区域的显示。具体的实现方式为: ```javascript $(function () { // 加载版权声明 $.ajax({ url: "/blog/copyright", type: "GET", success: function (result) { if (result && result.success) { $("#copyright") .html("版权声明:" + result.data); } } }); }); ``` 在服务器端,我们需要实现一个接口来获取版权声明的内容。具体的实现方式为: ```java @RestController @RequestMapping("/blog") public class BlogController { ... /** * 获取版权声明的内容 * * @return 版权声明的内容 */ @GetMapping("/copyright") public Result<String> getCopyright() { String content = "本博客所有文章均为作者原创或转载,未经授权禁止转载。"; return Result.success(content); } ... } ``` ## 2. 拓展模块的集成和配置 在上一篇文章,我们已经将博客系统的所有模块都整合到了一个工程,因此我们可以通过添加一个 Maven 模块来实现拓展模块的开发,并将其整合到原有工程。 具体的步骤如下: 1. 在项目根目录下创建一个新的 Maven 模块,命名为 `blog-ext`,并将其添加到工程。 2. 在 `blog-ext` 模块添加 `pom.xml` 文件,并添加依赖关系。 3. 在 `blog-ext` 模块添加 Spring Boot 的配置文件 `application.yml`,并添加相关配置。 4. 在 `blog-ext` 模块添加拓展模块的代码和资源文件。 ### 2.1 添加 Maven 模块 在项目根目录下创建一个新的 Maven 模块,命名为 `blog-ext`,并将其添加到工程。具体的步骤如下: 1. 在项目根目录下创建一个新的 Maven 模块,命名为 `blog-ext`。 ```bash $ cd ~/workspace/springboot-blog $ mvn archetype:generate -DgroupId=com.waylau.spring.boot.blog \ -DartifactId=blog-ext -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false ``` 2. 将 `blog-ext` 模块添加到工程。 ```xml <modules> <module>blog-api</module> <module>blog-service</module> <module>blog-web</module> <module>blog-ext</module> </modules> ``` ### 2.2 添加依赖关系 在 `blog-ext` 模块添加 `pom.xml` 文件,并添加依赖关系。具体的依赖关系如下: ```xml <dependencies> <dependency> <groupId>com.waylau.spring.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 添加 Spring Web MVC 的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 添加 MyBatis 的依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.boot.version}</version> </dependency> </dependencies> ``` ### 2.3 添加配置文件 在 `blog-ext` 模块添加 Spring Boot 的配置文件 `application.yml`,并添加相关配置。具体的配置如下: ```yaml spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/blog?useSSL=false&useUnicode=true&characterEncoding=utf8 username: root password: root mvc: view: prefix: /templates/ suffix: .html resources: static-locations: classpath:/static/ ``` ### 2.4 添加拓展模块的代码和资源文件 在 `blog-ext` 模块添加拓展模块的代码和资源文件。具体的步骤如下: 1. 在 `blog-ext` 模块添加 `com.waylau.spring.boot.blog.ext` 包,并在该包下添加 `BlogExtApplication` 类。 ```java @SpringBootApplication(scanBasePackages = "com.waylau.spring.boot.blog.ext") public class BlogExtApplication { public static void main(String[] args) { SpringApplication.run(BlogExtApplication.class, args); } } ``` 2. 在 `blog-ext` 模块添加 `resources` 目录,并在该目录下添加 `templates` 和 `static` 目录。 3. 在 `templates` 目录添加 `read-count.html` 和 `copyright.html`。 ```html <!-- read-count.html --> <div class="blog-sidebar-item"> <div class="blog-sidebar-title">阅读量</div> <div class="blog-sidebar-content"> <span id="read-count">0</span> </div> </div> ``` ```html <!-- copyright.html --> <div class="blog-footer"> <div><span id="copyright"> 版权声明:本博客所有文章均为作者原创或转载,未经授权禁止转载。 </span></div> </div> ``` 4. 在 `static` 目录添加 `js` 目录,并在该目录下添加 `read-count.js` 和 `copyright.js`。 ```javascript // read-count.js $(function () { // 统计阅读量 var blogId = $("#blogId").val(); $.ajax({ url: "/blog/read/" + blogId, type: "POST", success: function (result) { if (result && result.success) { $("#read-count").text(result.data); } else { $("#read-count").text(0); } } }); }); ``` ```javascript // copyright.js $(function () { // 加载版权声明 $.ajax({ url: "/blog/copyright", type: "GET", success: function (result) { if (result && result.success) { $("#copyright") .html("版权声明:" + result.data); } } }); }); ``` ## 3. 拓展模块的使用示例 在完成了拓展模块的开发和配置之后,我们需要将其与原有系统进行整合。具体的步骤如下: 1. 在原有系统添加对拓展模块的依赖关系。 在 `blog-web` 模块的 `pom.xml` 文件添加对 `blog-ext` 模块的依赖关系: ```xml <dependencies> ... <!-- 添加 blog-ext 的依赖 --> <dependency> <groupId>com.waylau.spring.boot.blog</groupId> <artifactId>blog-ext</artifactId> <version>${project.version}</version> </dependency> </dependencies> ``` 2. 在原有系统添加拓展模块的使用示例。 在博客文章详情页面添加一个阅读量的区域: ```html <!-- 添加阅读量的区域 --> <div th:replace="blog-ext :: read-count"></div> ``` 在博客页面底部添加一个版权声明的区域: ```html <!-- 添加版权声明的区域 --> <div th:replace="blog-ext :: copyright"></div> ``` 经过以上的步骤,我们就已经成功地将博客系统的拓展模块整合到了原有系统。 ## 总结 本文介绍了如何实现 Spring Boot 的拓展模块,并将其与原有系统进行整合。在实际开发,我们可以根据具体的需求来实现不同的拓展模块,并通过配置文件等方式将其整合到原有系统。这种方式既提高了代码的可维护性,又方便了模块的扩展和升级。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值