从0手写springCloud项目(二 框架代码详解)

栏目: Java · 发布时间: 4年前

内容简介:前面一篇将springCloud所需的几个组件搭建起来了,接下来以user模块为例子主要记录一下项目中集成的技术,框架,和使用方式。我想从以下几个地方总结:

写在前面

前面一篇将springCloud所需的几个组件搭建起来了,接下来以user模块为例子主要记录一下项目中集成的技术,框架,和使用方式。我想从以下几个地方总结:

  • mybatis-plus3
  • lcn5.0.2
  • liquibase
  • oauth2
  • others(es,rabbitmq,全局异常处理,feign之间异常处理,logback,mysql,redis)

集成mybatis-plus3

可能有些同学常用的持久层框架是jpa,但是就我实践而言,mybatisplus好用的不是一丁点,个人建议用mybatisplus...现在plus3的版本支持的还是蛮多的:乐观锁,版本号,代码生成器,分页插件,热加载,通用枚举,自动填充,动态数据源....详见官网( https://mp.baomidou.com ),而且3.0集成也比2.x简单了不少,最简单的只需要加一个pom坐标即可,但是如果需要个性化配置,我们还是要写config,当然也不麻烦,很简单的。

pom

<!--mybatis-plus-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.1.0</version>
</dependency>

yml

//可以看到我的各个文件存放的路径分别在哪里
 mybatis:
  type-aliases-package: cn.iamcrawler.crawler_common.domain.goddess
  mapper-locations: cn/iamcrawler/crawlergoddess/mapper/*Mapper.xml
  type-handlers-package: cn.iamcrawler.crawlergoddess.mapper.typehandler
  global-config:
    refresh-mapper: true

那么接下来继承plus的两个方法就可以了

/**
 * Created by liuliang on 2019/3/21.
 */
public interface DataUserMapper extends BaseMapper<DataUser> {
}


@Service
@Slf4j
public class DataUserService extends   ServiceImpl<DataUserMapper,DataUser>{

}

ps:大家可以想一想,spring的controller,service,mapper需要使用注解将其标识为bean才能扫描得到,而这里的mapper却没有添加@Repository注解,这是因为我在注解类上加了一个注解,指定spring启动的时候扫描到这个包下,所以这里就可以不用添加@Repository注解,也可以被spring扫描到啦。主类的注解如下:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableTransactionManagerServer
@MapperScan("cn.iamcrawler.crawlergoddess.mapper")//上面说的,就是这个注解!
public class CrawlerGoddessApplication {

    public static void main(String[] args) {
        SpringApplication.run(CrawlerGoddessApplication.class, args);
    }
}

是的,就是这么简单。plus最简单的集成就完成了,接下来继承mapper接口,service接口就可以写 sql 了。具体详见mybatis-plus官网。

ps:最后还有一个小知识点,其实大家可以看到,我的xml是放在和mapper一起的(默认是放在resource下面,打包成jar的时候路径就是我们常说的classpath),而我们知道,在maven打包jar的时候,是不会扫描这个 java 下的.xml文件的,为什么我打包jar也可以扫描呢?是因为我在maven里面配置了一个将java下的xml也打包的程序,如下:

<build>
        <finalName>crawler-goddess</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!---我上面说的就是下面这个配置哦-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

集成lcn5.0.2

微服务集成分布式事务框架已经不是一个新鲜的事情,在这方面,我前面也写过好几个文章介绍过,其中有springboot1.5.9+lcn4.1.0( https://segmentfault.com/a/11... ), 有springboot2.1.3+lcn5.0.2( https://segmentfault.com/a/11... )。大家可以按需阅读并配置,当然这里我这个demo配置的是5.0.2版本.毕竟这个版本比4.1.0强大了不少,并且配置也更加方便。具体大家也可以参照lcn官网,描述的非常详细( http://www.txlcn.org

集成liquibase

不知道大家是否经历过没有用程序代码管理sql语句的项目,我最开始经历过一次。每次上线前,组长会问我们有没有什么sql需要执行的,上线前执行还是上线后执行,他那边统计好,然后在生产数据库执行。这样是很麻烦的,每次都要人工统计,人工执行,如果sql没有问题还好,若是有问题,是加长了都不知道是谁写的这句sql,问题很难追根溯源。并且每次更换系统环境,都是直接要把表结构DDL一份出来...真的是很low啊有没有....

而liquibase是一个开源的数据跟踪,管理工具,它可以做我们上面说的所有事情,并且还很强大。接下来看一下在springboot2.1.3是怎么用的吧(boot版本不同,集成方式略有差异,大家注意一下)

pom

<!--liquibase依赖-->
<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
</dependency>

yml

spring:
  application:
    name: crawler-goddess
  liquibase:
    change-log: classpath:liquibase\master.xml #标识liquibase的入口

上面也讲到,classpath其实就是打包后的resource目录,所以这个入口在resource.liquibase下的一个叫master.xml文件里面(当然大家可以自定义路径)

从0手写springCloud项目(二 框架代码详解)

看一下这个master.xml里的信息:

<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
    <!--我这里之配置了一个路径-->   
    <include file="classpath:liquibase/changelog/release20180919.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>


可以看到配置里面是读的liquibase/changelog/release20180919.xml这个文件


release20180919.xml

 <?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

    <property name="now" value="now()" dbms="mysql,h2"/>
    <property name="autoIncrement" value="true" dbms="mysql,h2,postgresql,oracle"/>

<!--这里又可以引入其他文件的sql-->
    <changeSet id="201811010924001" author="liang.liu">
        <sqlFile path="classpath:liquibase/init/init_table.sql" encoding="UTF-8"/>
        <sqlFile path="classpath:liquibase/init/add_user.sql" encoding="UTF-8"/>
    </changeSet>
    <!--也可以自己创建表,删除表,创建索引,删除索引...-->
    <changeSet id="201906180001" author="liang.liu">
        <createTable tableName="test_table">
            <column name="id" type="varchar(20)" remarks="id">
                <constraints primaryKey="true"/>
            </column>
        </createTable>
    </changeSet>
</databaseChangeLog>

当然,更多的用法和配置大家也可以参照官网使用( http://www.liquibase.org/


以上所述就是小编给大家介绍的《从0手写springCloud项目(二 框架代码详解)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The NSHipster Fake Book (Objective-C)

The NSHipster Fake Book (Objective-C)

Mattt Thompson / NSHipster Portland, Oregon / 2014 / USD 19.00

Fake Books are an indispensable tool for jazz musicians. They contain the melody, rhythm, and chord changes for hundreds of standards, allowing a player to jump into any session cold, and "fake it" th......一起来看看 《The NSHipster Fake Book (Objective-C)》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具