Solon v2.7.5

::xm-jimmer-solon-plugin

</> markdown

此插件,由社区成员(Yui)贡献

<dependency>
    <groupId>vip.xunmo</groupId>
    <artifactId>xm-jimmer-solon-plugin</artifactId>
    <version>${last-version}</version>
</dependency>

1、描述

数据扩展插件,为 Solon Data 提供基于 jimmer(代码仓库)的框架适配,以提供ORM支持。

关于 Jimmer 详情可见官网文档:https://babyfish-ct.github.io/jimmer/zh/

2、强调多数据源支持

  • 强调多数据源的配置。例:demo.db1...,demo.db2...
  • 强调带 name 的 DataSource Bean
  • 强调使用 @Db("name") 的数据源注解

3、应用示例

  • 数据源配置
demo.db1:
  schema: rock
  jdbcUrl: jdbc:mysql://localhost:3306/rock?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true
  driverClassName: com.mysql.cj.jdbc.Driver
  username: root
  password: 123456
  • 代码应用

更多使用说明,请参考:https://github.com/zengyufei/xm-solon-demo/tree/jim/xm-solon-plugin/xm-jimmer-solon-plugin

//配置数据源
@Configuration
public class Config {
    //typed=true,表示默认数据源。@Db 可不带名字注入 
    @Bean(name="db1", typed=true)
    public DataSource db1(@Inject("${demo.db1}") HikariDataSource ds) throws Exception{
        //如果有需要加一下
        try(final Connection c = ds.getConnection()){
            c.prepareStatement(ResourceUtil.getResourceAsString("init.sql")).execute();
            c.prepareStatement(ResourceUtil.getResourceAsString("data.sql")).execute();
        } 
        
        return ds;
    }
}

//定义Dao接口
public interface UserRepository extends JRepository<User, String> {
}


//应用
@Slf4j
@Valid
@Controller
@Mapping("/user")
public class UserController {

    private final static UserTable TABLE = UserTable.$;

    private final static UserFetcher FETCHER = UserFetcher.$;

    @Db
    private JSqlClient sqlClient;

    @Db
    private UserRepository userRepository;

    @Post
    @Mapping("/list")
    public Page<User> list(@Validated UserQuery query, PageRequest pageRequest) throws Exception {
        final String usersId = query.getUserId();
        final String userName = query.getUserName();
        final LocalDateTime beginCreateTime = query.getBeginCreateTime();
        final LocalDateTime endCreateTime = query.getEndCreateTime();
        return userRepository.pager(pageRequest)
                .execute(sqlClient.createQuery(TABLE)
                        // 根据 用户id 查询
                        .whereIf(StrUtil.isNotBlank(usersId), () -> TABLE.usersId().eq(usersId))
                        // 根据 用户名称 模糊查询
                        .whereIf(StrUtil.isNotBlank(userName), () -> TABLE.userName().like(userName))
                        // 根据 创建时间 大于等于查询
                        .whereIf(beginCreateTime != null, () -> TABLE.createTime().ge(beginCreateTime))
                        // 根据 创建时间 小于等于查询
                        .whereIf(endCreateTime != null, () -> TABLE.createTime().le(endCreateTime))
                        // 默认排序 创建时间 倒排
                        .orderBy(TABLE.createTime().desc())
                        .select(TABLE.fetch(
                                // 查询 用户表 所有属性(非对象)
                                FETCHER.allScalarFields()
                                        // 查询 创建者 对象,只显示 姓名
                                        .create(UserFetcher.$.userName())
                                        // 查询 修改者 对象,只显示 姓名
                                        .update(UserFetcher.$.userName())
                        )));
    }

    @Post
    @Mapping("/getById")
    public User getById(@NotNull @NotBlank String id) throws Exception {
//        final User user = userRepository.findById(id).orElse(null);
        final User user = this.sqlClient.findById(User.class, id);
        return user;
    }

    @Post
    @Mapping("/add")
    public User add(@Validated UserInput input) throws Exception {
//        final User modifiedEntity = userRepository.save(input);
        final SimpleSaveResult<User> result = this.sqlClient.save(input);
        final User modifiedEntity = result.getModifiedEntity();
        return modifiedEntity;
    }

    @Post
    @Mapping("/update")
    public User update(@Validated UserInput input) throws Exception {
//        final User modifiedEntity = userRepository.update(input);
        final SimpleSaveResult<User> result = this.sqlClient.update(input);
        final User modifiedEntity = result.getModifiedEntity();
        return modifiedEntity;
    }

    @Post
    @Mapping("/deleteByIds")
    @Tran
    public int deleteByIds(List<String> ids) throws Exception {
//        final int totalAffectedRowCount = userRepository.deleteByIds(ids, DeleteMode.AUTO);
        final DeleteResult result = this.sqlClient.deleteByIds(User.class, ids);
        final int totalAffectedRowCount = result.getTotalAffectedRowCount();
        return totalAffectedRowCount;
    }

    /**
     * 主动抛出异常 - 用于测试
     */
    @Get
    @Mapping("/exception")
    public Boolean exception() throws Exception {
        throw new NullPointerException("主动抛出异常 - 用于测试 " + DateUtil.now());
    }

}