通过Maven简单搭建SSM框架

创建Maven就不用多说了,下面直接看Pom.xml里面的依赖吧

 <properties>
        <!-- spring版本号 -->
        <spring.version>5.0.0.RELEASE</spring.version>
        <!-- 因为使用了Spring5,如果jackson版本过低会报错 -->
        <jackson.version>2.9.3</jackson.version>
    </properties>
    <dependencies>
        <!-- spring核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

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

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- AOP begin -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.1</version>
        </dependency>
        <!-- AOP end -->

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- 添加MyBatis依赖 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.8</version>
        </dependency>
        <!-- 阿里巴巴的数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.16</version>
        </dependency>
        <!-- spring结成mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- jsp标准标签库 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- 日志 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.6.4</version>
        </dependency>
        <!-- Junit测试工具 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
        <!-- jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <!-- servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <!-- provided表明该包只在编译和测试的时候用 -->
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <!-- 配置以下信息,可以避免mapper.xml加载不成功 -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <!-- tomcat启动插件 -->
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <uriEncoding>UTF-8</uriEncoding>
                    <path>/</path>
                    <port>8</port>
                </configuration>
            </plugin>
        </plugins>
    </build>

配置文件
web.xml

<!-- 配置启动记载spring配置文件 -->
 2     <context-param>
 3         <param-name>contextConfigLocation</param-name>
 4         <param-value>classpath:applicationContext.xml</param-value>
 5     </context-param>
 6     <!-- 配置监听器加载spring -->
 7     <listener>
 8         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 9     </listener>
10 
11     <!-- UTF-8编码的Filter -->
12     <filter>
13         <filter-name>encodingFilter</filter-name>
14         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
15         <init-param>
16             <param-name>encoding</param-name>
17             <param-value>UTF-8</param-value>
18         </init-param>
19         <init-param>
20             <param-name>forceEncoding</param-name>
21             <param-value>true</param-value>
22         </init-param>
23     </filter>
24     <filter-mapping>
25         <filter-name>encodingFilter</filter-name>
26         <url-pattern>/*</url-pattern>
27     </filter-mapping>
28     <!-- 配置SpringMVC -->
29     <servlet>
30         <servlet-name>ssm</servlet-name>
31         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
32         <init-param>
33             <param-name>contextConfigLocation</param-name>
34             <param-value>classpath:Springmvc.xml</param-value>
35         </init-param>
36         <!-- 配置springmvc什么时候启动,参数必须为整数 -->
37         <!-- 如果为0或者大于0,则springMVC随着容器启动而启动 -->
38         <!-- 如果小于0,则在第一次请求进来的时候启动 -->
39         <load-on-startup>1</load-on-startup>
40     </servlet>
41     <servlet-mapping>
42         <servlet-name>ssm</servlet-name>
43         <!-- 所有的请求都进入springMVC -->
44         <url-pattern>/</url-pattern>
45     </servlet-mapping>

applicationContext.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" xmlns:context="http://www.springframework.org/schema/context"
  4     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
  5     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
  6     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
  7     xsi:schemaLocation="
  8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 10         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
 11         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
 12         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
 13         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 14         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 15         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
 16     <!-- 扫描注解 -->
 17     <context:component-scan base-package="com.yk" />
 18 
 19     <!-- 配置 读取properties文件 jdbc.properties -->
 20     <context:property-placeholder location="classpath:jdbc.properties" />
 21     <!-- 配置 数据源 -->
 22     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
 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         <property name="validationQuery" value="SELECT 1" />
 28         <!-- 初始化大小 -->
 29         <property name="initialSize" value="0" />
 30         <!-- 连接池最小空闲 -->
 31         <property name="minIdle" value="0" />
 32         <!-- 连接池最大使用连接数量 -->
 33         <property name="maxActive" value="20" />
 34         <!-- 获取连接最大等待时间 -->
 35         <property name="maxWait" value="60000" />
 36         <!-- 获取连接时测试是否有效 -->
 37         <property name="testOnBorrow" value="false" />
 38         <!-- 归还连接时是否测试有效 -->
 39         <property name="testOnReturn" value="false" />
 40         <!-- 连接空闲时测试是否有效 -->
 41         <property name="testWhileIdle" value="true" />
 42         <!-- 逐出连接的检测时间间隔(主要用于检测空闲的连接) -->
 43         <property name="timeBetweenEvictionRunsMillis" value="60000" />
 44         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒,最小逐出时间 -->
 45         <property name="minEvictableIdleTimeMillis" value="25200000" />
 46         <!-- 打开关闭“赃”连接(调用过getVendorConnection方法)功能 -->
 47         <property name="removeAbandoned" value="true" />
 48         <!-- 连接泄漏的超时时间 -->
 49         <property name="removeAbandonedTimeout" value="1800" />
 50         <!-- 关闭泄漏的连接时打印堆栈信息 -->
 51         <property name="logAbandoned" value="true" />
 52         <!-- 监控数据库 -->
 53         <!-- <property name="filters" value="stat" /> -->
 54         <property name="filters" value="mergeStat" />
 55     </bean>
 56     <!-- sessionFactory 将spring和mybatis整合 Spring与Mybatis相关的配置文件 typeAliasesPackage:可以配置多个,用','号分隔 
 57         basePackage:可以配置多个,用','号分隔 -->
 58     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 59         <property name="dataSource" ref="dataSource" />
 60         <!-- 加载mapper配置文件 -->
 61         <property name="mapperLocations" value="classpath*:mapper/*.xml" />
 62         <!-- 别名 -->
 63         <property name="typeAliasesPackage" value="com.yk.entity" />
 64         <!-- 加载Mybatis配置文件 -->
 65         <property name="configLocation" value="classpath:/mybatis-config.xml" />
 66     </bean>
 67     <!-- 配置扫描器 -->
 68     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 69         <property name="basePackage" value="com.yk.dao" />
 70         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 71     </bean>
 72 
 73     <!-- 配置事物 -->
 74     <!-- 事务管理器 -->
 75     <bean id="transactionManager"
 76         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 77         <!-- 数据源 -->
 78         <property name="dataSource" ref="dataSource" />
 79     </bean>
 80     <!-- 切面 -->
 81     <aop:config>
 82         <aop:advisor advice-ref="txAdvice"
 83             pointcut="execution(* com.yk.service.*.*(..))" />
 84     </aop:config>
 85     <!-- 通知 -->
 86     <tx:advice id="txAdvice" transaction-manager="transactionManager">
 87         <tx:attributes>
 88             <!-- 传播行为 -->
 89             <tx:method name="save*" propagation="REQUIRED" />
 90             <tx:method name="insert*" propagation="REQUIRED" />
 91             <tx:method name="add*" propagation="REQUIRED" />
 92             <tx:method name="create*" propagation="REQUIRED" />
 93             <tx:method name="delete*" propagation="REQUIRED" />
 94             <tx:method name="update*" propagation="REQUIRED" />
 95             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
 96             <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
 97             <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
 98             <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
 99             <tx:method name="*" propagation="SUPPORTS" read-only="true" />
100         </tx:attributes>
101     </tx:advice>
102 
103 
104 </beans>

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

log4j.properties

 ### 设置###
 2 log4j.rootLogger = DEBUG,stdout,D,E
 3 
 4 ### 输出信息到控制抬 ###
 5 log4j.appender.stdout = org.apache.log4j.ConsoleAppender
 6 log4j.appender.stdout.Target = System.out
 7 log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
 8 log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
 9 
10 ### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
11 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
12 log4j.appender.D.File = E://logs/log.log
13 log4j.appender.D.MaxFileSize = 10MB
14 log4j.appender.D.Threshold = ALL
15 log4j.appender.D.Append = true
16 log4j.appender.D.Threshold = DEBUG 
17 log4j.appender.D.layout = org.apache.log4j.PatternLayout
18 log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
19 
20 ### 输出ERROR 级别以上的日志到=E://logs/error.log ###
21 log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
22 log4j.appender.E.File =E://logs/error.log 
23 log4j.appender.D.MaxFileSize = 10MB
24 log4j.appender.D.Threshold = ALL
25 log4j.appender.E.Append = true
26 log4j.appender.E.Threshold = ERROR 
27 log4j.appender.E.layout = org.apache.log4j.PatternLayout
28 log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
29 
30 ###显示SQL语句部分  
31 log4j.logger.com.mybatis=DEBUG  
32 log4j.logger.com.mybatis.common.jdbc.SimpleDataSource=DEBUG  
33 log4j.logger.com.mybatis.common.jdbc.ScriptRunner=DEBUG  
34 log4j.logger.com.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG  
35 log4j.logger.java.sql.Connection=DEBUG  
36 log4j.logger.java.sql.Statement=DEBUG  
37 log4j.logger.java.sql.PreparedStatement=DEBUG

Springmvc.xml

<?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" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 9     <!-- 配置Controller扫描 -->
10     <context:component-scan base-package="com.yk.controller" />
11 
12     <!-- 配置注解驱动 -->
13     <mvc:annotation-driven />
14 
15     <!-- 配置视图解析器 -->
16     <bean
17         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
18         <!-- 前缀 -->
19         <property name="prefix" value="/WEB-INF/" />
20         <!-- 后缀 -->
21         <property name="suffix" value=".jsp" />
22     </bean>
23 
24     <!-- 静态资源映射 -->
25     <!-- <mvc:resources mapping="/static/**" location="/static/" cache-period="31556926" 
26         /> -->
27 
28 </beans>

mybatis-config.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
3 <configuration>
4     <settings>
5     <!-- 打印sql -->
6         <setting name="logImpl" value="STDOUT_LOGGING" />
7     </settings>
8 </configuration>

项目右键→Run As → maven build..


自此一个简单的SSM框架搭建完成,第一次写博客,也主要是为了记录自己在学习中遇到的问题,可以学习到的东西,方便自己以后回顾

posted @ 2020-04-07 08:09  无话可说丶  阅读(183)  评论(0编辑  收藏  举报