SpringBoot之SSM多模块应用

2021-02-13 22:17

阅读:428

标签: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.0modelVersion>
    packaging>pompackaging>
    modules>
        module>isearch-webmodule>
        module>isearch-pojomodule>
        module>isearch-servicemodule>
        module>isearch-daomodule>
    modules>
    parent>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starter-parentartifactId>
        version>2.2.6.RELEASEversion>
        relativePath/> 
    parent>
    groupId>com.blbgroupId>
    artifactId>isearchartifactId>
    version>0.0.1-SNAPSHOTversion>
    name>isearchname>
    description>Demo project for Spring Bootdescription>

    properties>
        java.version>1.8java.version>
    properties>
dependencyManagement>
    dependencies>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-data-elasticsearchartifactId>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>
        dependency>
            groupId>org.mybatis.spring.bootgroupId>
            artifactId>mybatis-spring-boot-starterartifactId>
            version>2.1.2version>
        dependency>
        dependency>
            groupId>mysqlgroupId>
            artifactId>mysql-connector-javaartifactId>
            version>5.1.47version>
            scope>runtimescope>
        dependency>

        dependency>
            groupId>org.projectlombokgroupId>
            artifactId>lombokartifactId>
            optional>trueoptional>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-testartifactId>
            scope>testscope>
            exclusions>
                exclusion>
                    groupId>org.junit.vintagegroupId>
                    artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
        dependency>
            groupId>com.alibabagroupId>
            artifactId>druidartifactId>
            version>1.1.10version>
        dependency>
    dependencies>
dependencyManagement>
    build>
        plugins>
            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
            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">
    
    
    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">
    
    `uid`, uname, upwd
  sql>
  select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    
    select 
    include refid="Base_Column_List" />
    from t_user
    where `uid` = #{uid,jdbcType=INTEGER}
  select>
  delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    
    delete from t_user
    where `uid` = #{uid,jdbcType=INTEGER}
  delete>
  insert id="insert" parameterType="com.blb.pojo.TUser">
    
    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">
    
    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">
    
    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">
    
    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>isearchartifactId>
        groupId>com.blbgroupId>
        version>0.0.1-SNAPSHOTversion>
    parent>
    modelVersion>4.0.0modelVersion>

    artifactId>isearch-daoartifactId>

    name>isearch-daoname>
    
    url>http://www.example.comurl>

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

    dependencies>
        dependency>
            groupId>com.blbgroupId>
            artifactId>isearch-pojoartifactId>
            version>0.0.1-SNAPSHOTversion>
        dependency>
        dependency>
            groupId>org.mybatis.spring.bootgroupId>
            artifactId>mybatis-spring-boot-starterartifactId>
            version>2.1.2version>
        dependency>
        dependency>
            groupId>mysqlgroupId>
            artifactId>mysql-connector-javaartifactId>
        dependency>
        dependency>
            groupId>com.alibabagroupId>
            artifactId>druidartifactId>
        dependency>
    dependencies>

    build>
        resources>
            resource>
                directory>src/main/javadirectory>
                includes>
                    include>**/*.xmlinclude>
                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>isearchartifactId>
        groupId>com.blbgroupId>
        version>0.0.1-SNAPSHOTversion>
    parent>
    modelVersion>4.0.0modelVersion>

    artifactId>isearch-serviceartifactId>

    name>isearch-servicename>
    
    url>http://www.example.comurl>

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

    dependencies>
        dependency>
            groupId>com.blbgroupId>
            artifactId>isearch-daoartifactId>
            version>0.0.1-SNAPSHOTversion>
        dependency>
        dependency>
            groupId>com.blbgroupId>
            artifactId>isearch-pojoartifactId>
            version>0.0.1-SNAPSHOTversion>
            scope>compilescope>
        dependency>
    dependencies>

    build>
        pluginManagement>
            plugins>
                
                plugin>
                    artifactId>maven-clean-pluginartifactId>
                    version>3.1.0version>
                plugin>
                
                plugin>
                    artifactId>maven-resources-pluginartifactId>
                    version>3.0.2version>
                plugin>
                plugin>
                    artifactId>maven-compiler-pluginartifactId>
                    version>3.8.0version>
                plugin>
                plugin>
                    artifactId>maven-surefire-pluginartifactId>
                    version>2.22.1version>
                plugin>
                plugin>
                    artifactId>maven-jar-pluginartifactId>
                    version>3.0.2version>
                plugin>
                plugin>
                    artifactId>maven-install-pluginartifactId>
                    version>2.5.2version>
                plugin>
                plugin>
                    artifactId>maven-deploy-pluginartifactId>
                    version>2.8.2version>
                plugin>
                
                plugin>
                    artifactId>maven-site-pluginartifactId>
                    version>3.7.1version>
                plugin>
                plugin>
                    artifactId>maven-project-info-reports-pluginartifactId>
                    version>3.0.0version>
                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>isearchartifactId>
        groupId>com.blbgroupId>
        version>0.0.1-SNAPSHOTversion>
    parent>
    modelVersion>4.0.0modelVersion>

    artifactId>isearch-webartifactId>

    name>isearch-webname>
    
    url>http://www.example.comurl>

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

    dependencies>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>
        dependency>
            groupId>com.blbgroupId>
            artifactId>isearch-pojoartifactId>
            version>0.0.1-SNAPSHOTversion>
        dependency>
        dependency>
            groupId>com.blbgroupId>
            artifactId>isearch-serviceartifactId>
            version>0.0.1-SNAPSHOTversion>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
            version>2.2.6.RELEASEversion>
            scope>compilescope>
        dependency>
    dependencies>

    build>
        plugins>
            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

 

@Component

SpringBoot之SSM多模块应用

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

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


评论


亲,登录后才可以留言!