• maven集成SSM项目,Tomcat部署运行——SSM整合框架搭建(二)


    系列一jetty试运行的maven框架成功后,我决定先从熟悉的Tomcat运行入手

    第一步:添加相关依赖包

    各种包,分为为:springDI依赖包(最基础的必备包),AOP相关依赖,springMVC依赖包,servlet及jsp相关依赖,数据库库依赖包,mybatis及Mybatis-spring依赖包等,上代码pom.xml

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      3 <modelVersion>4.0.0</modelVersion>
      4 <groupId>com.yusys.pro</groupId>
      5 <artifactId>NewsManagement</artifactId>
      6 <packaging>war</packaging>
      7 <version>0.0.1-SNAPSHOT</version>
      8 <name>NewsManagement Maven Webapp</name>
      9 <url>http://maven.apache.org</url>
     10 <dependencies>
     11 <dependency>
     12 <groupId>junit</groupId>
     13 <artifactId>junit</artifactId>
     14 <version>3.8.1</version>
     15 <scope>test</scope>
     16 </dependency>
     17 <!-- <dependency>
     18 <groupId>javax.servlet</groupId>
     19 <artifactId>javax.servlet-api</artifactId>
     20 <version>3.0.1</version>
     21 </dependency> -->
     22 <!-- sping相关依赖包 -->
     23 <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
     24 <dependency>
     25 <groupId>commons-logging</groupId>
     26 <artifactId>commons-logging</artifactId>
     27 <version>1.2</version>
     28 </dependency>
     29 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
     30 <dependency>
     31 <groupId>org.springframework</groupId>
     32 <artifactId>spring-context</artifactId>
     33 <version>4.3.13.RELEASE</version>
     34 </dependency>
     35 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
     36 <dependency>
     37 <groupId>org.springframework</groupId>
     38 <artifactId>spring-core</artifactId>
     39 <version>4.3.13.RELEASE</version>
     40 </dependency>
     41 <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
     42 <dependency>
     43 <groupId>org.springframework</groupId>
     44 <artifactId>spring-beans</artifactId>
     45 <version>4.3.13.RELEASE</version>
     46 </dependency>
     47 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
     48 <dependency>
     49 <groupId>org.springframework</groupId>
     50 <artifactId>spring-context-support</artifactId>
     51 <version>4.3.13.RELEASE</version>
     52 </dependency>
     53 <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
     54 <dependency>
     55 <groupId>org.springframework</groupId>
     56 <artifactId>spring-expression</artifactId>
     57 <version>4.3.13.RELEASE</version>
     58 </dependency>
     59 <!-- spring AOP 依赖包 -->
     60 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
     61 <dependency>
     62 <groupId>org.springframework</groupId>
     63 <artifactId>spring-aspects</artifactId>
     64 <version>4.3.13.RELEASE</version>
     65 </dependency>
     66 
     67 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
     68 <dependency>
     69 <groupId>org.springframework</groupId>
     70 <artifactId>spring-aop</artifactId>
     71 <version>4.3.13.RELEASE</version>
     72 </dependency>
     73 <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
     74 <dependency>
     75 <groupId>aopalliance</groupId>
     76 <artifactId>aopalliance</artifactId>
     77 <version>1.0</version>
     78 </dependency>
     79 <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
     80 <dependency>
     81 <groupId>org.aspectj</groupId>
     82 <artifactId>aspectjweaver</artifactId>
     83 <version>1.8.13</version>
     84 </dependency>
     85 <!-- springMVC依赖包 -->
     86 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
     87 <dependency>
     88 <groupId>org.springframework</groupId>
     89 <artifactId>spring-webmvc</artifactId>
     90 <version>4.3.13.RELEASE</version>
     91 </dependency>
     92 <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
     93 <dependency>
     94 <groupId>org.springframework</groupId>
     95 <artifactId>spring-web</artifactId>
     96 <version>4.3.13.RELEASE</version>
     97 </dependency>
     98 
     99 <!-- mybatis依赖包 -->
    100 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    101 <dependency>
    102 <groupId>org.mybatis</groupId>
    103 <artifactId>mybatis</artifactId>
    104 <version>3.4.1</version>
    105 </dependency>
    106 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    107 <dependency>
    108 <groupId>org.mybatis</groupId>
    109 <artifactId>mybatis-spring</artifactId>
    110 <version>1.3.0</version>
    111 </dependency>
    112 
    113 <!-- servlet及其他相关依赖 -->
    114 <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    115 <dependency>
    116 <groupId>javax.servlet</groupId>
    117 <artifactId>javax.servlet-api</artifactId>
    118 <version>3.1.0</version>
    119 <scope>provided</scope>
    120 </dependency>
    121 <!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
    122 <dependency>
    123 <groupId>javax.servlet</groupId>
    124 <artifactId>jsp-api</artifactId>
    125 <version>2.0</version>
    126 <scope>provided</scope>
    127 </dependency>
    128 <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    129 <dependency>
    130 <groupId>log4j</groupId>
    131 <artifactId>log4j</artifactId>
    132 <version>1.2.17</version>
    133 </dependency>
    134 <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    135 <dependency>
    136 <groupId>javax.servlet</groupId>
    137 <artifactId>jstl</artifactId>
    138 <version>1.2</version>
    139 </dependency>
    140 
    141 <!-- 数据库相关依赖包 -->
    142 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    143 <dependency>
    144 <groupId>org.springframework</groupId>
    145 <artifactId>spring-jdbc</artifactId>
    146 <version>4.3.13.RELEASE</version>
    147 </dependency>
    148 
    149 <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
    150 <dependency>
    151 <groupId>commons-dbcp</groupId>
    152 <artifactId>commons-dbcp</artifactId>
    153 <version>1.4</version>
    154 </dependency>
    155 
    156 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    157 <dependency>
    158 <groupId>mysql</groupId>
    159 <artifactId>mysql-connector-java</artifactId>
    160 <version>6.0.6</version>
    161 </dependency>
    162 
    163 <!-- 添加redis的jar 包 -->
    164 <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    165 <dependency>
    166 <groupId>redis.clients</groupId>
    167 <artifactId>jedis</artifactId>
    168 <version>2.9.0</version>
    169 </dependency>
    170 
    171  
    172 
    173 </dependencies>
    174 
    175 <build>
    176 <plugins> <plugin> <!-- 项目报红叉,下面子文件没有一个报红叉原因是缺少插件的配置项 ,jre不能修改成为1.7,update后即又重回1.5的原因-->
    177 <artifactId>maven-compiler-plugin</artifactId> <configuration> <target>1.7</target> <source>1.7</source> <encoding>UTF-8</encoding> </configuration> 
    178 </plugin>
    179 </plugins> 
    180 <finalName>NewsManagement</finalName>
    181 </build>
    182 </project>

     第二步,配置几个基础文件。应该从web.xml入手。这个是整个web程序的入口,相当于java的main函数,我在实际编码时并没有按照这个顺序,这里为了逻辑清楚,能够串联起来,我还是从这里说起,上代码;并将进行相应说明:

    1、配置web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
     3 <display-name>OrdersForGoods</display-name>
     4 
     5 <display-name>Archetype Created Web Application</display-name>
     6 <!-- 配置监听器加载上下文参数spring的配置文件 ,这个的作用是当tomcat启动程序时,首先会监听,加载相应的配置,
      好比是application读取文件流,构建一个bean工厂,用来为依赖注入DI做准备
    --> 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath:applicationContext-mybatis.xml</param-value> 10 </context-param> 11 <listener> 12 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 13 </listener> 14 <!-- 配置springmvc的servlet ,前端控制器作用就不说了,大家都明白--> 15 <servlet> 16 <servlet-name>springmvc</servlet-name> 注意:这里的的名字一定要和springMVC的配置文件XXX-servlet.xml中的XXX名字完全相等,否则报错 17 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 18 <init-param> 19 <param-name>contextConfigLocation</param-name> 20 <param-value>classpath:springmvc-servlet.xml</param-value> 21 </init-param> 22 <load-on-startup>1</load-on-startup> 23 </servlet> 24 <servlet-mapping> 25 <servlet-name>springmvc</servlet-name> 26 <url-pattern>/</url-pattern><!--这里用/是根目录的意思,之前我用/*是有问题的,这两者还是有区别的,大家自行文度娘--> 27 </servlet-mapping> 28 <!-- 配置过滤器 设置编码格式--> 29 <filter> 30 <filter-name>encoding</filter-name> 31 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 32 <init-param> 33 <param-name>encoding</param-name> 34 <param-value>utf-8</param-value> 35 </init-param> 36 </filter> 37 <filter-mapping> 38 <filter-name>encoding</filter-name> 39 <url-pattern>/*</url-pattern> 40 </filter-mapping> 41 <!-- 配置log4j日志的监听 容器启动就加载,同加载applicationContext.xml类似--> 42 <context-param> 43 <param-name>log4j</param-name> 44 <param-value>classpath:log4j.properties</param-value> 45 </context-param> 46 <listener> 47 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 48 </listener> 49 </web-app>

    2、applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3   xmlns="http://www.springframework.org/schema/beans" 
     4   xmlns:aop="http://www.springframework.org/schema/aop"
     5   xmlns:context="http://www.springframework.org/schema/context" 
     6   xmlns:tx="http://www.springframework.org/schema/tx"
     7   xmlns:cache="http://www.springframework.org/schema/cache" 
     8   xmlns:p="http://www.springframework.org/schema/p"
     9   xsi:schemaLocation="http://www.springframework.org/schema/beans 
    10      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    11      http://www.springframework.org/schema/aop
    12      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    13      http://www.springframework.org/schema/context
    14      http://www.springframework.org/schema/context/spring-context-4.3.xsd
    15      http://www.springframework.org/schema/tx
    16      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    17      http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd"
    18      >
    19      <!-- 加载jdbc属性文件 -->
    20      <context:property-placeholder location="classpath:jdbc.properties"/>
    21      <!-- 配置数据源 -->
    22      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
    23          <property name="driverClassName" value="${jdbc.driver}"/>
    24          <property name="url" value="${jdbc.url}"/>
    25         <property name="username" value="${jdbc.username}"/>
    26          <property name="password" value="${jdbc.password}"/>    
    27      </bean>
    28      <!-- 让事务注解生效 -->
    29      <tx:annotation-driven/>
    30      <!--配置事务 -->
    31      <bean id="transaction" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    32      <property name="dataSource" ref="dataSource"/>
    33      </bean>
    34      <!-- 配置mybatis的sqlSessionFactory -->
    35      <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
    36      <property name="dataSource" ref="dataSource"/>
    37      <property name="configLocation" value="classpath:mybatis-config.xml"/>
    38      </bean>
    39      <!-- mapper 接口所在包名,spring自动查找下面mapper.xml并生产相应实例,让接口和xml相关联 -->
    40      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    41      <property name="basePackage" value="com.yusys.dao"/>
    42      </bean>
    43      
    44 </beans>

    3、springmvc-servlet.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:p="http://www.springframework.org/schema/p"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xmlns:mvc="http://www.springframework.org/schema/mvc"
     7     xsi:schemaLocation="
     8         http://www.springframework.org/schema/beans
     9         http://www.springframework.org/schema/beans/spring-beans.xsd
    10         http://www.springframework.org/schema/context
    11         http://www.springframework.org/schema/context/spring-context.xsd
    12         http://www.springframework.org/schema/mvc
    13         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    14         <!-- 使项目所有注解生效 -->
    15         <context:component-scan base-package="com.yusys"/>
    16         <!-- 配置视图解析器 -->
    17         <bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    18             <property name="prefix" value="/WEB-INF/jsp/"/>
    19             <property name="suffix" value=".jsp"/>
    20         </bean>
    21         
    22         
    23         
    24         </beans>

    4、mybatis-config.xml

      这个文件配置很少,主要是因为在SSM整合中,实际上主要是整合了mybatis,之前由mybatis进行配置的与数据库的联系全交给了spring去做,如数据源、连接池、事务等等。这些交给spring以后,当程序需要的时候,就会依赖注入的方式提供给程序用。springmvc主要作用可以先简单理解为调配调用,视图解析。

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE configuration
     3   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
     4   "http://mybatis.org/dtd/mybatis-3-config.dtd">
     5   <configuration>
     6     <settings>
     7           <setting name="lazyLoadingEnabled" value="true"/>
     8       </settings>
     9       <typeAliases>
    10           <package name="com.yusys.entity"/>
    11       </typeAliases>
    12   </configuration>

    基础的配置文件就是这些,至于mapper.xml,属于dao层的内容,我倾向于将它理解为java代码的另一种特殊存在形式,放在后文进行说明

    第三步、就是编写代码了(这里只是为了演示说明这个过程,就放一个相关的entity、dao、service、controller,其他的有兴趣的自己添加补充)。

    1、entity实体类

     1 package com.yusys.entity;
     2 
     3 import java.util.Date;
     4 
     5 public class NewsDetail {
     6 
     7     private int id;
     8     private String title;
     9     private String summary;
    10     private String author;
    11     private Date createdate;
    12     public int getId() {
    13         return id;
    14     }
    15     public void setId(int id) {
    16         this.id = id;
    17     }
    18     public String getTitle() {
    19         return title;
    20     }
    21     public void setTitle(String title) {
    22         this.title = title;
    23     }
    24     public String getSummary() {
    25         return summary;
    26     }
    27     public void setSummary(String summary) {
    28         this.summary = summary;
    29     }
    30     public String getAuthor() {
    31         return author;
    32     }
    33     public void setAuthor(String author) {
    34         this.author = author;
    35     }
    36     public Date getCreatedate() {
    37         return createdate;
    38     }
    39     public void setCreatedate(Date createdate) {
    40         this.createdate = createdate;
    41     }
    42     
    43 }

    2、dao层

     1 package com.yusys.dao;
     2 
     3 import java.util.List;
     4 
     5 import com.yusys.entity.NewsDetail;
     6 
     7 public interface NewsDetailMapper {
     8     
     9     public List<NewsDetail> getAll();
    10     
    11     public List<NewsDetail> getNewsDetailByTitle();
    12 }
     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     3   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     4   <mapper namespace="com.yusys.dao.NewsDetailMapper">
     5   <select id="getAll" resultType="NewsDetail">
     6       select * from news_detail
     7   </select>
     8   <select id="getNewsDetailByTitle" parameterType="NewsDetail" resultType="NewsDetail">
     9       select * from news_detail where title like concat('%',#{title},'%') 
    10   </select>
    11   </mapper>

    3、service层

     1 package com.yusys.service;
     2 
     3 import java.util.List;
     4 
     5 import com.yusys.entity.NewsDetail;
     6 
     7 public interface NewsDetailService {
     8 
     9     public abstract List<NewsDetail> getAll();
    10 
    11     public abstract List<NewsDetail> getNewsDetailByTitle();
    12 
    13 }
    NewsDetailService
     1 package com.yusys.service;
     2 
     3 import java.util.List;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Service;
     7 
     8 import com.yusys.dao.NewsDetailMapper;
     9 import com.yusys.entity.NewsDetail;
    10 @Service
    11 public class NewsDetailServiceImpl implements NewsDetailService {
    12     @Autowired
    13     NewsDetailMapper newsDetailMpper;
    14     
    15     /* (non-Javadoc)
    16      * @see com.yusys.service.NewsDetailService#getAll()
    17      */
    18     @Override
    19     public List<NewsDetail> getAll(){
    20         return newsDetailMpper.getAll();
    21     }
    22     
    23     /* (non-Javadoc)
    24      * @see com.yusys.service.NewsDetailService#getNewsDetailByTitle()
    25      */
    26     @Override
    27     public List<NewsDetail> getNewsDetailByTitle(){
    28         return newsDetailMpper.getNewsDetailByTitle();
    29     }
    30 }

    4、controller层

     1 package com.yusys.controller;
     2 
     3 import java.util.List;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.ui.Model;
     8 import org.springframework.web.bind.annotation.RequestMapping;
     9 
    10 import com.yusys.entity.NewsDetail;
    11 import com.yusys.service.NewsDetailService;
    12 @Controller
    13 public class NewsDetailController {
    14     @Autowired
    15     NewsDetailService newsDetailService;
    16     
    17     @RequestMapping(value="/newsDetailList")
    18     public String getAllDetail(Model model){
    19         List<NewsDetail> list=newsDetailService.getAll();
    20         model.addAttribute("list", list);
    21         return "newsDetail";
    22     }
    23 }

    第四步、创建newsDetail.jsp进行显示。

    第五步、部署tomcat进行运行

    1、

    2、

    3、点击下一步后添加项目后finish

    4、tomcat未启动是双击

    5、进入Overview界面,选择以下配置,关闭该界面

    6、启动tomcat,运行程序,注掉controller中的方法调用,让其直接转到jsp页面,效果显现,一切OK,但是放开里面的方法,出现了问题,mysql的jar和tomcat的jar冲突的问题,具体问题及其他见相应的博文——maven集成SSM项目,Tomcat部署运行——SSM整合框架搭建(二)之问题

  • 相关阅读:
    MT7621 将gpio对应pin设置gpio模式
    openwrt jshn.h 详细用法数组遍历
    sql时间处理(使用函数DATEPART和STR)
    (1)Node.js 与 NPM 的 介绍、安装配置
    The elements in the "SELECT LIST" list must be separated using commas.
    1085. 【GDOI2008】彩球游戏
    3283. 【GDOI2013】大山王国的科举考试
    01.17~01.27 总结
    5190. 【NOI2017模拟7.1】景中人
    3647. 【GDOI2014】oj
  • 原文地址:https://www.cnblogs.com/xiaoyao-001/p/8277491.html
Copyright © 2020-2023  润新知