COLA各模块理解

2024-06-15  本文已影响0人  言午日尧耳总

domain

面相对象

public class Dog {
    String name;
    int age;

    void eat() {
    }

    void run() {
    }
}

面相表格

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String password;
}
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public void changeInfo(Long id) {
        User user = userMapper.selectById(id);
        user.setName("张三");
        user.setAge(18);
        user.setPassword(getMd5("123456"));
        userMapper.updateById(user);
    }

    private String getMd5(String password) {
        return password + "-md5"; // 模拟
    }
}

领域对象

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String password;

    public void changeInfo(String name, Integer age, String password) {
        this.name = name;
        this.age = age;
        this.password = getMd5(password);
    }

    private String getMd5(String password) {
        return password + "-md5"; // 模拟
    }
}
public class UserService {
    @Autowired
    private UserGateway gateway;

    public void changeInfo(Long id) {
        User user = gateway.get(id);
        user.changeInfo("张三", 18, "12345");
        gateway.save(user);
    }
}

领域对象与数据对象

代码实例

@Data
public class User {
    private Long id;
    private String name;
    private List<String> roles; // 权限列表

    public User(String name) {
        this.name = name;
        this.roles = new ArrayList<>();
    }

    public void addRole(String role) {
        this.roles.add(role);
    }
}
// 数据对象
@Data
@TableName("user")
public class UserDO {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private String rolesJson; // 转换为json字符串
}
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface UserConvertor {

    @Mapping(target = "rolesJson", expression = "java(map(entity.getRoles()))")
    UserDO toDataObject(User entity);

    @Mapping(target = "roles", expression = "java(map(dataObject.getRolesJson()))")
    User toEntity(UserDO dataObject);

    default String map(List<String> roles) {
        return JSON.toJSONString(roles);
    }

    default List<String> map(String rolesJson) {
        return JSON.parseArray(rolesJson, String.class);
    }
}

反例

@Data
@TableName("user")
public class UserEntity {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;

    @TableField(exist = false)
    private List<String> roles; // 字段不映射数据表

    private String rolesJson;   // 存储字段

    public UserEntity(String name) {
        this.name = name;
        this.roles = new ArrayList<>();
        this.rolesJson = "[]";
    }

    public void addRole(String role) {
        this.roles.add(role);
        this.rolesJson = JSON.toJSONString(this.roles);
    }
}

client

client接口定义

public interface UserServiceI{
    // 略
}

cleint打包发布私有Maven

    <dependencies>...略</dependencies>
    <build>...略</build>

    <distributionManagement>
        <repository>
            <id>maven-releases</id>
            <name>maven-releases</name>
            <url>${这里填写从nexus页面上复制的maven-releases的url}</url>
        </repository>
        <snapshotRepository>
            <id>maven-snapshots</id>
            <name>maven-snapshots</name>
            <url>${这里填写从nexus页面上复制的maven-snapshots的url}</url>
        </snapshotRepository>
    </distributionManagement>
<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
    <servers>
        <server>
            <id>maven-releases</id>
            <username>admin</username>
            <password>${密码}</password>
        </server>
        <server>
            <id>maven-snapshots</id>
            <username>admin</username>
            <password>${密码}</password>
        </server>
    </servers>
</settings>
# 复制maven配置文件
cp maven-settings.xml /usr/share/maven/conf/settings.xml
# 执行maven
mvn deploy -pl cola-springboot-demo-client -am

app层接口实现

@Service
@DubboService   // 注册为Dubbo服务
public class UserServiceImpl implements UserServiceI{
    // 略
}

其他微服务RPC调用

<dependency>
    <groupId>com.xxc</groupId>
    <artifactId>cola-springboot-demo-client</artifactId>
    <version>1.0.0</version>
</dependency>
@Component
public class XxxExe{
    @DubboReference
    private UserServiceI userService;

    public void execute(){
        // 略
    }
}
上一篇 下一篇

猜你喜欢

热点阅读