1.1 项目数据库设计
1.1.1 数据库表设计
2 搭建项目结构
1.IDEA创建项目类型为empty-project
1)创建mybatis逆向工程
创建maven项目,把表生成实体类,接口,mapper文件。
插件
存放在项目根目录下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径 -->
<!--<classPathEntry location="D:\course\server\mysql-connector-java-5.1.49.jar"/>-->
<!--
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
-->
<!--指定了这个依赖以上就不需要指定路径了-->
<!-- 配置table表信息内容体,targetRuntime指定采用MyBatis3的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ylb"
userId="root"
password="123456">
</jdbcConnection>
<!-- 生成model类,targetPackage指定model类的包名, targetProject指定生成的model放在eclipse的哪个工程下面-->
<javaModelGenerator targetPackage="com.bjpowernode.api.model"
targetProject="D:\Program\Java\Project\ylb\mapper\src\main\java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>
<!-- 生成MyBatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名, targetProject指定生成的mapper.xml放在eclipse的哪个工程下面 -->
<sqlMapGenerator targetPackage="mappers" targetProject="D:\Program\Java\Project\ylb\mapper\src\main\resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成MyBatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名, targetProject指定生成的Mapper接口放在eclipse的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.bjpowernode.dataservice.mapper" targetProject="D:\Program\Java\Project\ylb\mapper\src\main\java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 数据库表名及对应的Java模型类名 -->
<table tableName="b_product_info" domainObjectName="Product"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="b_bid_info" domainObjectName="BidInfo"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="b_income_record" domainObjectName="Income"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="b_recharge_record" domainObjectName="Recharge"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="u_user" domainObjectName="SysUser"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="u_finance_account" domainObjectName="FinanceAccount"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bjpowernode</groupId>
<artifactId>mapper</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mapper</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--mybatis代码自动生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2)创建父项目
3)创建micr-api项目
4)创建micr-dataservice项目
注意:
注意看这里爆红了,我们做以下修改。
在pom.xml(dataservice)中添加以下依赖。
<dependency>
<groupId>com.bjpowernode</groupId>
<artifactId>micr-api</artifactId>
<version>1.0.0</version>
</dependency>
5)添加父项目依赖
<!--首先把夫项目改为springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--添加依赖-->
<properties>
<java.version>17</java.version>
<dubbo.version>2.7.8</dubbo.version>
<zookeeper.version>2.7.8</zookeeper.version>
<mybatis.version>2.2.2</mybatis.version>
<fastjson>1.2.51</fastjson>
<swagger.version>2.9.2</swagger.version>
<swagger.ui.version>1.9.6</swagger.ui.version>
</properties>
<dependencyManagement>
<dependencies>
<!--声明项目使用的gav-->
<!--Dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!--Zookeeper-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${zookeeper.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--MyBatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--Fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson}</version>
</dependency>
<!--Swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${swagger.ui.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--添加插件-->
<build>
<plugins>
<!-- 编译插件 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<!-- 插件的版本 -->
<version>3.5.1</version>
<!-- 编译级别 -->
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- 编码格式 -->
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
6)添加micr-dataservice项目依赖
<!--把父项目改为自己的-->
<parent>
<groupId>com.bjpowernode</groupId>
<artifactId>micr-parent</artifactId>
<version>1.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--添加依賴-->
<dependencies>
<dependency>
<groupId>com.bjpowernode</groupId>
<artifactId>micr-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在pom.xml(父项目)添加聚合
<modules>
<module>../micr-api</module>
<module>../micr-dataservice</module>
</modules>
application.yml核心配置文件(在resources目录下)
#服务名称
spring:
application:
name: micr-dataservice
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ylb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: 123456
#设置mybatis
mybatis:
mapper-locations: classpath:/mappers/**/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
配置启动类 加上@MapperScan(“com.bjpowernode.dataservice.mapper”)
@MapperScan("com.bjpowernode.dataservice.mapper")
@SpringBootApplication
public class MicrDataserviceApplication {
public static void main(String[] args) {
SpringApplication.run(MicrDataserviceApplication.class, args);
}
}
项目运行以下效果
dataservice设置Dubbo
在pom.xml(data-servcie)中添加以下依赖
<!--Dubbo起步依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<type>pom</type>
</dependency>
application中添加dubbo服务配置
#Dubbo配置
dubbo:
registry:
address: zookeeper://localhost:2181
scan:
base-packages: com.bjpowernode.dataservice.service
provider:
timeout: 50000
retries: 0
在启动类上添加@EnableDubbo注解
//启动Dubbo服务
@EnableDubbo
@MapperScan("com.bjpowernode.dataservice.mapper")
@SpringBootApplication
public class MicrDataserviceApplication {
public static void main(String[] args) {
SpringApplication.run(MicrDataserviceApplication.class, args);
}
}
apache-zookeeper-3.5.5-bin.tar.gz
双击图标Zookeeper启动
运行项目
7)Web服务配置
将父项目改为自己的父项目
<parent>
<groupId>com.bjpowernode</groupId>
<artifactId>micr-parent</artifactId>
<version>1.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
在micr-parent中添加聚合
<modules>
<module>../micr-api</module>
<module>../micr-dataservice</module>
<module>../micr-web</module>
</modules>
web中的依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.bjpowernode</groupId>
<artifactId>micr-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<type>pom</type>
</dependency>
<!--Swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
</dependency>
</dependencies>
spring:
application:
name: micro-web
server:
port: 8000
servlet:
context-path: /api
encoding:
enabled: true
charset: utf-8
force: true
dubbo:
registry:
address: zookeeper://localhost:2181
scan:
base-packages: com.bjpowernode.front
consumer:
check: false
timeout: 50000
retries: 0
//启用Dubbo服务
@EnableDubbo
@SpringBootApplication
public class MicrWebApplication {
public static void main(String[] args) {
SpringApplication.run(MicrWebApplication.class, args);
}
}
点击运行是这个界面