SpringBoot之SSM多模块应用

Posted 几个Ak

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot之SSM多模块应用相关的知识,希望对你有一定的参考价值。

1.构建项目

1.创建新项目时选择Spring

 

 2.填写包名和项目名

 

 3.选择如下框架

 

 4.pom文件中使用dependencyManagement把依赖包起来

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>isearch-web</module>
        <module>isearch-pojo</module>
        <module>isearch-service</module>
        <module>isearch-dao</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.blb</groupId>
    <artifactId>isearch</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>isearch</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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</artifactId>
            <version>1.1.10</version>
        </dependency>
    </dependencies>
</dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

5.创建子模块

pojo --> dao --> service -->controller

 1.isearch-pojo模块

创建用户类

package com.blb.pojo;

public class TUser {
    private Integer uid;

    private String uname;

    private String upwd;

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpwd() {
        return upwd;
    }

    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
}

2.isearch-dao模块

 

 

 其中mapper类和xml用代码生成器生成

TUserMapper:

package com.blb.dao;

import com.blb.pojo.TUser;
import org.apache.ibatis.annotations.Mapper;

@Mapper
@Component
public interface TUserMapper { int deleteByPrimaryKey(Integer uid); int insert(TUser record); int insertSelective(TUser record); TUser selectByPrimaryKey(Integer uid); int updateByPrimaryKeySelective(TUser record); int updateByPrimaryKey(TUser record); }

TUserMapper.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.blb.dao.TUserMapper">
  <resultMap id="BaseResultMap" type="com.blb.pojo.TUser">
    <!--@mbg.generated-->
    <!--@Table t_user-->
    <id column="uid" jdbcType="INTEGER" property="uid" />
    <result column="uname" jdbcType="VARCHAR" property="uname" />
    <result column="upwd" jdbcType="VARCHAR" property="upwd" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    `uid`, uname, upwd
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--@mbg.generated-->
    select 
    <include refid="Base_Column_List" />
    from t_user
    where `uid` = #{uid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--@mbg.generated-->
    delete from t_user
    where `uid` = #{uid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.blb.pojo.TUser">
    <!--@mbg.generated-->
    insert into t_user (`uid`, uname, upwd
      )
    values (#{uid,jdbcType=INTEGER}, #{uname,jdbcType=VARCHAR}, #{upwd,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.blb.pojo.TUser">
    <!--@mbg.generated-->
    insert into t_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="uid != null">
        `uid`,
      </if>
      <if test="uname != null">
        uname,
      </if>
      <if test="upwd != null">
        upwd,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="uid != null">
        #{uid,jdbcType=INTEGER},
      </if>
      <if test="uname != null">
        #{uname,jdbcType=VARCHAR},
      </if>
      <if test="upwd != null">
        #{upwd,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.blb.pojo.TUser">
    <!--@mbg.generated-->
    update t_user
    <set>
      <if test="uname != null">
        uname = #{uname,jdbcType=VARCHAR},
      </if>
      <if test="upwd != null">
        upwd = #{upwd,jdbcType=VARCHAR},
      </if>
    </set>
    where `uid` = #{uid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.blb.pojo.TUser">
    <!--@mbg.generated-->
    update t_user
    set uname = #{uname,jdbcType=VARCHAR},
      upwd = #{upwd,jdbcType=VARCHAR}
    where `uid` = #{uid,jdbcType=INTEGER}
  </update>
</mapper>

pom文件:

需要用到pojo层的属性

<?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>isearch</artifactId>
        <groupId>com.blb</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>isearch-dao</artifactId>

    <name>isearch-dao</name>
    <!-- FIXME change it to the project\'s website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.blb</groupId>
            <artifactId>isearch-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

3.isearch-service模块

 

 

 TUserService接口类:

package com.blb.service;

import com.blb.pojo.TUser;

public interface TUserService {
    public void save(TUser user);
    public void remove(Integer uid);
    public void update(TUser user);
    public TUser get(Integer uid);
}

TUserService实例化类:

package com.blb.service.impl;

import com.blb.dao.TUserMapper;
import com.blb.pojo.TUser;
import com.blb.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TUserServiceImpl implements TUserService {
    @Autowired
    private TUserMapper tUserMapper;
    @Override
    public void save(TUser user) {
        tUserMapper.insert(user);
    }

    @Override
    public void remove(Integer uid) {
        tUserMapper.deleteByPrimaryKey(uid);
    }

    @Override
    public void update(TUser user) {
        tUserMapper.updateByPrimaryKey(user);
    }

    @Transactional(readOnly = true)
    public TUser get(Integer uid) {
        TUser tUser = tUserMapper.selectByPrimaryKey(uid);
        return tUser;
    }
}

pom文件:

要用到pojo和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>isearch</artifactId>
        <groupId>com.blb</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>isearch-service</artifactId>

    <name>isearch-service</name>
    <!-- FIXME change it to the project\'s website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.blb</groupId>
            <artifactId>isearch-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.blb</groupId>
            <artifactId>isearch-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <<

以上是关于SpringBoot之SSM多模块应用的主要内容,如果未能解决你的问题,请参考以下文章

精通系列)创建 Springboot 项目+基于 ssm 多模块项目案例+微服务实战

精通系列)创建 Springboot 项目+基于 ssm 多模块项目案例+微服务实战

Maven多模块项目,框架SSM怎么改成springboot的?

SpringBoot整合SSM之删除

小白入门之 Springboot整合SSM

第二章:springboot 整合JSP(打JAR针对多模块项目)