1. 程式人生 > >ssm框架搭建

ssm框架搭建

mpm efault depend dtd state mysql- scott poi delete

技術分享圖片2019-01-03技術分享圖片技術分享圖片

第一步

準備環境:idea,maven,jdk,tomcat,mysql,oracle

以上軟件的安裝配置不再贅述。

第二步

創建項目,添加maven管理jar包

引入jar包到pom文件

由於oralce連接數據庫的驅動jar包maven沒有,需要手動引入

pom文件的jar:

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9
.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.8.RELEASE</version> </dependency> <!-- https://
mvnrepository.com/artifact/org.aspectj/aspectjrt --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0
</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.10.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.0.6</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.oracle/ojdbc6 --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.1.0.6.0</version> <scope>system</scope> <systemPath>${basedir}/web/WEB-INF/lib/ojdbc6-11.1.0.6.0.jar</systemPath> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency>

第三步

搭建項目框架

(導入web支持,spring支持和mybatis支持會自動導入)

技術分享圖片

技術分享圖片

第四步

配置相關配置文件

配置文件:

applicationContent.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:aop="http://www.springframework.org/schema/aop"

   xmlns:context="http://www.springframework.org/schema/context"

   xmlns:tx="http://www.springframework.org/schema/tx"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans.xsd

   http://www.springframework.org/schema/aop

   http://www.springframework.org/schema/aop/spring-aop.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">

<!--開啟註解掃描功能-->
<context:annotation-config></context:annotation-config>
<!--定義註解掃描的包-->
<context:component-scan base-package="com.qy.mapper"></context:component-scan>
<context:component-scan base-package="com.qy.service"></context:component-scan>
<context:component-scan base-package="com.qy.domain"></context:component-scan>
<!--開啟aop自動代理模式-->
<aop:aspectj-autoproxy proxy-target-class="true"/>

<!--從上下文環境中讀取db.properties配置文件-->
<context:property-placeholder location="classpath*:db.properties"></context:property-placeholder>

<bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${oracle.driverClassName}"></property>
    <property name="url" value="${oracle.url}"></property>
    <property name="username" value="${oracle.username}"></property>
    <property name="password" value="${oracle.password}"></property>
    <property name="maxActive" value="${oracle.maxActive}"></property>
    <property name="initialSize" value="${oracle.initialSize}"></property>
</bean>

<!--通過mybatis和spring結合的類,創建sqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!--指定myabtis配置文件位置-->
    <property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- scan for mappers and let them be autowired
MapperScannerConfigurer Mybatis-Spring 會自動為我們註冊Mapper對應的MapperFactoryBean對象-->
<!-- Mapper接口所在包名,Spring會自動查找其下的Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.qy.mapper" />
</bean>
<!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
<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="update*"  propagation="REQUIRED"/>
        <tx:method name="delete*"  propagation="REQUIRED"/>
        <tx:method name="*" propagation="NOT_SUPPORTED"></tx:method>
    </tx:attributes>
</tx:advice>

<!-- 配置事務切入點 -->
<aop:config>
    <aop:pointcut expression="execution(* com.qy.service.impl.*.*(..))" id="pointCut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>
</beans>

db.properties 數據庫配置文件

mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/java1807
mysql.username=root
mysql.password=123456
mysql.maxActive=100
mysql.initialSize=5

oracle.driverClassName=oracle.jdbc.OracleDriver
oracle.url=jdbc:oracle:thin:@localhost:1521:orcl
oracle.username=scott
oracle.password=tiger
oracle.maxActive=100
oracle.initialSize=5

log4j2.xml 日誌配置文件

(需要在mybatis-config中配置setting)

<!--使用log4j2 配置輸出-->
<setting name="logImpl" value="LOG4J2"/>
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="Console"/>
        </Root>
        <Logger name="com.qy.mapper" level="trace" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>

    </Loggers>
</Configuration>

mybatis-config.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>

    <settings>
        <!--是否使用緩存 開發中禁用-->
        <setting name="cacheEnabled" value="false"/>
        <!--下劃線和駝峰式命名法的匹配  -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--使用啟用懶加載機制  true啟用-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="multipleResultSetsEnabled" value="true"/>
        <setting name="useColumnLabel" value="true"/>
        <setting name="useGeneratedKeys" value="true"/>
        <setting name="autoMappingBehavior" value="PARTIAL"/>
        <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
        <setting name="defaultExecutorType" value="SIMPLE"/>
        <setting name="defaultStatementTimeout" value="25"/>
        <setting name="defaultFetchSize" value="100"/>
        <setting name="safeRowBoundsEnabled" value="false"/>
        <setting name="localCacheScope" value="SESSION"/>
        <setting name="jdbcTypeForNull" value="OTHER"/>
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
    </settings>

    <typeAliases>
        <!--給類起別名-->
        <!--<typeAlias type="com.qy.domain.Question" alias="Question"></typeAlias>-->
        <!--給包中所有的類起別名 默認名字為類名 -->
        <package name="com.qy.domain"></package>
    </typeAliases>

    <mappers>
        <!--引入mapper映射文件的位置-->
       <!-- <mapper resource="mapper/UserMapper.xml"/>
        <mapper resource="com/qy/mapper/QuestionMapper.xml"/>
        <mapper resource="com/qy/mapper/DeptMapper.xml"/>
        <mapper resource="com/qy/mapper/EmpMapper.xml"/>-->
    </mappers>


</configuration>

springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

    <!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--指定視圖的前綴和後綴,Controller返回的String類型與這裏的前後綴拼接,構成返回的視圖頁面地址-->
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--開啟註解掃描功能-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 處理請求返回json字符串的中文亂碼問題 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- 解決Controller返回json中文亂碼問題 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <!-- <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> -->
                <!-- <property name="supportedMediaTypes" value="application/json;charset=UTF-8" > -->
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>

            <!-- fastJson配置 -->
            <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/json;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>

        </mvc:message-converters>
    </mvc:annotation-driven>


    <!--定義註解掃描的包-->
    <context:component-scan base-package="com.qy.controller"></context:component-scan>

    <!--處理靜態資源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

    <!--將鏈接中的靜態的訪問路徑映射為URL,常用於加載html、js、css、圖片、視頻等靜態資源-->
   <!-- <mvc:resources mapping="/js/**" location="/background/js/"/>
    <mvc:resources mapping="/images/**" location="/background/images/"/>
    <mvc:resources mapping="/css/**" location="/background/css/"/>
    <mvc:resources mapping="/fonts/**" location="/background/fonts/"/>
    <mvc:resources mapping="/lib/**" location="/background/lib/"/>-->

    <!--登錄攔截器-->
  <!--  <mvc:interceptors>
        <mvc:interceptor>
            &lt;!&ndash; 攔截所有URL中包含/user/的請求 &ndash;&gt;
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/**/fonts/**"/>
            <mvc:exclude-mapping path="/**/css/**"/>
            <mvc:exclude-mapping path="/**/js/**"/>
            <mvc:exclude-mapping path="/**/images/**"/>
            <mvc:exclude-mapping path="/**/lib/**"/>
            <mvc:exclude-mapping path="/**/login.html"/>
            <mvc:exclude-mapping path="/user/toLogin"/>
            <bean class="com.qy.controller.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>-->

    <!--springmvc定義好的,用來處理上傳文件的類-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 設置上傳文件的最大尺寸為1MB -->
        <property name="maxUploadSize">
            <value>1048576</value>
        </property>
    </bean>

 </beans>

web.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!--filter 過濾器 用來解決中文亂碼問題-->
    <filter>
        <filter-name>characterEncoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>default1</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>default1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--配置spring-->
    <!-- 配置去哪裡加載spring的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- 配置spring的監聽器,加載資源初始化文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

第五步

java目錄下的mapper層代替了原來的Dao層(寫接口)

原來dao層實現類變成了resources目錄下相應級目錄的***Mapper.xml

Mapper.xml裏面寫mybatis特有的crue(sql)語句。

大功告成!

ssm框架搭建