搭建框架SSM_maven,综合配置

项目的结构:

 

web.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Test_s_s_m</display-name>
  <!-- 404错误拦截 -->
       <error-page>
         <error-code>404</error-code>
         <location>/error404.jsp</location>
       </error-page>
      <!-- 500错误拦截 -->
       <error-page>
         <error-code>500</error-code>
         <location>/error500.jsp</location>
       </error-page>
          
          <!-- Spring和mybatis的配置文件 -->  
        <context-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:spring-mybatis.xml</param-value>  
        </context-param>  
        <!-- 编码过滤器 -->  
        <filter>  
            <filter-name>encodingFilter</filter-name>  
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
            <async-supported>true</async-supported>  
            <init-param>  
                <param-name>encoding</param-name>  
                <param-value>UTF-8</param-value>  
            </init-param>  
        </filter>  
        
        <filter-mapping>  
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>  
        <!-- Spring监听器 -->  
        <listener>  
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
        </listener>  
        <!-- 防止Spring内存溢出监听器 -->  
        <listener>  
            <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
        </listener>  
      
        <!-- Spring MVC servlet -->  
        <servlet>  
            <servlet-name>SpringMVC</servlet-name>  
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
            
            <!-- ContextconfigLocation配置springmvc加载的配置文件 适配器、处理映射器等-->
            <init-param>  
                <param-name>contextConfigLocation</param-name>  
                <param-value>classpath:spring-mvc.xml</param-value>  
            </init-param>  
            
            <load-on-startup>1</load-on-startup>  
            <async-supported>true</async-supported>  
        </servlet>  
        <servlet-mapping>  
            <servlet-name>SpringMVC</servlet-name>  
            <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->  
            <url-pattern>/</url-pattern>  
        </servlet-mapping>  
        <welcome-file-list>  
            <welcome-file>/index.jsp</welcome-file>  
        </welcome-file-list> 
  
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

spring-mvc.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-4.3.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-4.3.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">  
      
   <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->  
   <context:component-scan base-package="com.dkt.controller" />  
  
   <!-- 定义跳转的 前后缀 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->  
        <property name="prefix" value="/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  
  
  
      <!-- 配置注解的处理器映射器和处理器适配器 -->
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    
    
      <!-- 自定义参数类型绑定 -->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
     <property name="converters">
         <list>
             <!-- 日期类型绑定 -->
             <bean class="com.dkt.util.CustomDateConverter"/>
         </list>
     </property>
    </bean>
  
  
</beans>  

 

spring-mybatis.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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.3.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">                       
    <!-- 自动扫描 -->  
    <context:component-scan base-package="com.dkt"/>
    
    <!-- 引入配置文件 -->
    <bean id="fileBean"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>  
      <!-- 配置数据源  -->
    <bean id="datas" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />  
        <property name="username" value="${username}" />  
        <property name="password" value="${password}" />  
        <!-- 初始化连接大小 -->  
        <property name="initialSize" value="${initialSize}"></property>  
        <!-- 连接池最大数量 -->  
        <property name="maxActive" value="${maxActive}"></property>  
        <!-- 连接池最大空闲 -->  
        <property name="maxIdle" value="${maxIdle}"></property>  
        <!-- 连接池最小空闲 -->  
        <property name="minIdle" value="${minIdle}"></property>  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="${maxWait}"></property>  
    </bean>  
  
    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
    <bean id="sfactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="datas" />  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:com/dkt/entity/mapping/*.xml"></property>  
        <!-- 在springMvc中设置别名  -->
        <!-- 给映射的类配置别名 -->  
        <!-- 默认的别名是model类的首字母小写 -->  
        <!-- 如:StaffDeploy实体类。别名为:staffDeploy -->  
        <property name="typeAliasesPackage" value="com.dkt.entity" />
    </bean>  
    
  
    <!-- 配置mapper扫描器 DAO接口所在包名,Spring会自动查找其下的类和文件 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dkt.dao" />  
        <!-- ref引用一个已经存在的对象,ref可以引用其他的bean对象。
             value创建一个新的对象,value可以赋一些简单类型的值. 给每个 sqlSessionFactoryBean 创建一个新对象-->
        <property name="sqlSessionFactoryBeanName" value="sfactory"></property>  
    </bean>  
  
    <!-- (事务管理) -->  
    <bean id="trans" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="datas" />  
    </bean>  
    
    <!-- 通知 -->
    <tx:advice transaction-manager="trans" id="txAdvice">
        <tx:attributes>
            <!-- 传播行为 -->
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
 
    <!-- 配置aop  -->
    <aop:config>
        <aop:pointcut expression="execution(* com.dkt.service.impl.*.*(..))" id="pc"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
    </aop:config>
    
  
</beans> 

 

数据库连接文件jdbc.properties的配置:

driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/food
username=root
password=root
initialSize=0  
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000

 

entity、Dao和entiy的映射文件 是通过工具生成的!生成时一定要注意文件结构,不然导入进项目后会出错!!

 

实体/表映射文件: 注意路径!路径一定不能错!文件是使用工具生成的,生成时一定要注意!

<?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.dkt.dao.PersonTableMapper" >
  <resultMap id="BaseResultMap" type="com.dkt.entity.PersonTable" >
    <id column="pid" property="pid" jdbcType="INTEGER" />
    <result column="puser" property="puser" jdbcType="VARCHAR" />
    <result column="ppassword" property="ppassword" jdbcType="VARCHAR" />
    <result column="pname" property="pname" jdbcType="VARCHAR" />
    <result column="page" property="page" jdbcType="INTEGER" />
    <result column="pgender" property="pgender" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    pid, puser, ppassword, pname, page, pgender
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from person
    where pid = #{pid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from person
    where pid = #{pid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.dkt.entity.PersonTable" >
    insert into person (pid, puser, ppassword, 
      pname, page, pgender
      )
    values (#{pid,jdbcType=INTEGER}, #{puser,jdbcType=VARCHAR}, #{ppassword,jdbcType=VARCHAR}, 
      #{pname,jdbcType=VARCHAR}, #{page,jdbcType=INTEGER}, #{pgender,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.dkt.entity.PersonTable" >
    insert into person
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="pid != null" >
        pid,
      </if>
      <if test="puser != null" >
        puser,
      </if>
      <if test="ppassword != null" >
        ppassword,
      </if>
      <if test="pname != null" >
        pname,
      </if>
      <if test="page != null" >
        page,
      </if>
      <if test="pgender != null" >
        pgender,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="pid != null" >
        #{pid,jdbcType=INTEGER},
      </if>
      <if test="puser != null" >
        #{puser,jdbcType=VARCHAR},
      </if>
      <if test="ppassword != null" >
        #{ppassword,jdbcType=VARCHAR},
      </if>
      <if test="pname != null" >
        #{pname,jdbcType=VARCHAR},
      </if>
      <if test="page != null" >
        #{page,jdbcType=INTEGER},
      </if>
      <if test="pgender != null" >
        #{pgender,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.dkt.entity.PersonTable" >
    update person
    <set >
      <if test="puser != null" >
        puser = #{puser,jdbcType=VARCHAR},
      </if>
      <if test="ppassword != null" >
        ppassword = #{ppassword,jdbcType=VARCHAR},
      </if>
      <if test="pname != null" >
        pname = #{pname,jdbcType=VARCHAR},
      </if>
      <if test="page != null" >
        page = #{page,jdbcType=INTEGER},
      </if>
      <if test="pgender != null" >
        pgender = #{pgender,jdbcType=VARCHAR},
      </if>
    </set>
    where pid = #{pid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.dkt.entity.PersonTable" >
    update person
    set puser = #{puser,jdbcType=VARCHAR},
      ppassword = #{ppassword,jdbcType=VARCHAR},
      pname = #{pname,jdbcType=VARCHAR},
      page = #{page,jdbcType=INTEGER},
      pgender = #{pgender,jdbcType=VARCHAR}
    where pid = #{pid,jdbcType=INTEGER}
  </update>
</mapper>

 

service层 和 contorller 层的 的注入

package com.dkt.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dkt.dao.PersonTableMapper;
import com.dkt.entity.PersonTable;
import com.dkt.service.IpersonService;
@Service("personserice")
public class personService implements IpersonService{
    @Resource
    PersonTableMapper personDao;
    
    @Override
    public int deleteByPrimaryKey(Integer pid) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int insert(PersonTable record) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int insertSelective(PersonTable record) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public PersonTable selectByPrimaryKey(Integer pid) {
        
        return personDao.selectByPrimaryKey(pid);
    }

    @Override
    public int updateByPrimaryKeySelective(PersonTable record) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int updateByPrimaryKey(PersonTable record) {
        // TODO Auto-generated method stub
        return 0;
    }

}

 

 

contorller:

package com.dkt.controller;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dkt.entity.PersonTable;
import com.dkt.service.IpersonService;

@Controller
@Scope(value="prototype")
@RequestMapping("/person")
public class PersonController {
    @Resource(name="personserice")
    IpersonService perserv;
    
    @RequestMapping("/login")
    public ModelAndView login(){
        System.out.println("紧急你进!!!");
        PersonTable pertable = perserv.selectByPrimaryKey(2);
        System.out.println(pertable.toString());
        System.out.println("dsfdsfdsfsdfsd");
        return null;
        
    }
}

 

pom.xml配置:

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>SSM_M_Table</groupId>
    <artifactId>SSM_M_Table</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>SSM_M_Table</name>
    <description />
    <properties>
        <junit.version>4.12</junit.version>
        <jstl.version>1.2</jstl.version>
        <commons.version>1.2</commons.version>
        <mysql.version>5.1.39</mysql.version>
        <c3p0.version>0.9.2.1</c3p0.version>
        <hibernate.version>5.2.10.Final</hibernate.version>
        <hibernate.validator.version>5.4.0.Final</hibernate.validator.version>
        <spring.version>4.3.3.RELEASE</spring.version>
        <log.version>1.2.17</log.version>

        <!-- mybatis版本号 -->
        <mybatis.version>3.2.6</mybatis.version>
        <webVersion>3.1</webVersion>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>
    <dependencies>
        <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- mybatis核心包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!-- mybatis/spring包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- 上传组件包 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>

        <!-- 解决了jsp错误问题 -->
        <!-- <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> 
            <version>3.0</version> <scope>provided</scope> </dependency> -->
        <!-- https://mvnrepository.com/artifact/taglibs/standard -->
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>${commons.version}</version>
        </dependency>


        <!-- 引用c3p0 依赖 start -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>${c3p0.version}</version>
        </dependency>
        <!-- 引用c3p0 依赖 end -->

        <!-- Mysql start -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!-- Mysql end -->



        <!-- Spring start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- https://配置 aop 必须的jar包 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>

        <!-- Spring end -->

        <!-- Log4j start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log.version}</version>
        </dependency>
        <!-- Log4j end -->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.3</version>
        </dependency>
    </dependencies>

    <build>


        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

posted @ 2019-05-29 15:50  细竹赫映姬  阅读(99)  评论(0)    收藏  举报