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

作者 : 开心源码 本文共15058个字,预计阅读时间需要38分钟 发布时间: 2022-05-12 共184人阅读

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.Driverjdbc.url=jdbc:mysql://localhost:3306/taobao?characterEncoding=utf-8jdbc.username=rootjdbc.password=root

resource.properties

#FTP\u76F8\u5173\u914D\u7F6E#FTP\u7684ip\u5730\u5740FTP_ADDRESS=192.168.230.128FTP_PORT=21FTP_USERNAME=ftpuserFTP_PASSWORD=sunyong0305FTP_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/imagesIMAGE_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;@Servicepublic 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;@Controllerpublic class ItemController {@Autowired    private ItemService itemService;@RequestMapping("/item/{itemId}")@ResponseBodyprivate 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文件,报了 如下警告:

imageimage

处理办法:

直接 Window –> Preferences –> Maven –> Errors/Warnings –> Ignore 将两个警告,调成忽略就可处理。 如下图所示:

image

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

image.pngimage.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.pngimage.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 WebApplicationContextlog4j: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

测试成功!!!

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 如何搭建maven中,分布式工程(1)–整合SSM

发表回复