以前我们都是将各个层放在一个项目里,后期维护可能不太方便,代码结构也不清晰。
本文先如何使用 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 内容如下
2、summer-dao 的 pom.xml
3、summer-service 的 pom.xml
4、summer-web 的 pom.xml
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 全部覆盖,不覆盖也行,没影响
然后一行命令启动项目
5、在被依赖的模块(如 summer-dao)的pom.xml 中需要加下面代码,可以解决当依赖它(如 summer-service)的模块无法打包问题
6、打包后没有 xml 文件解决办法
在 pom.xml 的 build 标签里添加如下代码
文件结构如下
1、pom.xml
其中,前两个必选,后面那个 mybatis-generator 插件如果你用逆向工程生成 MyBatis 的实体和 mapper 的话,可选
2、application.properties
3、实体 User.java
4、UserMapper.java
5、UserMapper.xml
本文主要介绍的是 Maven 多模块开发和 SpringBoot + MyBatis 整合,service 层写的很简单不要过分在意,下面来使用 PostMan 测试一下请求
1、获得一个用户
2、获得多个用户
3、添加用户
4、更新用户
5、删除用户
源码:https://github.com/saysky/maven-multi-modules
本文先如何使用 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 内容如下
- <?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.liuyanzhao.summer</groupId>
- <artifactId>summer</artifactId>
- <packaging>pom</packaging>
- <version>1.0-SNAPSHOT</version>
- <!--关键-->
- <modules>
- <module>summer-service</module>
- <module>summer-web</module>
- <module>summer-dao</module>
- </modules>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.9.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- <kotlin.version>1.0.6</kotlin.version>
- </properties>
- <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>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.1.23</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- </dependencies>
- <build>
- <resources>
- <resource>
- <directory>src/main/resource</directory>
- <includes>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- </resources>
- <!--spring boot打包的话需要指定一个唯一的入门-->
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <!-- 指定该Main Class为全局的唯一入口 -->
- <mainClass>com.liuyanzhao.summer.SummerApplication</mainClass>
- <layout>ZIP</layout>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
2、summer-dao 的 pom.xml
- <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>summer</artifactId>
- <groupId>com.liuyanzhao.summer</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>summer-dao</artifactId>
- <packaging>jar</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>dao</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.30</version>
- </dependency>
- <!--mybatis-->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.0</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-core</artifactId>
- <version>1.3.5</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>summer-dao</finalName>
- <resources>
- <resource>
- <directory>src/main/resource</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>
- </project>
3、summer-service 的 pom.xml
- <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>summer</artifactId>
- <groupId>com.liuyanzhao.summer</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>summer-service</artifactId>
- <packaging>jar</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>service</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <!--依赖summer-dao-->
- <dependency>
- <groupId>com.liuyanzhao.summer</groupId>
- <artifactId>summer-dao</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>summer-service</finalName>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <classifier>exec</classifier>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
4、summer-web 的 pom.xml
- <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/maven-v4_0_0.xsd">
- <!--关键-->
- <parent>
- <artifactId>summer</artifactId>
- <groupId>com.liuyanzhao.summer</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>summer-web</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>web</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <!--依赖summer-dao-->
- <dependency>
- <groupId>com.liuyanzhao.summer</groupId>
- <artifactId>summer-dao</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <!--依赖summer-service-->
- <dependency>
- <groupId>com.liuyanzhao.summer</groupId>
- <artifactId>summer-service</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
- <!--启用不严格检查html-->
- <dependency>
- <groupId>net.sourceforge.nekohtml</groupId>
- <artifactId>nekohtml</artifactId>
- <version>1.9.22</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>summer-web</finalName>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </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)的模块无法打包问题
- <configuration>
- <classifier>exec</classifier>
- </configuration>
6、打包后没有 xml 文件解决办法
在 pom.xml 的 build 标签里添加如下代码
- <resources>
- <resource>
- <directory>src/main/resource</directory>
- <includes>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- </resources>
三、MyBatis 和 SpringBoot 整合
文件结构如下
1、pom.xml
- <!--mysql 驱动-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.30</version>
- </dependency>
- <!--mybatis-->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.0</version>
- </dependency>
- <!--mybatis-generator 插件-->
- <dependency>
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-core</artifactId>
- <version>1.3.5</version>
- </dependency>
其中,前两个必选,后面那个 mybatis-generator 插件如果你用逆向工程生成 MyBatis 的实体和 mapper 的话,可选
2、application.properties
- server.port=8080
- debug=true
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- spring.datasource.url=jdbc:mysql://localhost:3306/summer?useSSL=false&useUnicode=true&characterEncoding=utf-8
- spring.datasource.username=root
- spring.datasource.password=123456
- mybatis.type-aliases-package=com.liuyanzhao.summer.entity
- mybatis.mapper-locations=classpath:mapper/*.xml
3、实体 User.java
- package com.liuyanzhao.summer.entity;
- public class User {
- private Integer id;
- private String nickname;
- private String password;
- private String status;
- private String username;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getNickname() {
- return nickname;
- }
- public void setNickname(String nickname) {
- this.nickname = nickname == null ? null : nickname.trim();
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password == null ? null : password.trim();
- }
- public String getStatus() {
- return status;
- }
- public void setStatus(String status) {
- this.status = status == null ? null : status.trim();
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username == null ? null : username.trim();
- }
- }
4、UserMapper.java
- package com.liuyanzhao.summer.mapper;
- import com.liuyanzhao.summer.entity.User;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Param;
- import org.springframework.stereotype.Component;
- import java.util.List;
- @Mapper
- @Component("userMapper")
- public interface UserMapper {
- int deleteByPrimaryKey(Integer id);
- int insert(User record);
- int insertSelective(User record);
- User selectByPrimaryKey(Integer id);
- int updateByPrimaryKeySelective(User record);
- int updateByPrimaryKey(User record);
- List<User> selectAllUser();
- User selectByUsername(@Param("username") String username);
- }
5、UserMapper.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.liuyanzhao.summer.mapper.UserMapper">
- <resultMap id="BaseResultMap" type="com.liuyanzhao.summer.entity.User">
- <id column="id" jdbcType="INTEGER" property="id" />
- <result column="nickname" jdbcType="VARCHAR" property="nickname" />
- <result column="password" jdbcType="VARCHAR" property="password" />
- <result column="status" jdbcType="VARCHAR" property="status" />
- <result column="username" jdbcType="VARCHAR" property="username" />
- </resultMap>
- <sql id="Base_Column_List">
- id, nickname, password, status, username
- </sql>
- <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
- select
- <include refid="Base_Column_List" />
- from user
- where id = #{id,jdbcType=INTEGER}
- </select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
- delete from user
- where id = #{id,jdbcType=INTEGER}
- </delete>
- <insert id="insert" parameterType="com.liuyanzhao.summer.entity.User">
- insert into user (id, nickname, password,
- status, username)
- values (#{id,jdbcType=INTEGER}, #{nickname,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
- #{status,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR})
- </insert>
- <insert id="insertSelective" parameterType="com.liuyanzhao.summer.entity.User">
- insert into user
- <trim prefix="(" suffix=")" suffixOverrides=",">
- <if test="id != null">
- id,
- </if>
- <if test="nickname != null">
- nickname,
- </if>
- <if test="password != null">
- password,
- </if>
- <if test="status != null">
- status,
- </if>
- <if test="username != null">
- username,
- </if>
- </trim>
- <trim prefix="values (" suffix=")" suffixOverrides=",">
- <if test="id != null">
- #{id,jdbcType=INTEGER},
- </if>
- <if test="nickname != null">
- #{nickname,jdbcType=VARCHAR},
- </if>
- <if test="password != null">
- #{password,jdbcType=VARCHAR},
- </if>
- <if test="status != null">
- #{status,jdbcType=VARCHAR},
- </if>
- <if test="username != null">
- #{username,jdbcType=VARCHAR},
- </if>
- </trim>
- </insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.liuyanzhao.summer.entity.User">
- update user
- <set>
- <if test="nickname != null">
- nickname = #{nickname,jdbcType=VARCHAR},
- </if>
- <if test="password != null">
- password = #{password,jdbcType=VARCHAR},
- </if>
- <if test="status != null">
- status = #{status,jdbcType=VARCHAR},
- </if>
- <if test="username != null">
- username = #{username,jdbcType=VARCHAR},
- </if>
- </set>
- where id = #{id,jdbcType=INTEGER}
- </update>
- <update id="updateByPrimaryKey" parameterType="com.liuyanzhao.summer.entity.User">
- update user
- set nickname = #{nickname,jdbcType=VARCHAR},
- password = #{password,jdbcType=VARCHAR},
- status = #{status,jdbcType=VARCHAR},
- username = #{username,jdbcType=VARCHAR}
- where id = #{id,jdbcType=INTEGER}
- </update>
- <!--查询所有的用户-->
- <select id="selectAllUser" resultMap="BaseResultMap">
- select
- <include refid="Base_Column_List" />
- from user
- </select>
- <!--根据用户名查询-->
- <select id="selectByUsername" parameterType="string" resultMap="BaseResultMap">
- select
- <include refid="Base_Column_List" />
- from user
- WHERE username = #{username}
- </select>
- </mapper>
四、RestFul 风格URL 的测试
本文主要介绍的是 Maven 多模块开发和 SpringBoot + MyBatis 整合,service 层写的很简单不要过分在意,下面来使用 PostMan 测试一下请求
1、获得一个用户
2、获得多个用户
3、添加用户
4、更新用户
5、删除用户
五、源码地址
源码:https://github.com/saysky/maven-multi-modules
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏