从git clon出人人开源的renren-generator
将renren-generator文件夹复制到项目中
在项目pom.xml中添加renren-generator
<module>renren-generator</module>
在renren-generator的配置文件中找到application.yml
修改其中的数据库地址及表名
url: jdbc:mysql://192.168.56.10:3306/gulimall_pms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
其中数据库地址为:
192.168.56.10:3306
表名为:
gulimall_pms
修改完成后还需要修改配置文件中的generator.properties
//主路径名
mainPath=com.atguigu
#包名
package=com.atguigu.gulimall
#表示需要执行代码生成器的模块名
moduleName=product
#作者名
author=shenli
#Email
email=601163371@qq.com
#表前缀(类名不会包含表前缀)
tablePrefix=pms_
关键是
moduleName=product
和
tablePrefix=pms_
moduleName表示了idea中的模块名
tablePrefix表示了可以取消生成数据库中的表前缀
执行renren-generator
的
RenrenApplication
进入此界面,全选后点击生成代码会下载压缩包
压缩包中有main文件夹,将生成的main文件夹复制到项目对应的模块src/main文件夹处,就可以获得项目生成的文件
此时我们发现逆向生成的代码中很多注解,依赖都没有,需要慢慢导入
创建gulimall-common的maven文件,让其成为公共的模块
让各个微服务模块依赖gulimall-common
然后在gulimall-common中添加两个依赖
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- 只要装了lombok插件.就可以使用@Data注解自动表示生成了get set等方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
在java目录下创建com.atguigu.common目录将renren-fast中的src/main/java/io.renren/common/utils包复制到com.atguigu.common目录下
还有renren-fast中的xss包
删除此路径下的所有@RequiresPermissions(“product:attrattrgrouprelation:list”)注解修改逆向工程的模板,将@RequiresPermissions注解注释掉
重启renren-generator
重新生成product
删除不必要的xss文件
将renren-fast中的REException复制到common的utils中并修改import导入解决报错
1、配置数据源;
1)、导入数据库的驱动。https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-versions.html
2)、在application.yml配置数据源相关信息
2、配置MyBatis-Plus;
1)、使用@MapperScan
2)、告诉MyBatis-Plus,sql映射文件位置
spring:
datasource:
username: root
password: root
url: jdbc:mysql://192.168.56.10:3306/gulimall_pms
driver-class-name: com.mysql.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
# 自增主键↑
报错
将对应的包复制到common中再次导入
报错
Failed to resolve org.junit.platform:junit-platform-launcher:1.8.2
Failed to resolve org.junit.vintage:junit-vintage-engine:5.8.2
导入包后又报错
取消导入包,将@Test注解改为import org.junit.Test;包下的Test,问题解决
测试时发现
@RunWith(SpringRunner.class)
@SpringBootTest
public class GulimallProductApplicationTests {
@Autowired
BrandService brandService;
@Test
void contextLoads() {
BrandEntity brandEntity = new BrandEntity();
brandEntity.setName("小米");
brandService.save(brandEntity);
System.out.println("保存成功...");
}
}
后来查询错误,需要在void contextLoads()方法前加上public ,问题解决
成功测试在数据库gulimall_pms中的pms_brdan表中增加数据
修改描述
BrandEntity brandEntity = new BrandEntity();
brandEntity.setBrandId(1L);
brandEntity.setDescript("金立");
brandService.updateById(brandEntity);
根据brand_id查询数据
List<BrandEntity> list = brandService.list(new QueryWrapper<BrandEntity>().eq("brand_id", 1L));
list.forEach((item)->{
System.out.println(item);
});