美文网首页程序员每天写500字IT@程序员猿媛
如何搭建maven中,分布式工程(1)--整合SSM

如何搭建maven中,分布式工程(1)--整合SSM

作者: 您好简书 | 来源:发表于2019-07-16 01:03 被阅读1次

1.在camel-manger-web中配置mybatis resource spring


image.png

mybatis包下
SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 配置分页插件    拦截器-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>


resource包下 db.properties resource.properties

db.properties 配置数据库

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/taobao?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

resource.properties

#FTP\u76F8\u5173\u914D\u7F6E
#FTP\u7684ip\u5730\u5740
FTP_ADDRESS=192.168.230.128
FTP_PORT=21
FTP_USERNAME=ftpuser
FTP_PASSWORD=sunyong0305
FTP_BASE_PATH=/home/ftpuser/www/images

#\u56FE\u7247\u670D\u52A1\u5668\u7684\u76F8\u5173\u914D\u5236
#\u56FE\u7247\u670D\u52A1\u5668\u7684\u57FA\u7840\u8DEF\u5F84
#FILI_UPLOAD_PATH=D:/temp/imagestest/webapps/images
IMAGE_BASE_URL=http://192.168.230.128

spring


image.png

applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 数据库连接池 -->
    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:resource/*.properties" />
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
    </bean>
    <!-- 配置sqlsessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置扫描包,加载mapper代理对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="camel.manager.mapper"></property>
    </bean>
</beans>


applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 扫描包加载Service实现类 -->
    <context:component-scan base-package="camel.manager.service"></context:component-scan>
</beans>


applicationContext-trans.xml 事务

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 传播行为 -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* camel.manager.service.*.*(..))" />
    </aop:config>
</beans>


springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.camel.controller" />
    <mvc:annotation-driven />
    <!-- 定义文件上传解析器 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设定默认编码 -->
        <property name="defaultEncoding" value="UTF-8"></property>
        <!-- 设定文件上传的最大值5MB,5*1024*1024 -->
        <property name="maxUploadSize" value="5242880"></property>
    </bean>
<!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean> 
<!-- 配置静态资源的映射 -->
    <mvc:resources location="/WEB-INF/css/" mapping="/css/**" />
    <mvc:resources location="/WEB-INF/js/" mapping="/js/**" />
</beans>


因为 image.png

所以在src/main/java下 新建一个名字叫com.camel.controller的包

数据库

新建数据库名字叫e3mall

image.png

导入sql

image.png

6.2. Mybatis逆向工程

使用mybatis官方提供的mybatis-generator生成pojo、mapper接口及映射文件。
并且将pojo放到e3-manager-pojo工程中。
将mapper接口及映射文件放到e3-manager-dao工程中。
6.7. 整合测试
6.7.1. 需求
根据商品id查询商品信息,返回json数据。

6.7.2. Dao层
由于是单表查询可以使用逆向工程生成的代码。

6.7.3. Service层
参数:商品id
返回值:TbItem
业务逻辑:根据商品id查询商品信息。
camel.manager.service 包下ItemService.java 创建接口

package camel.manager.service;

import camel.dspt.manager.pojo.TbItem;

public interface ItemService {
    //根据商品查id

    TbItem getItemById(long id);
}

ItemServiceImpl.java 实现类

package imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import camel.dspt.manager.pojo.TbItem;
import camel.dspt.manager.pojo.TbItemExample;
import camel.dspt.manager.pojo.TbItemExample.Criteria;
import camel.manager.mapper.TbItemMapper;
import camel.manager.service.ItemService;
@Service
public class ItemServiceImpl implements ItemService{
@Autowired
    private TbItemMapper tbItemMapper;
    @Override
    public TbItem getItemById(long id) {
//      TbItem tbItem=  tbItemMapper.selectByPrimaryKey(id);
        //按条件查询
        TbItemExample example=new TbItemExample();
        Criteria  criteria=example.createCriteria();
        //设置查询条件
        criteria.andIdEqualTo(id);
        List<TbItem> list=tbItemMapper.selectByExample(example);
        if(list!=null &&list.size()>0){
            return list.get(0);
        }
        return null;
    }

}

在camel-manger-web工程下 com.camel.controller 创建ItemController.java

package com.camel.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import camel.dspt.manager.pojo.TbItem;
import camel.manager.service.ItemService;

@Controller
public class ItemController {
@Autowired
    private ItemService itemService;
@RequestMapping("/item/{itemId}")
@ResponseBody
private TbItem getItemById(@PathVariable Long itemId) {
    TbItem tbItem = itemService.getItemById(itemId);
    return tbItem;

}
}

测试

image.png

点击向下的箭头 选择RUN Configurations


image.png

全选删除

image.png
image.png

Maven Waring : Version is duplicate of parent version

问题描述:

新项目在创建的时候,因为用到了分模块的,所以导致子模块的pom文件,报了 如下警告:

image image

解决办法:

直接 Window --> Preferences --> Maven --> Errors/Warnings --> Ignore 将两个警告,调成忽略即可解决。 如下图所示:

image

在camel-dspt-manager的聚合工程 选择pom.xml


image.png image.png
运行后 出现java.net.BindException: Address already in use: JVM_Bind :8080解决方法

端口冲突

根据异常图,判断问题为端口冲突


image.png

1.按Windows键+R,输入cmd,进入命令行模式,如下图所示


image.png

2.cmd命令模式下输入cd c:\windows\system32进入到系统文件夹下,输入netstat -ano,然后找到占用8080端口的那个进程 如下图所示

image.png image.png

3.PID号为1488的进程占用着8080端口,打开任务管理器查看详细信息,查找PID号为1488进程,点击结束任务即可,如下图所示


image.png

4.重新启动想启动的应用

效果截图:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] camel-dspt-manager                                                 [pom]
[INFO] camel-dspt-manager-pojo                                            [jar]
[INFO] camel-manager-mapper                                               [jar]
[INFO] camel-manager-service                                              [jar]
[INFO] camel-manger-web                                                   [war]
[INFO] 
[INFO] ------------------< www.camel.com:camel-dspt-manager >------------------
[INFO] Building camel-dspt-manager 0.0.1-SNAPSHOT                         [1/5]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ camel-dspt-manager >>>
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ camel-dspt-manager <<<
[INFO] 
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ camel-dspt-manager ---
[INFO] Skipping non-war project
[INFO] 
[INFO] ---------------< www.camel.com:camel-dspt-manager-pojo >----------------
[INFO] Building camel-dspt-manager-pojo 0.0.1-SNAPSHOT                    [2/5]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ camel-dspt-manager-pojo >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.7:resources (default-resources) @ camel-dspt-manager-pojo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ camel-dspt-manager-pojo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ camel-dspt-manager-pojo <<<
[INFO] 
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ camel-dspt-manager-pojo ---
[INFO] Skipping non-war project
[INFO] 
[INFO] -----------------< www.camel.com:camel-manager-mapper >-----------------
[INFO] Building camel-manager-mapper 0.0.1-SNAPSHOT                       [3/5]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ camel-manager-mapper >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.7:resources (default-resources) @ camel-manager-mapper ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 11 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ camel-manager-mapper ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ camel-manager-mapper <<<
[INFO] 
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ camel-manager-mapper ---
[INFO] Skipping non-war project
[INFO] 
[INFO] ----------------< www.camel.com:camel-manager-service >-----------------
[INFO] Building camel-manager-service 0.0.1-SNAPSHOT                      [4/5]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ camel-manager-service >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.7:resources (default-resources) @ camel-manager-service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ camel-manager-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ camel-manager-service <<<
[INFO] 
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ camel-manager-service ---
[INFO] Skipping non-war project
[INFO] 
[INFO] -------------------< www.camel.com:camel-manger-web >-------------------
[INFO] Building camel-manger-web 0.0.1-SNAPSHOT                           [5/5]
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ camel-manger-web >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.7:resources (default-resources) @ camel-manger-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 7 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ camel-manger-web ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ camel-manger-web <<<
[INFO] 
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ camel-manger-web ---
[INFO] Running war on http://localhost:8088/
[INFO] Using existing Tomcat server configuration at E:\java\workspace\camel-dspt-manager\camel-manger-web\target\tomcat
[INFO] create webapp with contextPath: 
七月 16, 2019 12:46:31 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8088"]
七月 16, 2019 12:46:31 上午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Tomcat
七月 16, 2019 12:46:31 上午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.47
七月 16, 2019 12:46:35 上午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
七月 16, 2019 12:46:35 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
七月 16, 2019 12:46:37 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'camel-dspt-manager'
七月 16, 2019 12:46:38 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8088"]

在网页上输入http://localhost:8088/item/562379
结果

image.png

测试成功!!!

相关文章

  • SSM框架-整合

    SSM整合 搭建整合环境 1. 搭建整合环境 创建相应的数据库 创建Maven工程并导入相应坐标创建ssm_par...

  • 如何搭建maven中,分布式工程(1)--整合SSM

    1.在camel-manger-web中配置mybatis resource spring mybatis包下S...

  • SSM项目搭建

    总结加套路,建立框架思路 先整合SS,在加入Mybatis 1.建立maven工程,搭建SSM环境 1.1 因为初...

  • <锋迷商城>项目搭建(01)

    基于Maven的聚合工程完成项目搭建,前端采用vue+axios,后端采用springboot整合ssm75162...

  • SSM Maven框架基础整合(二)

    前言 上篇文章讲解了如何创建Maven工程和SSM框架所需的基本配置SSM Maven框架基础整合(一) 一、SS...

  • Maven工程的搭建

    今天我们来说一下Maven项目工程的搭建,接下来我会介绍一下SSM框架的整合。 第一步、创建Maven工程 右键-...

  • Maven + Spring + SpringMVC + MyB

    参考:Java框架搭建-Maven、Mybatis、Spring MVC整合搭建SSM框架——Spring+Spr...

  • SSM框架整合

    SSM框架整合 基于Maven+SpringMVC+Spring+MyBatis的组合,快速搭建SSM框架。适合初...

  • SSM框架整合

    SSM框架整合 1.1 原始方式整合 1.准备工作 2.创建Maven工程 3.导入Maven坐标 参考:素材/配...

  • SSM整合详解

    SSM项目整合 一、maven工程 pom.xml 二、domain 1、Account.java 三、contr...

网友评论

    本文标题:如何搭建maven中,分布式工程(1)--整合SSM

    本文链接:https://www.haomeiwen.com/subject/clgmkctx.html