码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot之SSM多模块应用

时间:2020-04-18 13:58:29      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:add   source   save   fir   lifecycle   生成器   需要   stc   factory   

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>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

4.isearch-web模块

技术图片

 

 IsearchApplication类要在其他子包的根包下:

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class IsearchApplication {
    public static void main(String[] args) {
        SpringApplication.run(IsearchApplication.class);
    }
}

HelloController类:

package com.blb.controller;

import com.blb.pojo.TUser;
import com.blb.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloController {
    @Autowired
    private TUserService tUserService;

    @RequestMapping(value = "/user/add",method = RequestMethod.POST)
    public Map add(@RequestBody TUser user){
        tUserService.save(user);
        Map map=new HashMap();
        map.put("msg","保存成功");
        map.put("status",true);
        return map;
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public Map get(@PathVariable("id")Integer id){
        TUser user = tUserService.get(id);
        Map map=new HashMap();
        map.put("msg","查询成功");
        map.put("status",true);
        map.put("data",user);
        return map;
    }
}

application.yml文件:

server:
  port: 9090
spring:
  mybatis:
    mapperLocations:classpath:*.xml
    typeAliasesPackage:com.blb.pojo
  datasource:
    name: mysql
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:6666/ssm?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
    username: root
    password: 123456
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 30000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: select 1
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: false
      max-pool-prepared-statement-per-connection-size: 20
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=6000

pom文件:

要用到pojo和service层

<?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-web</artifactId>

    <name>isearch-web</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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.blb</groupId>
            <artifactId>isearch-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.blb</groupId>
            <artifactId>isearch-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.6.RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

@Component

SpringBoot之SSM多模块应用

标签:add   source   save   fir   lifecycle   生成器   需要   stc   factory   

原文地址:https://www.cnblogs.com/asksk/p/12722227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!