1 Star 1 Fork 0

Letsenjoy / Mybatis-dsl

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

Mybatis-dsl

介绍

动态sql
不用写xml
会自动封装DTO


软件架构

####底层是

  • mybatis-plus
  • hutool
  • mybatis-dynamic-sql
  • spring
  • lombok
  • jeecgboot(可选,主要用到它的代码生成器)

注意:并不是说要引入这些依赖,这些依赖自动引入


安装教程

1.设置maven私服:在pom的<repositories>标签中添加

<repository>
    <id>netmi</id>
    <name>Red_K.-public</name>
    <url>http://118.31.175.228:8081/repository/Red_K.-public/</url>
</repository>

2.添加依赖

<dependency>
    <groupId>com.nm.red</groupId>
    <artifactId>rk-mybatis-ds</artifactId>
    <version>1.0.0-RELEASE</version>
</dependency>

3.如果在自己的setting中设置了镜像还需要排除私服

<mirrors>
       <mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>*,!jeecg,!jeecg-snapshots,!getui-nexus,!netmi</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
       </mirror> 
</mirrors>

使用说明(最佳实践)

为了方便描述假设有2张表
表1:tbl_user表

id username head_url
1 red google
2 yellow firefox
3 green 360

表2:tbl_money表

id value user_id
1 100 2
1 200 1

1.创建2张表对应的类

public class UserTable extends SqlTable {
    public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER);
    public final SqlColumn<String> username = column("username", JDBCType.VARCHAR);
    public final SqlColumn<String> headUrl = column("head_url", JDBCType.VARCHAR);
    public UserTable() {
        super("tbl_user");
    }
}
public class MoneyTable extends SqlTable {
    public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER);
    public final SqlColumn<Integer> value = column("value", JDBCType.INTEGER);
    public final SqlColumn<Integer> userId = column("user_id", JDBCType.INTEGER);
    public MoneyTable() {
        super("tbl_money");
    }
}

2.创建一个DB类将上面的类添加进去(这一步非必要,但是这样以后只要引入这个类就行了 比较方便)

public final class DB {
    public static final UserTable user = new UserTable();
    public static final MoneyTable money = new MoneyTable();
}

3.mapper继承RKMapper Service继承RKService 这样新的Service 将同时具有Mybatis-plus 的功能
public interface UserMapper extends RKMapper<User>
public interface IUserService extends IRKService<User> public class UserServiceImpl extends RKServiceImpl<UserMapper, UserMapper, User> implements IZdMemberService

4.在Controller引入我们的DB
import static *.*.*.DB

5.使用

假设要查询用户id=1的用户名和金额

1.创建DTO(主表---tbl_user)

public class UserDTO{
private String username;
private Integer value;
//这里也可以将字段名命名为表名  下换线或驼峰都可以
@RKOneToMany("tblMoney")
private List<MoneyDTO> record;
}

2.创建DTO(副表---tbl_money)

public class MoneyDTO{
private Integer value;
}

3.使用service查询

public class TestController{
   //省略一些代码    
   public void getInfo(){
       UserDTO userDTO = iUserService.rkQueryWrapper()
       .autoWrapper()
       .from(user)
       .rkLeftJoin(money, user.id, money.userId)    
       .rkSelect(user.username,money.value)
       .rkEq(user.id,1)
       .rkOne(UserDTO.class);
   }
}

Comments ( 2 )

Sign in for post a comment

About

Cancel

Releases (1)

All

Contributors

All

Activities

load more
can not load any more
Java
1
https://gitee.com/letsenjoy/mybatis-dsl.git
git@gitee.com:letsenjoy/mybatis-dsl.git
letsenjoy
mybatis-dsl
Mybatis-dsl
master

Search