前言
通過前面的多章學(xué)習(xí),相信大家對于springboot整合各個基礎(chǔ)依賴已經(jīng)得心應(yīng)手了。并且,對于各個依賴的選擇和使用都有了一定的理解。本章,將對前段時間的所學(xué)做一個總結(jié),會完成一個可以上生產(chǎn)的項(xiàng)目基礎(chǔ)架構(gòu)。
首先,我們來歸納一下一般項(xiàng)目的需求:
- 多數(shù)據(jù)源
- 分布式事務(wù)
- 數(shù)據(jù)持久層
- 緩存
- 日志記錄
- druid數(shù)據(jù)源監(jiān)控
- api文檔
以上需求基本囊括了所有項(xiàng)目的基礎(chǔ)。那么我們,根據(jù)需求,對應(yīng)我們的前幾章所學(xué),不難得出springboot+mybatis+多數(shù)據(jù)源+druid+atomikos+redis+log+swagger2 的搭配,另外,項(xiàng)目多環(huán)境的情況,還必須提供切換環(huán)境的配置。為了解決mybatis寫entity個mapper.xml文件麻煩的問題,我們需要引入自動生成插件mybatis-generator。
再者,這邊我們考慮到項(xiàng)目的解耦,還會在將整個項(xiàng)目拆分成多個獨(dú)立的模塊,這在實(shí)際開發(fā)過程中也是很重要的一部分。
項(xiàng)目結(jié)構(gòu)定義

如圖,我們把一個項(xiàng)目拆成了共4個模塊
mr-entity 存放實(shí)體類(mapper類,mapper.xml文件)
mr-service 存放業(yè)務(wù)邏輯類
mr-utils 存放工具類,如redis等等工具
web-service 真正的web服務(wù)應(yīng)用,依賴于以上三個模塊。
關(guān)于項(xiàng)目拆分成多個模塊的好處如下:
- 模塊與模塊松耦合,通過依賴來調(diào)用
- 重復(fù)的代碼可以單獨(dú)抽成一個模塊共用
- 打包時可以分模塊打包,做個java開發(fā)的都知道,項(xiàng)目會越來越大,后續(xù)整個項(xiàng)目打包編譯時間會很長,所以多模塊很有必要
- 不同的模塊可以分配給不同的人開發(fā)維護(hù),彼此不會沖突
創(chuàng)建父項(xiàng)目


注意,使用IDEA創(chuàng)建父項(xiàng)目時選擇的是

創(chuàng)建父項(xiàng)目完成后,我們將項(xiàng)目下的src目錄刪掉,只保留pom.xml文件即可。
創(chuàng)建子項(xiàng)目
右鍵父項(xiàng)目


同樣的操作依次創(chuàng)建
mr-entity
mr-service
mr-utils
web-service
這幾個子模塊,同時注意,除了web-service這個模塊,將其它子模塊的無關(guān)文件意義刪除,精簡項(xiàng)目結(jié)構(gòu)。
刪除后結(jié)構(gòu)如下
mr-entity
刪除
.mvn
src/test
.gitignore
HELP.md
mvnw
mvnw.cmd
刪除java文件夾下的xxxxxxApplication入口文件(因?yàn)閑ntity模塊只是個公共類模塊,無需運(yùn)行)

mr-service
刪除
.mvn
src/test
.gitignore
HELP.md
mvnw
mvnw.cmd
刪除java文件夾下的xxxxxxApplication入口文件

mr-utils
刪除
.mvn
src/test
.gitignore
HELP.md
mvnw
mvnw.cmd
刪除java文件夾下的xxxxxxApplication入口文件

最后,強(qiáng)調(diào)下,web-service作為一個服務(wù),不用做任何處理,不需要刪除文件!
至此,精簡后的多模塊項(xiàng)目已經(jīng)創(chuàng)建完畢,下面我們來通過pom文件來建立父子關(guān)系。
多模塊父子關(guān)系建立
我們在父pom.xml添加:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<!-- 申明有哪些子模塊 -->
<packaging>pom</packaging>
<modules>
<module>mr-entity</module>
<module>mr-service</module>
<module>mr-utils</module>
<module>web-service</module>
</modules>
然后我們在各個子模塊的pom.xml文件依次添加父依賴
<!-- 申明父模塊 -->
<parent>
<groupId>com.mrcoder</groupId>
<artifactId>mrcoder-service</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
同時,除了web-service這個子模塊,刪除其它子模塊的 <dependencies>和 <build>標(biāo)簽。
添加依賴和子模塊之間的依賴關(guān)系
好了,通過以上的操作,父子模塊關(guān)系很清晰了,但是子模塊與子模塊之間的依賴關(guān)系還沒有建立(意思就是,mr-entity作為實(shí)體類模塊,肯定會被mr-service模塊使用,那么如何在mr-service模塊內(nèi)使用mr-entity的實(shí)體類呢?)
接下來 ,我們來修改pom.xml文件建立這種依賴關(guān)系
mr-entity模塊不依賴任務(wù)子模塊,所以無需任務(wù)改動,關(guān)于注釋的build信息,大家先不用管。
mr-entity/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.mrcoder</groupId>
<artifactId>mr-entity</artifactId>
<version>0.0.1</version>
<name>mr-entity</name>
<description>mr-entity</description>
<parent>
<groupId>com.mrcoder</groupId>
<artifactId>mrcoder-service</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<!--<build>-->
<!--<plugins>-->
<!--<plugin>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-maven-plugin</artifactId>-->
<!--</plugin>-->
<!--<!– mybatis generator 自動生成代碼插件 –>-->
<!--<plugin>-->
<!--<groupId>org.mybatis.generator</groupId>-->
<!--<artifactId>mybatis-generator-maven-plugin</artifactId>-->
<!--<version>${mybatis-generator.version}</version>-->
<!--<configuration>-->
<!--<!– 對應(yīng)generator配置文件的路徑 –>-->
<!--<configurationFile>src/main/resources/generator/masterGenerator.xml</configurationFile>-->
<!--<!–<configurationFile>src/main/resources/generator/slaveGenerator.xml</configurationFile>–>-->
<!--<overwrite>true</overwrite>-->
<!--<verbose>true</verbose>-->
<!--</configuration>-->
<!--</plugin>-->
<!--</plugins>-->
<!--</build>-->
</project>
mr-service模塊依賴mr-utils和mr-entity
mr-service/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.mrcoder</groupId>
<artifactId>mr-service</artifactId>
<version>0.0.1</version>
<name>mr-service</name>
<description>mr-service</description>
<parent>
<groupId>com.mrcoder</groupId>
<artifactId>mrcoder-service</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.mrcoder</groupId>
<artifactId>mr-entity</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.mrcoder</groupId>
<artifactId>mr-utils</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
</project>
mr-utils不依賴任何子模塊
mr-utils/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.mrcoder</groupId>
<artifactId>mr-utils</artifactId>
<version>0.0.1</version>
<name>mr-utils</name>
<description>mr-utils</description>
<parent>
<groupId>com.mrcoder</groupId>
<artifactId>mrcoder-service</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
</project>
最后我們看真正的對外服務(wù)模塊web-service(大家實(shí)際開發(fā)時最好在模塊的命名上和其它子模塊區(qū)分開,比如作者就喜歡加個-service后綴來代表它是真正的對外服務(wù)模塊)
web-service依賴其它的所有子模塊,其它子模塊其實(shí)就是web-service拆分出去的~
web-service/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.mrcoder</groupId>
<artifactId>web-service</artifactId>
<version>0.0.1</version>
<name>web-service</name>
<description>web-service</description>
<parent>
<groupId>com.mrcoder</groupId>
<artifactId>mrcoder-service</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.mrcoder</groupId>
<artifactId>mr-entity</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.mrcoder</groupId>
<artifactId>mr-utils</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.mrcoder</groupId>
<artifactId>mr-service</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
<!-- 需要打包的模塊才帶build信息,不需要的請打包時注釋 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 主程序需要知道m(xù)ain class -->
<mainClass>com.mrcoder.webservice.WebServiceApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依賴的包都打包到生成的Jar包中-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
關(guān)于build塊,大家先不用管,這是關(guān)于打包代碼,后續(xù)作者會講。
至此模塊間的依賴關(guān)系已建立完成
添加依賴
我們將使用到的所有依賴加入到pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.mrcoder</groupId>
<artifactId>mrcoder-service</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<!-- 申明有哪些子模塊 -->
<packaging>pom</packaging>
<modules>
<module>mr-entity</module>
<module>mr-service</module>
<module>mr-utils</module>
<module>web-service</module>
</modules>
<properties>
<java.version>1.8</java.version>
<sb.version>2.1.4.RELEASE</sb.version>
<mybatis.version>2.0.0</mybatis.version>
<druid.version>1.1.16</druid.version>
<mysql.version>8.0.11</mysql.version>
<redis.version>1.4.7.RELEASE</redis.version>
<fastjson.version>1.2.57</fastjson.version>
<mybatis-generator.version>1.3.2</mybatis-generator.version>
<swagger.version>2.6.1</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${sb.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${sb.version}</version>
<scope>test</scope>
</dependency>
<!-- 引入依賴 start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${sb.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- 添加對jta事務(wù)支持的依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
<version>${sb.version}</version>
</dependency>
<!-- mysql 8.0+ 對druid 1.1.10 xa事務(wù)支持有問題,so 兩個都用release版解決問題 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 添加對aop支持的依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>${sb.version}</version>
</dependency>
<!-- 添加對redis支持的依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${redis.version}</version>
</dependency>
<!-- json支持 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- 引入 swagger等相關(guān)依賴 -->
<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>
<!-- 引入依賴 end -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!--默認(rèn)關(guān)掉單元測試 -->
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
關(guān)于依賴有和作用已在代碼中注釋,另外,仔細(xì)的同學(xué)肯定發(fā)現(xiàn)了,作者將版本號單獨(dú)使用了<properties>標(biāo)簽包住了,這也是常用的方式,目的是為了方便的管理依賴版本,統(tǒng)一管理。
接下來,我們來完善各個子模塊的代碼。
mr-entity
結(jié)構(gòu)如下

手動建立各個package和類文件
entity/master/Student
package com.mrcoder.mrentity.entity.master;
import java.io.Serializable;
public class Student implements Serializable {
private Long id;
private String name;
private int age;
private int grade;
public Student() {
}
public Student(String name, int age, int grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", grade=" + grade +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
entity/slave/Teacher
package com.mrcoder.mrentity.entity.slave;
import java.io.Serializable;
public class Teacher implements Serializable {
private Long id;
private String name;
private int age;
private int course;
public Teacher() {
}
public Teacher(String name, int age, int course) {
this.name = name;
this.age = age;
this.course = course;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
", course='" + course + '\'' +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getCourse() {
return course;
}
public void setCourse(int course) {
this.course = course;
}
}
mapper/master/StudentMapper
package com.mrcoder.mrentity.mapper.master;
import com.mrcoder.mrentity.entity.master.Student;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface StudentMapper {
// @Select("SELECT * FROM student")
List<Student> getList();
// @Select("SELECT * FROM student WHERE id = #{id}")
Student getById(Long id);
// @Insert("INSERT INTO student(age,grade,name) VALUES(#{age}, #{grade}, #{name})")
Integer insert(Student student);
// @Update("UPDATE student SET name=#{name},age=#{age}, grade=#{grade} WHERE id =#{id}")
Integer update(Student student);
// @Delete("DELETE FROM student WHERE id =#{id}")
Integer delete(Long id);
//注解方式,本項(xiàng)目不建議使用
@Select("SELECT * FROM student")
List<Student> getListByAnno();
}
mapper/slave/TeacherMapper
package com.mrcoder.mrentity.mapper.slave;
import com.mrcoder.mrentity.entity.slave.Teacher;
import java.util.List;
public interface TeacherMapper {
// @Select("SELECT * FROM teacher")
List<Teacher> getList();
// @Select("SELECT * FROM teacher WHERE id = #{id}")
Teacher getById(Long id);
// @Insert("INSERT INTO teacher(age,course,name) VALUES(#{age}, #{course}, #{name})")
void insert(Teacher teacher);
// @Update("UPDATE teacher SET name=#{name},age=#{age}, course=#{course} WHERE id =#{id}")
void update(Teacher teacher);
// @Delete("DELETE FROM teacher WHERE id =#{id}")
void delete(Long id);
}
resources/mapper/master/StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mrcoder.mrentity.mapper.master.StudentMapper">
<resultMap id="BaseResultMap" type="com.mrcoder.mrentity.entity.master.Student">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
<result column="grade" property="grade" javaType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List">
id, name, age, grade
</sql>
<select id="getList" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM student
</select>
<select id="getById" parameterType="java.lang.Long" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM student
WHERE id = #{id}
</select>
<insert id="insert" parameterType="com.mrcoder.mrentity.entity.master.Student">
INSERT INTO
student
(age,grade,name)
VALUES
(#{age}, #{grade}, #{name})
</insert>
<update id="update" parameterType="com.mrcoder.mrentity.entity.master.Student">
UPDATE
student
SET
age = #{age},
grade = #{grade},
name = #{name}
WHERE
id = #{id}
</update>
<delete id="delete" parameterType="java.lang.Long">
DELETE FROM
student
WHERE
id =#{id}
</delete>
</mapper>
resources/mapper/master/TeacherMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mrcoder.mrentity.mapper.slave.TeacherMapper">
<resultMap id="BaseResultMap" type="com.mrcoder.mrentity.entity.slave.Teacher">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
<result column="course" property="course" javaType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List">
id, name, age, course
</sql>
<select id="getList" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM teacher
</select>
<select id="getById" parameterType="java.lang.Long" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM teacher
WHERE id = #{id}
</select>
<insert id="insert" parameterType="com.mrcoder.mrentity.entity.slave.Teacher">
INSERT INTO
teacher
(age,course,name)
VALUES
(#{age}, #{course}, #{name})
</insert>
<update id="update" parameterType="com.mrcoder.mrentity.entity.slave.Teacher">
UPDATE
teacher
SET
age = #{age},
course = #{course},
name = #{name}
WHERE
id = #{id}
</update>
<delete id="delete" parameterType="java.lang.Long">
DELETE FROM
teacher
WHERE
id =#{id}
</delete>
</mapper>
關(guān)于resources/generator/文件夾下的內(nèi)容,“熟讀”我前幾章的同學(xué)肯定知道,這倆個xml文件是mybatis用來自動生成entity和mapper文件的,這里不多贅述,不懂的同學(xué)可以百度“mybatis自動生成代碼”查詢。
這邊順帶解釋下mr-entity/pom.xml中的

注釋的代碼是用mybatis用來自動生成代碼的,細(xì)心的同學(xué)發(fā)現(xiàn)了,

這邊有兩個配置文件,一個是master,一個是slave,我們可以在mr-entity的結(jié)構(gòu)中發(fā)現(xiàn),所有的類文件都是通過master和slave包來區(qū)分兩個數(shù)據(jù)源的,也就是master數(shù)據(jù)源的實(shí)體類就放到master包內(nèi)~,生成代碼的邏輯也是一樣,所以這邊會有兩個配置文件,大家使用時,用到master自動生成時請注釋slave的配置,用到slave就注釋master。(這一塊作者暫時沒有找到能同時生成master和slave多個數(shù)據(jù)源代碼而且不需要改配置的方式,后續(xù)有時間會去研究下)
最后,強(qiáng)調(diào)一下,關(guān)于master和slave命名的問題,不是我們理解的主從數(shù)據(jù)庫關(guān)系,僅僅是作者習(xí)慣命名,和主從沒有任何關(guān)系,各位看官可以自由命名!
關(guān)于resources/generator/masterGenerator.xml和resources/generator/slaveGenerator.xml,有兩個地方需要大家根據(jù)實(shí)際情況修改后才能使用

master數(shù)據(jù)源生成代碼的配置文件
resources/generator/masterGenerator.xml
<?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>
<!-- 本地?cái)?shù)據(jù)庫驅(qū)動程序jar包的全路徑 -->
<classPathEntry
location="D:\MaveRepository\mysql\mysql-connector-java\8.0.15\mysql-connector-java-8.0.15.jar"/>
<context id="context" targetRuntime="MyBatis3">
<!-- optional,旨在創(chuàng)建class時,對注釋進(jìn)行控制 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 數(shù)據(jù)庫的相關(guān)配置 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://192.168.145.131:3306/test"
userId="root" password="123456"/>
<!-- 非必需,類型處理器,在數(shù)據(jù)庫類型和java類型之間的轉(zhuǎn)換控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- Model實(shí)體類生成的位置 -->
<javaModelGenerator targetPackage="com.mrcoder.mrentity.entity.master" targetProject="src/main/java">
<!-- 是否對model添加 構(gòu)造函數(shù) -->
<property name="constructorBased" value="false"/>
<!-- 是否允許子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="true"/>
<!-- 建立的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構(gòu)造方法 -->
<property name="immutable" value="false"/>
<!-- 給Model添加一個父類 -->
<!--<property name="rootClass" value="com.foo.louis.Hello"/>-->
<!-- 是否對類CHAR類型的列的數(shù)據(jù)進(jìn)行trim操作 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- *Mapper.xml 文件的位置 -->
<sqlMapGenerator targetPackage="mapper/master" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- Mapper 接口文件的位置 -->
<javaClientGenerator targetPackage="com.mrcoder.mrentity.mapper.master" targetProject="src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
<!--
定義Maper.java 源代碼中的ByExample() 方法的可視性,可選的值有:
public;
private;
protected;
default
注意:如果 targetRuntime="MyBatis3",此參數(shù)被忽略
-->
<property name="exampleMethodVisibility" value=""/>
<!--方法名計(jì)數(shù)器Important note: this property is ignored if the target runtime is MyBatis3.-->
<property name="methodNameCalculator" value=""/>
<!--為生成的接口添加父接口-->
<property name="rootInterface" value=""/>
</javaClientGenerator>
<!-- 相關(guān)表的配置 -->
<table tableName="person" enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
slave數(shù)據(jù)源生成代碼的配置文件
resources/generator/slaveGenerator.xml
<?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>
<!-- 本地?cái)?shù)據(jù)庫驅(qū)動程序jar包的全路徑 -->
<classPathEntry location="D:\MaveRepository\mysql\mysql-connector-java\8.0.15\mysql-connector-java-8.0.15.jar"/>
<context id="context" targetRuntime="MyBatis3">
<!-- optional,旨在創(chuàng)建class時,對注釋進(jìn)行控制 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 數(shù)據(jù)庫的相關(guān)配置 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://192.168.145.131:3306/test2"
userId="root" password="123456"/>
<!-- 非必需,類型處理器,在數(shù)據(jù)庫類型和java類型之間的轉(zhuǎn)換控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- Model實(shí)體類生成的位置 -->
<javaModelGenerator targetPackage="com.mrcoder.mrentity.entity.slave" targetProject="src/main/java">
<!-- 是否對model添加 構(gòu)造函數(shù) -->
<property name="constructorBased" value="false"/>
<!-- 是否允許子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="true"/>
<!-- 建立的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構(gòu)造方法 -->
<property name="immutable" value="false"/>
<!-- 給Model添加一個父類 -->
<!--<property name="rootClass" value="com.foo.louis.Hello"/>-->
<!-- 是否對類CHAR類型的列的數(shù)據(jù)進(jìn)行trim操作 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- *Mapper.xml 文件的位置 -->
<sqlMapGenerator targetPackage="mapper/slave" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- Mapper 接口文件的位置 -->
<javaClientGenerator targetPackage="com.mrcoder.mrentity.mapper.slave" targetProject="src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
<!--
定義Maper.java 源代碼中的ByExample() 方法的可視性,可選的值有:
public;
private;
protected;
default
注意:如果 targetRuntime="MyBatis3",此參數(shù)被忽略
-->
<property name="exampleMethodVisibility" value=""/>
<!--方法名計(jì)數(shù)器Important note: this property is ignored if the target runtime is MyBatis3.-->
<property name="methodNameCalculator" value=""/>
<!--為生成的接口添加父接口-->
<property name="rootInterface" value=""/>
</javaClientGenerator>
<!-- 相關(guān)表的配置 -->
<table tableName="orders" enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
項(xiàng)目地址
https://github.com/MrCoderStack/SpringBootFrame
請關(guān)注我的訂閱號
