使用IDEA搭建 SSM 框架

文章目录

      • 使用IDEA搭建 SSM 框架
        • SSM 框架整合流程

使用IDEA搭建 SSM 框架

SSM 框架整合流程

建项目之前你需要先准备好maven和maven的本地仓库

(1)首先需要新建一个Maven项目,然后导入相关的Maven依赖


<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-coreartifactId>
  <version>4.0.9.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-contextartifactId>
  <version>4.0.9.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-beansartifactId>
  <version>4.0.9.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-expressionartifactId>
  <version>4.0.9.RELEASEversion>
dependency>

<dependency>
  <groupId>org.mybatisgroupId>
  <artifactId>mybatisartifactId>
  <version>3.3.0version>
dependency>

<dependency>
  <groupId>org.mybatisgroupId>
  <artifactId>mybatis-springartifactId>
  <version>1.3.1version>
dependency>

<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-webartifactId>
  <version>4.0.9.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-webmvcartifactId>
  <version>4.0.9.RELEASEversion>
dependency>

<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-jdbcartifactId>
  <version>4.0.9.RELEASEversion>
dependency>

<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-aopartifactId>
  <version>4.0.9.RELEASEversion>
dependency>

<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-txartifactId>
  <version>4.0.9.RELEASEversion>
dependency>

<dependency>
  <groupId>com.mchangegroupId>
  <artifactId>c3p0artifactId>
  <version>0.9.5.2version>
dependency>

<dependency>
  <groupId>mysqlgroupId>
  <artifactId>mysql-connector-javaartifactId>
  <version>5.1.46version>
dependency>


<dependency>
  <groupId>log4jgroupId>
  <artifactId>log4jartifactId>
  <version>1.2.14version>
dependency>


<dependency>
  <groupId>org.projectlombokgroupId>
  <artifactId>lombokartifactId>
  <version>1.18.12version>
dependency>


<dependency>
  <groupId>commons-iogroupId>
  <artifactId>commons-ioartifactId>
  <version>1.3.2version>
dependency>
<dependency>
  <groupId>commons-fileuploadgroupId>
  <artifactId>commons-fileuploadartifactId>
  <version>1.3.3version>
dependency>
<dependency>
  <groupId>commons-langgroupId>
  <artifactId>commons-langartifactId>
  <version>2.6version>
dependency>

(2)创建项目的大体结构

先创建项目整体结构,之后创建dao层(dao)和dao层里的接口,service层,controller层(控制层),pojo层(实体层).在创建resources包,在其中创建db.properties(数据库连接文件)和log4j.properties(日志文件)

使用IDEA搭建 SSM 框架_第1张图片
(3) 创建mapper文件,连接到dao接口,在里面可以编写sql语句
使用IDEA搭建 SSM 框架_第2张图片

(4)创建applicationContext.xml文件来配置mybatis和spring的整合文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    
    <context:component-scan base-package="cn.kgc"/>

    
    <context:property-placeholder location="classpath*:db.properties"/>

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${mysql.username}"/>
        <property name="driverClass" value="${mysql.driver"/>
        <property name="password" value="${mysql.password}"/>
        <property name="jdbcUrl" value="${mysql.url"/>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <property name="typeAliasesPackage" value="cn.kgc.ssm.pojo"/>
        
        <property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"/>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.kgc"/>
    bean>

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>

    
    <tx:advice transaction-manager="transactionManager" id="txAdvice">
        <tx:attributes>
            
            <tx:method name="*" propagation="REQUIRED" timeout="-1"/>
        tx:attributes>
    tx:advice>

    
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.kgc.ssm.service.*.*(..))"/>
    aop:config>
beans>

(5)创建applicationContext-mvc.xml文件来配置spring mvc 文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:component-scan base-package="cn.kgc"/>

    
    

    
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="suffix" value=".jsp"/>
                    <property name="prefix" value="/WEB-INF/jsp/"/>
                    
                    <property name="order" value="2"/>
                bean>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    
                    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                    
                    <property name="prefix" value="/statics/html/"/>
                    
                    <property name="suffix" value=".html"/>
                    
                    <property name="viewNames" value="html*"/>
                    
                    <property name="order" value="1"/>
                bean>
            list>
        property>
    bean>

    
    <mvc:resources mapping="/statics/**" location="/statics/"/>
    
    <mvc:annotation-driven>
        
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        
                        <value>application/json;charset=UTF-8value>
                        <value>application/xml;charset=UTF-8value>
                        <value>text/html;charset=UTF-8value>
                    list>
                property>
            bean>
        mvc:message-converters>
    mvc:annotation-driven>
        
    
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                
                <prop key="java.lang.RuntimeException">errorprop>
            props>
        property>
    bean>

    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        
        <property name="maxUploadSize" value="1024000000"/>
        
        <property name="defaultEncoding" value="UTF-8"/>
    bean>
beans>

(6)修改web.xml文件,添加spring mvc 配置

web.xml的文件中如果添加spring mvc 配置,需要修改表头


<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>Archetype Created Web Applicationdisplay-name>

  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath*:applicationContext.xmlparam-value>
  context-param>

  <servlet>
    <servlet-name>springmvcservlet-name>
    
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath*:applicationContext-mvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

  
  <filter>
    <filter-name>encodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
    <init-param>
      <param-name>forceEncodingparam-name>
      <param-value>trueparam-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>encodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
web-app>

之后你就可以愉快的编写逻辑代码了!!!

你可能感兴趣的:(ssm框架,后端Java,java)