微服务开发

一、基础环境

  • 项目环境
  1. JDK1.8
  2. gradle 4.X
  • 新增微服务主项目后再新增微服务项目,在新增的微服务项目中lib报下添加以下包,并且添加到运行环境中
HikariCP-2.6.3.jar
jasypt-1.9.2.jar
jasypt-spring-boot-1.18.jar
jasypt-spring-boot-starter-1.18.jar
jsqlparser-0.9.5.jar
mybatis-3.4.4.jar
mybatis-plus-2.1.9.jar
mybatis-plus-core-2.1.9.jar
mybatis-plus-generate-2.1.9.jar
mybatis-plus-support-2.1.9.jar
mybatis-spring-1.3.1.jar
mybatis-spring-boot-autoconfigure-1.3.0.jar
mybatis-spring-boot-starter-1.3.0.jar
mybatisplus-spring-boot-starter-1.0.5.jar
hutool-all-4.0.9.jar
  • 在application.properties配置加密盐(即下方ENC(XXXXXXXXX))
    jasypt.encryptor.password=e!Jd&ljyJ^e4I5oU
  • 在application.properties配置数据源
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
#spring.datasource.username=dept
#spring.datasource.password=novell
#加密敏感信息
spring.datasource.username=ENC(ukdPxk2YeSrilKZFvsNr2g==)
spring.datasource.password=ENC(nK0oIOT1Mx6Kn27UhntqBg==)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.pool-name=DataBaseHikari
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
  • 添加mybatis和其插件支持
# mybatis-plus
# 映射mybatis的xml文件
mybatis-plus.mapper-locations = classpath:mapper/*.xml
# 映射实体类包
mybatis-plus.typeAliasesPackage = com.gdtnx.cloud.**.domain

至此,该项目已支持Mybatis

二、样例说明

  • 单表CURD,使用Mybastis-plus插件
  • domain 层
@TableName("tb_sys_user")//标注数据库表名
public class User implements Serializable {
private static final long serialVersionUID = -5858254991390168074L;
   /** 用户ID */ //必须设置主键
   @TableId(value = "id", type = IdType.ID_WORKER_STR)
   private String id;
   /** 用户名 */
   private String name;
   
   //省略get、set;
}
  • mapper 层
public interface UserMapper extends BaseMapper<User>  {
   //自定义mybatis查询
   List selectUserList(Pagination page, @Param("params") Map<String, Object> map);
}
  • service 层

接口

public interface IUserService extends IService<User> {

}

实现

@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService {

}
  • controller 层
@RestController
@RequestMapping("user")
public class UserController {
//注入service
@Autowired private IUserService service;
/**
* add:(新增). <br/>
*/
@PostMapping(path = GlobalRoute.ADD)
public ItemResponse add(@RequestBody ItemRequest item) {
 try {
     User model = (User) item.toBean(User.class);
     this.service.insert(model);
     return new ItemResponse(model);
 } catch (Exception e) {
     return new ItemResponse(HttpItemStatus.DATABASE_ERROR);
 }
}

/**
* edit:(编辑). <br/>
*/
@PostMapping(path = GlobalRoute.EDIT)
public ItemResponse edit(@RequestBody ItemRequest item) {
 try {
     User model = (User) item.toBean(User.class);
     this.service.updateById(model);
     return new ItemResponse(this.service.selectById(model.getId()));
 } catch (Exception e) {
     return new ItemResponse(HttpItemStatus.DATABASE_ERROR);
 }
}

/**
* delete:(删除). <br/>
*/
@PostMapping(path = GlobalRoute.DEL)
public ItemResponse delete(@RequestBody ItemRequest item) {
 try {
     User model = (User) item.toBean(User.class);
     this.service.deleteById(model.getId());
     return new ItemResponse(model.getId());
 } catch (Exception e) {
     return new ItemResponse(HttpItemStatus.DATABASE_ERROR);
 }
}

/**
* get:(根据ID查询按钮组信息). <br/>
*/
@PostMapping(path = GlobalRoute.GET)
public ItemResponse get(@RequestBody ItemRequest item) {
 try {
     User model = (User) item.toBean(User.class);
     return new ItemResponse(this.service.selectById(model.getId()));
 } catch (Exception e) {
     return new ItemResponse(HttpItemStatus.DATABASE_ERROR);
 }
}

/**
* list:(根据属性查询). <br/>
*/
@PostMapping(path = GlobalRoute.LIST)
public ItemResponse list(@RequestBody ItemRequest item) {
 User model = (User) item.toBean(User.class);
 EntityWrapper<User> wrapper = new EntityWrapper<>(model);
 return new ItemResponse(this.service.selectList(wrapper));
}
/**
  * page:(分页查询). <br/>
  */
 @PostMapping(path = GlobalRoute.PAGE)
 public ItemResponse page(@RequestParam Map<String, Object> params, @RequestBody ItemRequest item) {
     User model = (User) item.toBean(User.class);
     Page<User> page = this.service.selectPage(new Condition<>(params), new EntityWrapper<>(model));
     return new ItemResponse(page);
 }
/**
  * list1:(自定义xml查询,继承mybatis-plus的基类). <br/>
  */
 @SuppressWarnings({ "rawtypes" ,"unchecked"})
 @PostMapping(path = GlobalRoute.LIST+"1")
 public ItemResponse list1(@RequestParam Map<String, Object> params, @RequestBody ItemRequest item) {
     Map map = (Map) item.toBean(Map.class);
     Page page = this.service.selectUserPage(new Condition<>(params), map);
     return new ItemResponse(page);
 }   
}
  • mybatis 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.gdtnx.cloud.demo.mapper.UserMapper">
   <select id="selectUserList" resultType="java.util.Map" parameterType="java.util.Map"><!--使用Map传参-->
         SELECT * FROM tb_sys_user
         <if test="params.state!=null">
             WHERE state=#{params.state}
       </if>
   </select>
</mapper>

三、数据传输加密
@Encrypt加密 @Decrypt解密 @EnableEncrypt 启用加密功能,注意点:

  1. 返回类型强制使用 ItemResponse
  2. 请求方法强制使用POST提交
  3. 请求数据需用ItemRequest接收,并且使用 @RequestBody 注解
@Decrypt  //将请求对象的加密数据进行解密(@Decrypt(false)可强制使用false不解密)
@Encrypt //将返回对象的数据加密(@Encrypt(false)可强制使用false不加密)
@PostMapping(path = GlobalRoute.LIST)
public ItemResponse list(@RequestBody ItemRequest item) {
    User model = (User) item.toBean(User.class);
    EntityWrapper<User> wrapper = new EntityWrapper<>(model);
    return new ItemResponse(this.service.selectList(wrapper));
}

启动类上

@MapperScan("com.gdtnx.cloud.**.mapper")  //扫描mybatis的接口类
@EnableEncrypt //启用加密功能
public class Application {

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

    }

    // 注册分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        return page;
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354