首先创建你父工作,没有什么可以说直接上图:

Maven 构建SpringCloud 微服务 springcloud maven多模块_java

什么不用选择直接next,填写一个父项目名称如图:

Maven 构建SpringCloud 微服务 springcloud maven多模块_spring boot_02

点击finish.删除不用的文件只保留pom.xml如图:

Maven 构建SpringCloud 微服务 springcloud maven多模块_spring_03

 

 

父工程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>org.example</groupId>
    <artifactId>demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>demo-dao</module>
        <module>demo-service</module>
        <module>demo-web</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <url>http://maven.apache.org</url>

    <name>demo-mybatis</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定该Main Class为全路径 -->
                    <mainClass>com.example.web.DemoWebApplication</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

一次按建父工的方式创建另外子项目demo-dao,demo-service.没有什么说的,直接粘贴出来POM文件配置:

demo-serivce pom.xml: 依赖demo-dao

<?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">
    <parent>
        <artifactId>demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>demo-service</artifactId>

    <name>demo-service</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>demo-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

demo-dao 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">
    <parent>
        <artifactId>demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-dao</artifactId>
    <dependencies>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>

</project>

demo-web项目有些跟上面两个demo-service,demo-dao不一样的地方。

直接使用idea创建spring initial项目如图:

Maven 构建SpringCloud 微服务 springcloud maven多模块_java_04

 

 

 

 

 

创建好后如图:

Maven 构建SpringCloud 微服务 springcloud maven多模块_maven_05

 

 

 

 

 

 

 

 

 

 

demo-web 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">
    <parent>
        <artifactId>demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-dao</artifactId>
    <dependencies>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>

</project>

demo-web这样创建不会是父工作demo的子项目是一个独立的项目,所以要修改demo.xml 把demo-web加到demo父工程里pom.mxl,修改demo-web工程的父工程为demo,修改如上。

当然也可以不这样创建,但是有些配置文件如:application.properties,DemoWebApplication运行类也可以自己创建。

demo-web中application.properties文件中配置如下:

debug=true
server.port=8002
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.application.name=demo-web
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.entity

DemoWebApplication.java文件头配置:

@SpringBootApplication(scanBasePackages = {"com.example"})
//@EnableCaching(proxyTargetClass = true)
@MapperScan(basePackages = {"com.example.mapper"})
public class DemoWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoWebApplication.class, args);
    }

}

这里@MapperScan(basePackages = {"com.example.mapper"})这个不配置会发现不了*mapper.xml,@SpringBootApplication(scanBasePackages = {"com.example"})最好也配置上,不然不扫描一子项目jar包类,导致类找不到,默认扫描run类包路径下的类。

其它就是一些基本的MVC的一路流畅的操作。

UserController.java

@RestController
public class UserController {

    @Resource
    private UserService userService;


    @RequestMapping("/all")
    public List select(){
        //处理get请求,获取用户列表
        return userService.select();
    }
}

UserService.java UserServiceImpl.java

public interface UserService {
    List selectByName(String name);
    int insert(User user);
    void update(User user);
    void delete(Long id);
    User verify(User user);
    List select();
}

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List selectByName(String name) {
        return null;
    }

    @Override
    public int insert(User user) {
        return 0;
    }

    @Override
    public void update(User user) {

    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public User verify(User user) {
        return null;
    }

    @Override
    public List select() {
        return userMapper.select();
    }

}

UserMapper.java。注意这个类名要与mapper.xml文件名一致,还有就是没有实现类(spring会动态代理创建实现类)。@Component("userMapper")还有这全一定要加,不然会报userMapper bean can not find.

@Component("userMapper")
public interface UserMapper {
    List selectByName(String name);
    int insert(User user);
    void update(User user);
    void delete(Long id);
    User verify(User user);
    List select();
}

UserMapper.xml:

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
    <resultMap id="userResultMap" type="com.example.entity.User">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="pwd" property="pwd"/>
        <result column="sex" property="sex"/>
        <result column="mobile" property="mobile"/>
        <result column="email" property="email"/>
        <result column="avatar" property="avatar"/>
        <result column="status" property="status"/>
    </resultMap>

    <select id="selectByName" resultMap="userResultMap" parameterType="java.lang.String">
        select * from `user` where `name`=#{name}
    </select>

    <select id="verify" resultMap="userResultMap" parameterType="com.example.entity.User">
        select * from `user` where name=#{name} and pwd=#{pwd}
    </select>

    <select id="select" resultMap="userResultMap" >
        select * from `user`
    </select>

    <insert id="insert" parameterType="com.example.entity.User">
        INSERT into `user` (`name`,pwd,sex,mobile,email,avatar,status) values (#{name},#{pwd},#{sex},#{mobile},#{email},#{avatar},#{status})
    </insert>

    <update id="update" parameterType="com.example.entity.User">
        update `user` set name=#{name},pwd=#{pwd},sex=#{sex},mobile=#{mobile},email=#{email},avatar=#{avatar},status=#{status} where id=#{id}
    </update>

    <delete id="delete" parameterType="java.lang.Long">
        DELETE from `user` where id=#{id}
    </delete>

</mapper>

依然是熟悉的模样。

sql如下:

CREATE TABLE  USER (
  `ID` bigint(20) NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
  `NAME` varchar(50) DEFAULT NULL COMMENT '姓名',
  `PWD` varchar(50) NOT NULL COMMENT '密码',
  `SEX` varchar(3) DEFAULT '' COMMENT '性别',
  `MOBILE` varchar(20) DEFAULT NULL COMMENT '手机',
  `EMAIL` varchar(150) DEFAULT NULL COMMENT 'EMAIL',
  `AVATAR` varchar(20) DEFAULT NULL COMMENT '昵称',
  `status` varchar(500) DEFAULT 'Y' COMMENT '有效状态'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPRESSED COMMENT='用户表';

都是基本操作,没有什么可解释。

User.java类

public class User {
    private Long id;
    private String name;
    private String pwd;
    private Character sex;
    private String mobile;
    private String email;
    private String avatar;
    private String status;


    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                ", sex=" + sex +
                ", mobile='" + mobile + '\'' +
                ", email='" + email + '\'' +
                ", avatar='" + avatar + '\'' +
                ", status='" + status + '\'' +
                '}';
    }

    public User() {
    }

    public User(String name, String pwd, Character sex, String mobile, String email, String avatar, String status) {
        this.name = name;
        this.pwd = pwd;
        this.sex = sex;
        this.mobile = mobile;
        this.email = email;
        this.avatar = avatar;
        this.status = status;
    }

    public User(String name, String pwd) {
        this.name = name;
        this.pwd = pwd;
    }

    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 String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public Character getSex() {
        return sex;
    }

    public void setSex(Character sex) {
        this.sex = sex;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

这些基本可以生成,mapper.java User.java等,可自行搜索。

如果是idea IDE,会是这样的效果:

Maven 构建SpringCloud 微服务 springcloud maven多模块_java_06

 

 

 

 

 

 

 

 

点击这个绿色箭头会跳转到mapper.xml文件,相反,mapper.xml也有同样的绿色箭头,也可以路转到Mapper.java.

这样就可以启动项目了。

中间遇到不少问题。

1.在pom.xml中配置问题。这个一定要配置。不做打包不全文件打包不进jar里面,导致打包成功,启动不成功,说找不到相就类。

<resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.Field userMapper in com.example.impl.UserServiceImpl required a bean of type 'com.example.mapper.UserMapper' that could not be found.这个问题未解,把项目删除重新按上面步骤创建,成功了.可能是我创建项目时候问题。

3.No qualifying bean of type 'com.example.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

这个问题在UserServiceImpl.java里面配置使用@Autowired出现的。

@Autowired
    private UserMapper userMapper;

后来改为:

@Resource
    private UserMapper userMapper;

The bean 'userMapper' could not be injected as a 'com.example.mapper.UserMapper' because it is a JDK dynamic proxy that implements:
Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.

cause by :
Bean named 'userMapper' is expected to be of type 'com.example.mapper.UserMapper' but was actually of type 'com.sun.proxy.$Proxy57'

这个问题,网上搜索不少地方,就是问题依旧。提示说在运行类上面加个注解:

@EnableCaching(proxyTargetClass = true)加上也是同样的问题,感觉跟没加一个样。

不知道是创建版本问题,还是创建项目方式不对问题。问题没有排查到。重建一个项目没有出现。