一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 基于Mybatis plus 自動代碼生成器的實現代碼

基于Mybatis plus 自動代碼生成器的實現代碼

2021-04-30 14:30夏天的尾巴ljh Java教程

本文通過實例代碼給大家介紹了基于Mybatis-plus 自動代碼生成器的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

1.使用的是maven項目,添加依賴

?
1
2
3
4
5
6
<!-- mybatis-plus begin -->
   <dependency>
     <groupid>com.baomidou</groupid>
     <artifactid>mybatis-plus</artifactid>
     <version>2.2.0</version>
   </dependency>

還有數據庫的連接

?
1
2
3
4
5
<dependency>
    <groupid>mysql</groupid>
    <artifactid>mysql-connector-java</artifactid>
    <scope>compile</scope>
</dependency>

最后是源碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import com.baomidou.mybatisplus.generator.autogenerator;
import com.baomidou.mybatisplus.generator.config.datasourceconfig;
import com.baomidou.mybatisplus.generator.config.globalconfig;
import com.baomidou.mybatisplus.generator.config.packageconfig;
import com.baomidou.mybatisplus.generator.config.strategyconfig;
import com.baomidou.mybatisplus.generator.config.rules.dbtype;
import com.baomidou.mybatisplus.generator.config.rules.namingstrategy;
import org.junit.jupiter.api.test;
/**
 * mybatis-plus 自動生成代碼
 *
 * @author terry
 * @version 1.0
 * @date 2018-05-16 09:35
 */
public class simplemp {
  @test
  public void generatecode() {
    //指定包名
    string packagename = "com.hciot.hhhh";
    //user -> userservice, 設置成true: user -> iuserservice
    boolean servicenamestartwithi = false;
    //指定生成的表名
    string[] tablenames = new string[]{"data_air_sensor_co", "order_product", "relation_device_gateway"};
    generatebytables(servicenamestartwithi, packagename, tablenames);
  }
  /**
   * 根據表自動生成
   *
   * @param servicenamestartwithi 默認為false
   * @param packagename      包名
   * @param tablenames      表名
   * @author terry
   */
  private void generatebytables(boolean servicenamestartwithi, string packagename, string... tablenames) {
    //配置數據源
    datasourceconfig datasourceconfig = getdatasourceconfig();
    // 策略配置
    strategyconfig strategyconfig = getstrategyconfig(tablenames);
    //全局變量配置
    globalconfig globalconfig = getglobalconfig(servicenamestartwithi);
    //包名配置
    packageconfig packageconfig = getpackageconfig(packagename);
    //自動生成
    atuogenerator(datasourceconfig, strategyconfig, globalconfig, packageconfig);
  }
  /**
   * 集成
   *
   * @param datasourceconfig 配置數據源
   * @param strategyconfig  策略配置
   * @param config      全局變量配置
   * @param packageconfig  包名配置
   * @author terry
   */
  private void atuogenerator(datasourceconfig datasourceconfig, strategyconfig strategyconfig, globalconfig config, packageconfig packageconfig) {
    new autogenerator()
        .setglobalconfig(config)
        .setdatasource(datasourceconfig)
        .setstrategy(strategyconfig)
        .setpackageinfo(packageconfig)
        .execute();
  }
  /**
   * 設置包名
   *
   * @param packagename 父路徑包名
   * @return packageconfig 包名配置
   * @author terry
   */
  private packageconfig getpackageconfig(string packagename) {
    return new packageconfig()
        .setparent(packagename)
        .setxml("mapper")
        .setmapper("dao")
        .setcontroller("controller")
        .setentity("entity");
  }
  /**
   * 全局配置
   *
   * @param servicenamestartwithi false
   * @return globalconfig
   * @author terry
   */
  private globalconfig getglobalconfig(boolean servicenamestartwithi) {
    globalconfig globalconfig = new globalconfig();
    globalconfig
        .setbasecolumnlist(true)
        .setbaseresultmap(true)
        .setactiverecord(false)
        .setauthor("terry")
        //設置輸出路徑
        .setoutputdir(getoutputdir("mybatis-plus"))
        .setfileoverride(true);
    if (!servicenamestartwithi) {
      //設置service名
      globalconfig.setservicename("%sservice");
    }
    return globalconfig;
  }
  /**
   * 返回項目路徑
   *
   * @param projectname 項目名
   * @return 項目路徑
   * @author terry
   */
  private string getoutputdir(string projectname) {
    string path = this.getclass().getclassloader().getresource("").getpath();
    int index = path.indexof(projectname);
    return path.substring(1, index) + projectname + "/src/main/java/";
  }
  /**
   * 策略配置
   *
   * @param tablenames 表名
   * @return strategyconfig
   * @author terry
   */
  private strategyconfig getstrategyconfig(string... tablenames) {
    return new strategyconfig()
        // 全局大寫命名 oracle 注意
        .setcapitalmode(true)
        .setentitylombokmodel(false)
        // 表名、字段名、是否使用下劃線命名(默認 false)
        .setdbcolumnunderline(true)
        //從數據庫表到文件的命名策略
        .setnaming(namingstrategy.underline_to_camel)
        //需要生成的的表名,多個表名傳數組
        .setinclude(tablenames);
  }
  /**
   * 配置數據源
   *
   * @return 數據源配置 datasourceconfig
   * @author terry
   */
  private datasourceconfig getdatasourceconfig() {
    string dburl = "jdbc:mysql://localhost:3306/test";
    return new datasourceconfig().setdbtype(dbtype.mysql)
        .seturl(dburl)
        .setusername("root")
        .setpassword("root")
        .setdrivername("com.mysql.jdbc.driver");
  }
  /**
   * 根據表自動生成
   *
   * @param packagename 包名
   * @param tablenames 表名
   * @author terry
   */
  @suppresswarnings("unused")
  private void generatebytables(string packagename, string... tablenames) {
    generatebytables(true, packagename, tablenames);
  }
}

總結

以上所述是小編給大家介紹的基于mybatis plus 自動代碼生成器的實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://blog.csdn.net/weixin_41685100/article/details/80342246

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本偷拍xxxxxxww | 国产精品久久国产精品99盘 | 成年美女黄网站色视频大全免费 | gay中国| 四虎com | 99精品久久99久久久久久 | 女人叉开腿让男人捅 | 91av俱乐部 | 午夜办公室 | 情缘免费观看完整版 | 糖心视频在线观看 | 国产乱人乱精一区二区视频密 | haodiaocao的视频这里看 | 亚洲高清在线精品一区 | 金牛网155755水心论坛黄大父母 | 亚洲国产精品自产在线播放 | 青青草原国产 | 成人精品一区二区三区中文字幕 | 久久婷婷丁香五月色综合啪免费 | 99久久国语露脸精品国产 | 久草在线精彩免费视频 | 人人精品久久 | 国产精品青青青高清在线密亚 | 天海翼最新作品 | 99视频九九精品视频在线观看 | cosplay 极品videos | 美国大片成人性网 | 1717she精品视频在线观看 | 校服下的白嫩小乳尖h1v1 | 亚洲国产精品自产在线播放 | 放荡警察巨r麻麻出轨小说 范冰冰特黄xx大片 饭冈加奈子在线播放观看 法国老妇性xx在线播放 | 亚洲第6页 | 免费在线看a | 美女脱了内裤打开腿让人桶网站o | 我的妹妹最近有点怪在线观看 | 国产精品亚洲综合久久 | 跪趴好紧h | 日韩综合久久 | 亚洲国产成人资源在线桃色 | 蘑菇香蕉茄子绿巨人丝瓜草莓 | 欧洲男同直粗无套播放视频 |