欢迎光临
感谢一路有你

ssm框架搭建

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 

整体结构

基本的配置

pom.xml


<code class="">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;>
  &lt;modelVersion>4.0.0&lt;/modelVersion>

  &lt;groupId>con.zf&lt;/groupId>
  &lt;artifactId>ssm2&lt;/artifactId>
  &lt;version>1.0-SNAPSHOT&lt;/version>
  &lt;packaging>jar&lt;/packaging>

  &lt;name>ssm2 Maven Webapp&lt;/name>
  &lt;!-- FIXME change it to the project's website -->
  &lt;url>http://www.example.com&lt;/url>
&lt;!--  &lt;modules>-->
&lt;!--    &lt;module>ssm_bean&lt;/module>-->
&lt;!--    &lt;module>ssm_dao&lt;/module>-->
&lt;!--    &lt;module>ssm_service&lt;/module>-->
&lt;!--    &lt;module>ssm_web1&lt;/module>-->
&lt;!--  &lt;/modules>-->
  &lt;properties>
    &lt;project.build.sourceEncoding>UTF-8&lt;/project.build.sourceEncoding>
    &lt;maven.compiler.source>1.7&lt;/maven.compiler.source>
    &lt;maven.compiler.target>1.7&lt;/maven.compiler.target>
    &lt;!-- spring版本号 -->
    &lt;spring.version>5.0.2.RELEASE&lt;/spring.version>
    &lt;!-- mybatis版本号 -->
    &lt;mybatis.version>3.2.6&lt;/mybatis.version>
    &lt;!-- log4j日志文件管理包版本 -->
    &lt;slf4j.version>1.7.7&lt;/slf4j.version>
    &lt;log4j.version>1.2.17&lt;/log4j.version>
    &lt;c3p0.version>0.9.5.2&lt;/c3p0.version>
    &lt;taglibs.version>1.1.2&lt;/taglibs.version>
  &lt;/properties>

  &lt;dependencies>
    &lt;dependency>
      &lt;groupId>junit&lt;/groupId>
      &lt;artifactId>junit&lt;/artifactId>
      &lt;version>4.11&lt;/version>
      &lt;scope>compile&lt;/scope>
    &lt;/dependency>
    &lt;!-- spring核心包 -->
    &lt;dependency>
      &lt;groupId>org.springframework&lt;/groupId>
      &lt;artifactId>spring-core&lt;/artifactId>
      &lt;version>${spring.version}&lt;/version>
    &lt;/dependency>

    &lt;dependency>
      &lt;groupId>org.springframework&lt;/groupId>
      &lt;artifactId>spring-web&lt;/artifactId>
      &lt;version>${spring.version}&lt;/version>
    &lt;/dependency>
    &lt;dependency>
      &lt;groupId>org.springframework&lt;/groupId>
      &lt;artifactId>spring-oxm&lt;/artifactId>
      &lt;version>${spring.version}&lt;/version>
    &lt;/dependency>
    &lt;dependency>
      &lt;groupId>org.springframework&lt;/groupId>
      &lt;artifactId>spring-tx&lt;/artifactId>
      &lt;version>${spring.version}&lt;/version>
    &lt;/dependency>

    &lt;dependency>
      &lt;groupId>org.springframework&lt;/groupId>
      &lt;artifactId>spring-jdbc&lt;/artifactId>
      &lt;version>${spring.version}&lt;/version>
    &lt;/dependency>

    &lt;dependency>
      &lt;groupId>org.springframework&lt;/groupId>
      &lt;artifactId>spring-webmvc&lt;/artifactId>
      &lt;version>${spring.version}&lt;/version>
    &lt;/dependency>
    &lt;dependency>
      &lt;groupId>org.springframework&lt;/groupId>
      &lt;artifactId>spring-aop&lt;/artifactId>
      &lt;version>${spring.version}&lt;/version>
    &lt;/dependency>

    &lt;dependency>
      &lt;groupId>org.springframework&lt;/groupId>
      &lt;artifactId>spring-context-support&lt;/artifactId>
      &lt;version>${spring.version}&lt;/version>
    &lt;/dependency>

    &lt;dependency>
      &lt;groupId>org.springframework&lt;/groupId>
      &lt;artifactId>spring-test&lt;/artifactId>
      &lt;version>${spring.version}&lt;/version>
    &lt;/dependency>
    &lt;!-- mybatis核心包 -->
    &lt;dependency>
      &lt;groupId>org.mybatis&lt;/groupId>
      &lt;artifactId>mybatis&lt;/artifactId>
      &lt;version>${mybatis.version}&lt;/version>
    &lt;/dependency>
    &lt;!-- mybatis/spring包 -->
    &lt;dependency>
      &lt;groupId>org.mybatis&lt;/groupId>
      &lt;artifactId>mybatis-spring&lt;/artifactId>
      &lt;version>1.2.2&lt;/version>
    &lt;/dependency>
    &lt;!-- 导入java ee jar 包 -->
    &lt;dependency>
      &lt;groupId>javax&lt;/groupId>
      &lt;artifactId>javaee-api&lt;/artifactId>
      &lt;version>7.0&lt;/version>
    &lt;/dependency>

    &lt;!-- 导入Mysql数据库链接jar包 -->
    &lt;dependency>
      &lt;groupId>mysql&lt;/groupId>
      &lt;artifactId>mysql-connector-java&lt;/artifactId>
      &lt;version>5.1.30&lt;/version>
    &lt;/dependency>
    &lt;!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
    &lt;dependency>
      &lt;groupId>commons-dbcp&lt;/groupId>
      &lt;artifactId>commons-dbcp&lt;/artifactId>
      &lt;version>1.2.2&lt;/version>
    &lt;/dependency>
    &lt;!-- JSTL标签类 -->
    &lt;dependency>
      &lt;groupId>jstl&lt;/groupId>
      &lt;artifactId>jstl&lt;/artifactId>
      &lt;version>1.2&lt;/version>
    &lt;/dependency>
    &lt;!-- 日志文件管理包 -->
    &lt;!-- log start -->
    &lt;dependency>
      &lt;groupId>log4j&lt;/groupId>
      &lt;artifactId>log4j&lt;/artifactId>
      &lt;version>${log4j.version}&lt;/version>
    &lt;/dependency>


    &lt;!-- 数据连接池 -->
    &lt;dependency>
      &lt;groupId>com.mchange&lt;/groupId>
      &lt;artifactId>c3p0&lt;/artifactId>
      &lt;version>${c3p0.version}&lt;/version>
    &lt;/dependency>

    &lt;dependency>
      &lt;groupId>taglibs&lt;/groupId>
      &lt;artifactId>standard&lt;/artifactId>
      &lt;version>${taglibs.version}&lt;/version>
    &lt;/dependency>

    &lt;dependency>
      &lt;groupId>org.slf4j&lt;/groupId>
      &lt;artifactId>slf4j-api&lt;/artifactId>
      &lt;version>${slf4j.version}&lt;/version>
    &lt;/dependency>
    &lt;dependency>
      &lt;groupId>org.slf4j&lt;/groupId>
      &lt;artifactId>slf4j-log4j12&lt;/artifactId>
      &lt;version>${slf4j.version}&lt;/version>
    &lt;/dependency>

    &lt;!-- 导入servlet-api/jsp -->
    &lt;dependency>
      &lt;groupId>javax.servlet&lt;/groupId>
      &lt;artifactId>javax.servlet-api&lt;/artifactId>
      &lt;version>3.1.0&lt;/version>
      &lt;scope>provided&lt;/scope>
    &lt;/dependency>
    &lt;!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
    &lt;dependency>
      &lt;groupId>javax.servlet.jsp&lt;/groupId>
      &lt;artifactId>javax.servlet.jsp-api&lt;/artifactId>
      &lt;version>2.3.1&lt;/version>
      &lt;scope>provided&lt;/scope>
    &lt;/dependency>
    &lt;dependency>
      &lt;groupId>com.github.pagehelper&lt;/groupId>
      &lt;artifactId>pagehelper&lt;/artifactId>
      &lt;version>5.1.2&lt;/version>
    &lt;/dependency>

  &lt;/dependencies>

  &lt;build>
    &lt;finalName>ssm2&lt;/finalName>
    &lt;pluginManagement>&lt;!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      &lt;plugins>
        &lt;plugin>
          &lt;artifactId>maven-clean-plugin&lt;/artifactId>
          &lt;version>3.1.0&lt;/version>
        &lt;/plugin>
        &lt;!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        &lt;plugin>
          &lt;artifactId>maven-resources-plugin&lt;/artifactId>
          &lt;version>3.0.2&lt;/version>
        &lt;/plugin>
        &lt;plugin>
          &lt;artifactId>maven-compiler-plugin&lt;/artifactId>
          &lt;version>3.8.0&lt;/version>
        &lt;/plugin>
        &lt;plugin>
          &lt;artifactId>maven-surefire-plugin&lt;/artifactId>
          &lt;version>2.22.1&lt;/version>
        &lt;/plugin>
        &lt;plugin>
          &lt;artifactId>maven-war-plugin&lt;/artifactId>
          &lt;version>3.2.2&lt;/version>
        &lt;/plugin>
        &lt;plugin>
          &lt;artifactId>maven-install-plugin&lt;/artifactId>
          &lt;version>2.5.2&lt;/version>
        &lt;/plugin>
        &lt;plugin>
          &lt;artifactId>maven-deploy-plugin&lt;/artifactId>
          &lt;version>2.8.2&lt;/version>
        &lt;/plugin>
      &lt;/plugins>
    &lt;/pluginManagement>
  &lt;/build>
&lt;/project>

</code>

web.xml


<code class=""><br />&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
         xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&quot;
         version=&quot;3.1&quot;>
&lt;!--  &lt;display-name>Archetype Created Web Application&lt;/display-name>-->

  &lt;!-- 配置监听器 -->
  &lt;listener>
    &lt;listener-class>org.springframework.web.context.ContextLoaderListener&lt;/listener-class>
  &lt;/listener>
  &lt;listener>
    &lt;listener-class>org.springframework.web.context.request.RequestContextListener&lt;/listener-class>
  &lt;/listener>
  &lt;!-- 配置加载类路径的配置文件 -->
  &lt;context-param>
    &lt;param-name>contextConfigLocation&lt;/param-name>
    &lt;param-value>classpath*:applicationContext.xml&lt;/param-value>
  &lt;/context-param>



  &lt;!-- 前端控制器(加载classpath:spring-mvc.xml 服务器启动创建servlet) -->
  &lt;servlet>
    &lt;servlet-name>dispatcherServlet&lt;/servlet-name>
    &lt;servlet-class>org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class>
    &lt;!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
    &lt;init-param>
      &lt;param-name>contextConfigLocation&lt;/param-name>
      &lt;param-value>classpath:springmvc.xml&lt;/param-value>
    &lt;/init-param>
    &lt;!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
    &lt;load-on-startup>1&lt;/load-on-startup>
  &lt;/servlet>
  &lt;servlet-mapping>
    &lt;servlet-name>dispatcherServlet&lt;/servlet-name>
    &lt;url-pattern>*.do&lt;/url-pattern>
  &lt;/servlet-mapping>

  &lt;!-- 解决中文乱码过滤器 -->
  &lt;filter>
    &lt;filter-name>characterEncodingFilter&lt;/filter-name>
    &lt;filter-class>org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class>
    &lt;init-param>
      &lt;param-name>encoding&lt;/param-name>
      &lt;param-value>UTF-8&lt;/param-value>
    &lt;/init-param>
  &lt;/filter>
  &lt;filter-mapping>
    &lt;filter-name>characterEncodingFilter&lt;/filter-name>
    &lt;url-pattern>/*&lt;/url-pattern>
  &lt;/filter-mapping>

&lt;/web-app>


</code>

配置文件
applicationContext.xml


<code class="">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
       xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
       xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
       xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
       xsi:schemaLocation=&quot;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/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd&quot;>

    &lt;!--自动扫描 开启注解扫描-->
    &lt;context:component-scan base-package=&quot;com.zf&quot;>
        &lt;!--        配置不扫描的路径-->
        &lt;context:exclude-filter type=&quot;annotation&quot; expression=&quot;org.springframework.stereotype.Controller&quot;/>
    &lt;/context:component-scan>




&lt;!--    spring整合mybatis-->
    &lt;!-- 1.配置数据库相关参数properties的属性:${url} -->
    &lt;context:property-placeholder location=&quot;classpath:db.properties&quot;/>
    &lt;!-- 2.配置数据源 -->
    &lt;bean id=&quot;dataSource&quot; class=&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot;>
        &lt;property name=&quot;driverClass&quot; value=&quot;${jdbc.driver}&quot;/>
        &lt;property name=&quot;jdbcUrl&quot; value=&quot;${jdbc.url}&quot;/>
        &lt;property name=&quot;user&quot; value=&quot;${jdbc.username}&quot;/>
        &lt;property name=&quot;password&quot; value=&quot;${jdbc.password}&quot;/>
        &lt;property name=&quot;maxPoolSize&quot; value=&quot;30&quot;/>
        &lt;property name=&quot;minPoolSize&quot; value=&quot;2&quot;/>
    &lt;/bean>

    &lt;!-- 3.配置SqlSessionFactory对象 -->
    &lt;bean id=&quot;sqlSessionFactory&quot; class=&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;>
        &lt;!-- 注入数据库连接池 -->
        &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/>
        &lt;!-- 扫描bean包 使用别名 -->
&lt;!--        &lt;property name=&quot;typeAliasesPackage&quot; value=&quot;com.zhongruan.bean&quot;>&lt;/property>-->
        &lt;!--配置加载映射文件 UserMapper.xml-->
&lt;!--        &lt;property name=&quot;mapperLocations&quot; value=&quot;classpath:applicationContext.xml&quot;/>-->


&lt;!--        加入分页-->
        &lt;property name=&quot;plugins&quot;>
            &lt;array>
                &lt;bean class=&quot;com.github.pagehelper.PageInterceptor&quot;>
                    &lt;property name=&quot;properties&quot;>
                        &lt;!--使用下面的方式配置参数,一行配置一个 -->
                        &lt;value>
                            helperDialect=mysql
                            offsetAsPageNum=true
                            rowBoundsWithCount=true
                            pageSizeZero=true
                            reasonable=true
                        &lt;/value>
                    &lt;/property>
                &lt;/bean>
            &lt;/array>
        &lt;/property>
    &lt;/bean>

    &lt;!-- 自动生成dao,mapper-->
    &lt;!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    &lt;bean class=&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;>
        &lt;!-- 给出需要扫描Dao接口包 -->
        &lt;property name=&quot;basePackage&quot; value=&quot;com.zf.dao&quot;/>
        &lt;!-- 注入sqlSessionFactory -->
&lt;!--        &lt;property name=&quot;sqlSessionFactoryBeanName&quot; value=&quot;sqlSessionFactory&quot;/>-->
    &lt;/bean>
    &lt;!-- 配置事务-->
&lt;!--     5.配置事务管理器 -->
    &lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;>
        &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/>
    &lt;/bean>
&lt;!--    配置事务通知-->
    &lt;tx:advice id=&quot;txAdvice&quot; transaction-manager=&quot;transactionManager&quot;>
        &lt;tx:attributes>
            &lt;tx:method name=&quot;find*&quot; read-only=&quot;true&quot;>&lt;/tx:method>
            &lt;tx:method name=&quot;*&quot; isolation=&quot;DEFAULT&quot;>&lt;/tx:method>
        &lt;/tx:attributes>
    &lt;/tx:advice>

&lt;!--    配置aop增强-->
    &lt;aop:config>
&lt;!--        &lt;aop:advisor advice-ref=&quot;txAdvice&quot; pointcut=&quot;execution(* com.zf.service.impl.*ServiceImpl.*(..))&quot; />-->
    &lt;/aop:config>


    &lt;!-- 6.开启事务注解   -->
&lt;!--    &lt;tx:annotation-driven>&lt;/tx:annotation-driven>-->



&lt;/beans>
</code>

db.properties


<code class="">jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/java_test?useSSL=true&amp;characterEncoding=utf-8
jdbc.username=root
jdbc.password=
</code>

log4j.properties


<code class=""># Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
</code>

springmvc.xml


<code class="">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
       xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
       xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
       xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
       xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-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/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd&quot;>

    &lt;!-- 1.注解扫描位置-->
    &lt;context:component-scan base-package=&quot;com.zf&quot;>
        &lt;context:include-filter type=&quot;annotation&quot; expression=&quot;org.springframework.stereotype.Controller&quot;/>
    &lt;/context:component-scan>

    &lt;!-- 2.配置映射处理和适配器-->
&lt;!--    &lt;bean class=&quot;org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping&quot;/>-->
&lt;!--    &lt;bean class=&quot;org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter&quot;/>-->

    &lt;!-- 3.视图的解析器-->
    &lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;>
        &lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/page/&quot; />
        &lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot; />
    &lt;/bean>

&lt;!--    过滤静态资源-->

&lt;!--    &lt;mvc:resources mapping=&quot;/css/**&quot; location=&quot;/css/&quot;/>-->
&lt;!--    &lt;mvc:resources mapping=&quot;/js/**&quot; location=&quot;/js/&quot;/>-->
&lt;!--    &lt;mvc:resources mapping=&quot;/image/**&quot; location=&quot;/images/&quot;/>-->

    &lt;mvc:annotation-driven/>

&lt;/beans>

</code>

操作步骤

1.创建实体domain

2.创建dao层

3. 创建jsp

4.创建controller

创建service的接口

service的实现类

5.进入list.jsp

赞(1) 打赏
未经允许不得转载:王明昌博客 » ssm框架搭建
分享到: 更多 (0)

相关推荐

  • 暂无文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

×
订阅图标按钮