SpringBoot + MyBatis 整合 和 Maven多模块运行、打包

avatar 2018年07月24日13:12:42 6 10947 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此
以前我们都是将各个层放在一个项目里,后期维护可能不太方便,代码结构也不清晰。

本文先如何使用 Maven 多模块开发,然后再讲一下 SpringBoot 和 MyBatis 整合

文件结构如下





其中 summer 是父项目,summer-dao、summer-service、summer-web 是子项目。

summer-web 依赖 summer-service,summer-service 依赖 summer-dao


一、新建项目


1、新建 Maven 项目,作为父项目,然后删除掉 src 之类的目录,只需要保留一个 pom.xml





pom.xml 内容如下
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>com.liuyanzhao.summer</groupId>
  7.     <artifactId>summer</artifactId>
  8.     <packaging>pom</packaging>
  9.     <version>1.0-SNAPSHOT</version>
  10.     <!--关键-->
  11.     <modules>
  12.         <module>summer-service</module>
  13.         <module>summer-web</module>
  14.         <module>summer-dao</module>
  15.     </modules>
  16.     <parent>
  17.         <groupId>org.springframework.boot</groupId>
  18.         <artifactId>spring-boot-starter-parent</artifactId>
  19.         <version>1.5.9.RELEASE</version>
  20.         <relativePath/> <!-- lookup parent from repository -->
  21.     </parent>
  22.     <properties>
  23.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  24.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  25.         <java.version>1.8</java.version>
  26.         <kotlin.version>1.0.6</kotlin.version>
  27.     </properties>
  28.     <dependencies>
  29.         <dependency>
  30.             <groupId>org.springframework.boot</groupId>
  31.             <artifactId>spring-boot-starter-web</artifactId>
  32.         </dependency>
  33.         <dependency>
  34.             <groupId>org.springframework.boot</groupId>
  35.             <artifactId>spring-boot-starter-test</artifactId>
  36.             <scope>test</scope>
  37.         </dependency>
  38.         <dependency>
  39.             <groupId>junit</groupId>
  40.             <artifactId>junit</artifactId>
  41.             <version>4.12</version>
  42.         </dependency>
  43.         <dependency>
  44.             <groupId>com.alibaba</groupId>
  45.             <artifactId>fastjson</artifactId>
  46.             <version>1.1.23</version>
  47.         </dependency>
  48.         <dependency>
  49.             <groupId>org.springframework.boot</groupId>
  50.             <artifactId>spring-boot-devtools</artifactId>
  51.             <optional>true</optional>
  52.         </dependency>
  53.         <dependency>
  54.             <groupId>org.projectlombok</groupId>
  55.             <artifactId>lombok</artifactId>
  56.             <optional>true</optional>
  57.         </dependency>
  58.     </dependencies>
  59.     <build>
  60.         <resources>
  61.             <resource>
  62.                 <directory>src/main/resource</directory>
  63.                 <includes>
  64.                     <include>**/*.properties</include>
  65.                     <include>**/*.xml</include>
  66.                 </includes>
  67.                 <filtering>false</filtering>
  68.             </resource>
  69.         </resources>
  70.         <!--spring boot打包的话需要指定一个唯一的入门-->
  71.         <plugins>
  72.             <plugin>
  73.                 <groupId>org.springframework.boot</groupId>
  74.                 <artifactId>spring-boot-maven-plugin</artifactId>
  75.                 <configuration>
  76.                     <!-- 指定该Main Class为全局的唯一入口 -->
  77.                     <mainClass>com.liuyanzhao.summer.SummerApplication</mainClass>
  78.                     <layout>ZIP</layout>
  79.                 </configuration>
  80.                 <executions>
  81.                     <execution>
  82.                         <goals>
  83.                             <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
  84.                         </goals>
  85.                     </execution>
  86.                 </executions>
  87.             </plugin>
  88.         </plugins>
  89.     </build>
  90. </project>



2、summer-dao 的 pom.xml
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.     <!--关键-->
  4.     <parent>
  5.         <artifactId>summer</artifactId>
  6.         <groupId>com.liuyanzhao.summer</groupId>
  7.         <version>1.0-SNAPSHOT</version>
  8.     </parent>
  9.     <modelVersion>4.0.0</modelVersion>
  10.     <artifactId>summer-dao</artifactId>
  11.     <packaging>jar</packaging>
  12.     <version>1.0-SNAPSHOT</version>
  13.     <name>dao</name>
  14.     <url>http://maven.apache.org</url>
  15.     <properties>
  16.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17.     </properties>
  18.     <dependencies>
  19.         <dependency>
  20.             <groupId>mysql</groupId>
  21.             <artifactId>mysql-connector-java</artifactId>
  22.             <version>5.1.30</version>
  23.         </dependency>
  24.         <!--mybatis-->
  25.         <dependency>
  26.             <groupId>org.mybatis.spring.boot</groupId>
  27.             <artifactId>mybatis-spring-boot-starter</artifactId>
  28.             <version>1.3.0</version>
  29.         </dependency>
  30.         <dependency>
  31.             <groupId>org.mybatis.generator</groupId>
  32.             <artifactId>mybatis-generator-core</artifactId>
  33.             <version>1.3.5</version>
  34.         </dependency>
  35.     </dependencies>
  36.     <build>
  37.         <finalName>summer-dao</finalName>
  38.         <resources>
  39.             <resource>
  40.                 <directory>src/main/resource</directory>
  41.                 <includes>
  42.                     <include>**/*.properties</include>
  43.                     <include>**/*.xml</include>
  44.                 </includes>
  45.                 <filtering>false</filtering>
  46.             </resource>
  47.         </resources>
  48.         <plugins>
  49.             <plugin>
  50.                 <groupId>org.springframework.boot</groupId>
  51.                 <artifactId>spring-boot-maven-plugin</artifactId>
  52.                 <configuration>
  53.                     <classifier>exec</classifier>
  54.                 </configuration>
  55.             </plugin>
  56.         </plugins>
  57.     </build>
  58. </project>



3、summer-service 的 pom.xml
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.     <!--关键-->
  4.     <parent>
  5.         <artifactId>summer</artifactId>
  6.         <groupId>com.liuyanzhao.summer</groupId>
  7.         <version>1.0-SNAPSHOT</version>
  8.     </parent>
  9.     <modelVersion>4.0.0</modelVersion>
  10.     <artifactId>summer-service</artifactId>
  11.     <packaging>jar</packaging>
  12.     <version>1.0-SNAPSHOT</version>
  13.     <name>service</name>
  14.     <url>http://maven.apache.org</url>
  15.     <properties>
  16.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17.     </properties>
  18.     <dependencies>
  19.         <!--依赖summer-dao-->
  20.         <dependency>
  21.             <groupId>com.liuyanzhao.summer</groupId>
  22.             <artifactId>summer-dao</artifactId>
  23.             <version>1.0-SNAPSHOT</version>
  24.         </dependency>
  25.         <dependency>
  26.             <groupId>org.springframework.boot</groupId>
  27.             <artifactId>spring-boot-starter-test</artifactId>
  28.             <scope>test</scope>
  29.         </dependency>
  30.         <dependency>
  31.             <groupId>junit</groupId>
  32.             <artifactId>junit</artifactId>
  33.             <version>4.12</version>
  34.         </dependency>
  35.     </dependencies>
  36.     <build>
  37.         <finalName>summer-service</finalName>
  38.         <plugins>
  39.             <plugin>
  40.                 <groupId>org.springframework.boot</groupId>
  41.                 <artifactId>spring-boot-maven-plugin</artifactId>
  42.                 <configuration>
  43.                     <classifier>exec</classifier>
  44.                 </configuration>
  45.             </plugin>
  46.         </plugins>
  47.     </build>
  48. </project>



4、summer-web 的 pom.xml
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.     <!--关键-->
  4.     <parent>
  5.         <artifactId>summer</artifactId>
  6.         <groupId>com.liuyanzhao.summer</groupId>
  7.         <version>1.0-SNAPSHOT</version>
  8.     </parent>
  9.     <modelVersion>4.0.0</modelVersion>
  10.     <artifactId>summer-web</artifactId>
  11.     <version>1.0-SNAPSHOT</version>
  12.     <packaging>jar</packaging>
  13.     <name>web</name>
  14.     <url>http://maven.apache.org</url>
  15.     <dependencies>
  16.         <!--依赖summer-dao-->
  17.         <dependency>
  18.             <groupId>com.liuyanzhao.summer</groupId>
  19.             <artifactId>summer-dao</artifactId>
  20.             <version>1.0-SNAPSHOT</version>
  21.         </dependency>
  22.         <!--依赖summer-service-->
  23.         <dependency>
  24.             <groupId>com.liuyanzhao.summer</groupId>
  25.             <artifactId>summer-service</artifactId>
  26.             <version>1.0-SNAPSHOT</version>
  27.         </dependency>
  28.         <dependency>
  29.             <groupId>junit</groupId>
  30.             <artifactId>junit</artifactId>
  31.             <version>3.8.1</version>
  32.             <scope>test</scope>
  33.         </dependency>
  34.         <dependency>
  35.             <groupId>org.springframework.boot</groupId>
  36.             <artifactId>spring-boot-starter-thymeleaf</artifactId>
  37.         </dependency>
  38.         <!--启用不严格检查html-->
  39.         <dependency>
  40.             <groupId>net.sourceforge.nekohtml</groupId>
  41.             <artifactId>nekohtml</artifactId>
  42.             <version>1.9.22</version>
  43.         </dependency>
  44.     </dependencies>
  45.     <build>
  46.         <finalName>summer-web</finalName>
  47.         <plugins>
  48.             <plugin>
  49.                 <groupId>org.springframework.boot</groupId>
  50.                 <artifactId>spring-boot-maven-plugin</artifactId>
  51.             </plugin>
  52.         </plugins>
  53.     </build>
  54. </project>


二、Maven 打包几点注意


1、父项目里的 pom.xml 打包类型是 pom

2、父项目需要在 modules 里加入子项目名

3、子模块项目里的 pom.xml 的 parent  相等于继承,写 summer

4、打包的时候可以使用在 maven 中选择父项目的 package 打包,然后将三个 jar 放到一起,解压合并部署

命令是:

unzip -q summer-dao-exec.jar

unzip -q summer-service-exec.jar

unzip -q summer-web.jar

问有重复文件是否覆盖,选择 A 全部覆盖,不覆盖也行,没影响

然后一行命令启动项目

nohup java org.springframework.boot.loader.JarLauncher >output.txt &




5、在被依赖的模块(如 summer-dao)的pom.xml 中需要加下面代码,可以解决当依赖它(如 summer-service)的模块无法打包问题
  1. <configuration>
  2.     <classifier>exec</classifier>
  3. </configuration>



6、打包后没有 xml 文件解决办法

在 pom.xml 的 build 标签里添加如下代码
  1. <resources>
  2.     <resource>
  3.         <directory>src/main/resource</directory>
  4.         <includes>
  5.             <include>**/*.properties</include>
  6.             <include>**/*.xml</include>
  7.         </includes>
  8.         <filtering>false</filtering>
  9.     </resource>
  10. </resources>


三、MyBatis 和 SpringBoot 整合


文件结构如下



1、pom.xml
  1. <!--mysql 驱动-->
  2.      <dependency>
  3.          <groupId>mysql</groupId>
  4.          <artifactId>mysql-connector-java</artifactId>
  5.          <version>5.1.30</version>
  6.      </dependency>
  7.      <!--mybatis-->
  8.      <dependency>
  9.          <groupId>org.mybatis.spring.boot</groupId>
  10.          <artifactId>mybatis-spring-boot-starter</artifactId>
  11.          <version>1.3.0</version>
  12.      </dependency>
  13.      <!--mybatis-generator 插件-->
  14.      <dependency>
  15.          <groupId>org.mybatis.generator</groupId>
  16.          <artifactId>mybatis-generator-core</artifactId>
  17.          <version>1.3.5</version>
  18.      </dependency>

其中,前两个必选,后面那个 mybatis-generator 插件如果你用逆向工程生成 MyBatis 的实体和 mapper 的话,可选



2、application.properties
  1. server.port=8080
  2. debug=true
  3. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  4. spring.datasource.url=jdbc:mysql://localhost:3306/summer?useSSL=false&useUnicode=true&characterEncoding=utf-8
  5. spring.datasource.username=root
  6. spring.datasource.password=123456
  7. mybatis.type-aliases-package=com.liuyanzhao.summer.entity
  8. mybatis.mapper-locations=classpath:mapper/*.xml



3、实体 User.java
  1. package com.liuyanzhao.summer.entity;
  2. public class User {
  3.     private Integer id;
  4.     private String nickname;
  5.     private String password;
  6.     private String status;
  7.     private String username;
  8.     public Integer getId() {
  9.         return id;
  10.     }
  11.     public void setId(Integer id) {
  12.         this.id = id;
  13.     }
  14.     public String getNickname() {
  15.         return nickname;
  16.     }
  17.     public void setNickname(String nickname) {
  18.         this.nickname = nickname == null ? null : nickname.trim();
  19.     }
  20.     public String getPassword() {
  21.         return password;
  22.     }
  23.     public void setPassword(String password) {
  24.         this.password = password == null ? null : password.trim();
  25.     }
  26.     public String getStatus() {
  27.         return status;
  28.     }
  29.     public void setStatus(String status) {
  30.         this.status = status == null ? null : status.trim();
  31.     }
  32.     public String getUsername() {
  33.         return username;
  34.     }
  35.     public void setUsername(String username) {
  36.         this.username = username == null ? null : username.trim();
  37.     }
  38. }



4、UserMapper.java
  1. package com.liuyanzhao.summer.mapper;
  2. import com.liuyanzhao.summer.entity.User;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Param;
  5. import org.springframework.stereotype.Component;
  6. import java.util.List;
  7. @Mapper
  8. @Component("userMapper")
  9. public interface UserMapper {
  10.     int deleteByPrimaryKey(Integer id);
  11.     int insert(User record);
  12.     int insertSelective(User record);
  13.     User selectByPrimaryKey(Integer id);
  14.     int updateByPrimaryKeySelective(User record);
  15.     int updateByPrimaryKey(User record);
  16.     List<User> selectAllUser();
  17.     User selectByUsername(@Param("username") String username);
  18. }



5、UserMapper.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.liuyanzhao.summer.mapper.UserMapper">
  4.   <resultMap id="BaseResultMap" type="com.liuyanzhao.summer.entity.User">
  5.     <id column="id" jdbcType="INTEGER" property="id" />
  6.     <result column="nickname" jdbcType="VARCHAR" property="nickname" />
  7.     <result column="password" jdbcType="VARCHAR" property="password" />
  8.     <result column="status" jdbcType="VARCHAR" property="status" />
  9.     <result column="username" jdbcType="VARCHAR" property="username" />
  10.   </resultMap>
  11.   <sql id="Base_Column_List">
  12.     id, nickname, password, status, username
  13.   </sql>
  14.   <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  15.     select
  16.     <include refid="Base_Column_List" />
  17.     from user
  18.     where id = #{id,jdbcType=INTEGER}
  19.   </select>
  20.   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  21.     delete from user
  22.     where id = #{id,jdbcType=INTEGER}
  23.   </delete>
  24.   <insert id="insert" parameterType="com.liuyanzhao.summer.entity.User">
  25.     insert into user (id, nickname, password,
  26.       status, username)
  27.     values (#{id,jdbcType=INTEGER}, #{nickname,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
  28.       #{status,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR})
  29.   </insert>
  30.   <insert id="insertSelective" parameterType="com.liuyanzhao.summer.entity.User">
  31.     insert into user
  32.     <trim prefix="(" suffix=")" suffixOverrides=",">
  33.       <if test="id != null">
  34.         id,
  35.       </if>
  36.       <if test="nickname != null">
  37.         nickname,
  38.       </if>
  39.       <if test="password != null">
  40.         password,
  41.       </if>
  42.       <if test="status != null">
  43.         status,
  44.       </if>
  45.       <if test="username != null">
  46.         username,
  47.       </if>
  48.     </trim>
  49.     <trim prefix="values (" suffix=")" suffixOverrides=",">
  50.       <if test="id != null">
  51.         #{id,jdbcType=INTEGER},
  52.       </if>
  53.       <if test="nickname != null">
  54.         #{nickname,jdbcType=VARCHAR},
  55.       </if>
  56.       <if test="password != null">
  57.         #{password,jdbcType=VARCHAR},
  58.       </if>
  59.       <if test="status != null">
  60.         #{status,jdbcType=VARCHAR},
  61.       </if>
  62.       <if test="username != null">
  63.         #{username,jdbcType=VARCHAR},
  64.       </if>
  65.     </trim>
  66.   </insert>
  67.   <update id="updateByPrimaryKeySelective" parameterType="com.liuyanzhao.summer.entity.User">
  68.     update user
  69.     <set>
  70.       <if test="nickname != null">
  71.         nickname = #{nickname,jdbcType=VARCHAR},
  72.       </if>
  73.       <if test="password != null">
  74.         password = #{password,jdbcType=VARCHAR},
  75.       </if>
  76.       <if test="status != null">
  77.         status = #{status,jdbcType=VARCHAR},
  78.       </if>
  79.       <if test="username != null">
  80.         username = #{username,jdbcType=VARCHAR},
  81.       </if>
  82.     </set>
  83.     where id = #{id,jdbcType=INTEGER}
  84.   </update>
  85.   <update id="updateByPrimaryKey" parameterType="com.liuyanzhao.summer.entity.User">
  86.     update user
  87.     set nickname = #{nickname,jdbcType=VARCHAR},
  88.       password = #{password,jdbcType=VARCHAR},
  89.       status = #{status,jdbcType=VARCHAR},
  90.       username = #{username,jdbcType=VARCHAR}
  91.     where id = #{id,jdbcType=INTEGER}
  92.   </update>
  93.   <!--查询所有的用户-->
  94.   <select id="selectAllUser"  resultMap="BaseResultMap">
  95.     select
  96.     <include refid="Base_Column_List" />
  97.     from user
  98.   </select>
  99.   <!--根据用户名查询-->
  100.   <select id="selectByUsername"  parameterType="string" resultMap="BaseResultMap">
  101.     select
  102.     <include refid="Base_Column_List" />
  103.     from user
  104.     WHERE username = #{username}
  105.   </select>
  106. </mapper>




四、RestFul 风格URL 的测试


本文主要介绍的是 Maven 多模块开发和 SpringBoot + MyBatis 整合,service 层写的很简单不要过分在意,下面来使用 PostMan 测试一下请求

1、获得一个用户





2、获得多个用户





3、添加用户





4、更新用户





5、删除用户




五、源码地址


源码:https://github.com/saysky/maven-multi-modules















  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0