首页 > 其他分享 >4.实现CRUD功能

4.实现CRUD功能

时间:2022-09-01 17:25:47浏览次数:64  
标签:功能 generator 项目 实现 true CRUD 模块 org com

1.逆向工程生成代码

参考:https://gitee.com/renrenio/renren-generator

1. 把renren-generator项目加入到webshop项目中
2. 修改renren-generator项目数据库配置application.yml文件,即设置为需要逆向工程的数据库;
3. 把项目中controller层模版中的有关权限的部分先注释,在resources/template/Controller.java.vm,即后续会把安全组件shiro替换为SpringSecurity;
4. 修改代码生成器配置信息generator.properties,修改四处:mainPath、package、moduleName、tablePrefix,对应到相应的表;

5. 启动renren-generator项目,访问 http://localhost:80 ,全选表,点击生成代码
6. 会生成一个压缩包,解压后里面会有main文件夹和sq文件,复制这里的main文件夹替换对应项目的main文件夹
7. 生成的文件中,resources文件夹下面的src文件夹可以删除,这是自动生成的前端文件
8. 依次将其他的模块,生成相应模块的代码:
  a.修改application.yml:      数据库名称
  b.修改generator.properties: moduleName、tablePrefix
  c.启动项目,生成代码并替换

2. 提取基础公共模块

启动项目时,会发现有很多的公共基础类,所以将这些公共类提取出来,创建一个公共模块 webshop-common;
在项目运行过程中,将公共的、基础的工具类放入该模块;

2.1数据库相关模块依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>
<!--mybatis-plus依赖-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.1</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.13</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.79</version>
</dependency>
<!--lombok依赖 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.8</version>
</dependency>

2.2其他常用模块依赖

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>	 
    <artifactId>httpcore</artifactId>
    <version>4.4.13</version>
</dependency>
<!--commons-lang依赖-->
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>
<!--servlet依赖-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>

3. 实现简单的CRUD功能

以商品 product 为例,实现CRUD功能;

  1. 将基础模块 webshop-common 添加到该项目依赖中:
<dependency>
    <groupId>org.example</groupId>
    <artifactId>webshop-common</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
  1. 添加数据库配置:
server:
  port: 8001
spring:
  application:
    name: product-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/pms?characterEncoding=UTF-8&useSSL=false
      username: root
      password: 123456
      initial-size: 10
      max-active: 100
      min-idle: 10
      max-wait: 60000
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      #Oracle需要打开注释
      #validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        #login-username: admin
        #login-password: admin
      filter:
        stat:
          log-slow-sql: true
          slow-sql-millis: 1000
          merge-sql: false
        wall:
          config:
            multi-statement-allow: true
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
  1. 启动类为:
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("org.example.product")
public class ProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class, args);
    }
}
  1. 启动项目,可正常启动,访问:http://localhost:8001/product/brand/list 可正常返回数据。
  2. 测试其他接口,功能正常。
  3. 其他模块依次进行。

标签:功能,generator,项目,实现,true,CRUD,模块,org,com
From: https://www.cnblogs.com/lailix/p/16646126.html

相关文章