从零开始搭建纯净的SSM框架

  • Spring
      Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。
      Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地new一个对象,而是让Spring框架帮你来完成这一切。
  • SpringMVC
      SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
  • mybatis
      mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
    页面发送请求给控制器,控制器调用业务层处理逻辑,逻辑层向持久层发送请求,持久层与数据库交互,后将结果返回给业务层,业务层将处理逻辑发送给控制器,控制器再调用视图展现数据。
1.新建maven项目
2.导入相关依赖
		<!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--数据库依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--数据库连接池: c3p0-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <!--servlet-jsp-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--日志 start-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>

静态资源导出

<!--静态资源导出问题-->
    <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>
    </build>
3.创建xml文件
  • 1.applicationContext.xml
  • 2.database.properties
  • 3.mybatis-config.xml
  • 4.spring-mapper.xml
  • 5.spring-mvc.xml
  • 6.spring-service.xml
  • 7.web.xml

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
	
	<!--整合所有配置文件-->
    <import resource="classpath:spring-mapper.xml"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="classpath:spring-mvc.xml"/>
</beans>

数据库连接配置database.properties

jdbc.driver=com.mysql.jdbc.Driver
##如果使用的是MySQL8.0+,增加时区的配置 &serverTimeZone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/blog?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dts/mybatis-3-config.dtd">
<configuration>
	<!--配置数据源,交给Spring-->
	<!--实体类取别名-->
    <typeAliases>
        <package name="com.cp.pojo"/>
    </typeAliases>
</configuration>

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

    <!--1.关联数据库配置文件-->
    <context:property-placeholder location="classpath:database.properties"/>

    <!--2.连接池
        dbcp:半自动化操作,不能自动连接
        c3p0:自动化操作(自动化的加载配置文件,并且可以自动映射到对象中)
        druid : hikari
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!--c3p0连接池的私有属性-->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!--关闭连接后不自动commit-->
        <property name="autoCommitOnClose" value="false"/>
        <!--获取连接超时时间-->
        <property name="checkoutTimeout" value="10000"/>
        <!--当连接失败重试数-->
        <property name="acquireIncrement" value="3"/>
    </bean>

    <!--3.sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--配置dao接口扫描包,动态实现了mapper接口注入到Spring中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--要扫描的包-->
        <property name="basePackage" value="com.cp.mapper"/>
    </bean>
</beans>

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: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-4.3.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--1.注解驱动-->
    <mvc:annotation-driven/>
    <!--2.静态资源过滤-->
    <mvc:default-servlet-handler/>
    <!--3.扫描包:controller-->
    <context:component-scan base-package="com.cp.controller"/>

    <!--4.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

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


    <!--扫描service下的包-->
    <context:component-scan base-package="com.cp.service"/>

    <!--将所有的业务类注入到Spring,可以通过配置或注解实现-->
    <bean id="blogService" class="com.cp.service.impl.BlogServiceImpl">
        <property name="blogMapper" ref="blogMapper"/>
    </bean>

    <!--3.声明式事务-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--声明service层可以使用@Transaction注解,指定txManager事务-->
    <tx:annotation-driven transaction-manager="txManager"/>
    <!--4.aop横切事务-->
    <!-- 切面类 -->
    <bean id="logger" class="com.cp.log.Log"></bean>

    <!-- 切面配置 -->
    <aop:config>
        <!-- 切面 -->
        <aop:aspect ref="logger">
           <!-- 切入点 -->
            <!-- 指定方法、返回值、参数个数和类型 -->
            <!-- <aop:pointcut expression="execution(public void com.cp.service.impl.UserServiceImpl.save(com.fengqing.bean.User))" id="savePointcut"/> -->
            <!-- 指定方法、返回值、参数个数,类型任意 -->
            <!-- <aop:pointcut expression="execution(public void com.cp.service.impl.UserServiceImpl.save(*))" id="savePointcut"/> -->
            <!-- 指定方法、返回值,参数个数和类型任意 -->
            <!-- <aop:pointcut expression="execution(public void com.cp.service.impl.UserServiceImpl.save(..))" id="savePointcut"/> -->
            <!-- 指定返回值,方法、参数个数和类型任意 -->
            <!-- <aop:pointcut expression="execution(public void com.cp.service.impl.UserServiceImpl.*(..))" id="savePointcut"/> -->
            <!-- 指定返回值,impl下的类及其子包的类里的所有方法 -->
            <!-- <aop:pointcut expression="execution(public void com.cp.service.impl.*.*(..))" id="savePointcut"/> -->
            <!-- 指定返回值,service下的类及其子包的类里的所有方法 -->
            <!-- <aop:pointcut expression="execution(public void com.cp.service..*.*(..))" id="savePointcut"/> -->
            <!-- service下的类及其子包的类里的所有方法,所有返回类型 -->
            <aop:pointcut expression="execution(public * com.cp.service..*.*(..))" id="savePointcut"/>

            <!-- 增强 -->
            <!-- 前置增强 -->
            <!-- <aop:before method="before" pointcut-ref="savePointcut"/> -->
            <!-- 后置增强 -->
            <!-- <aop:after-returning method="afterReturning" pointcut-ref="savePointcut"/> -->
            <!-- 异常增强 -->
            <!-- <aop:after-throwing method="afterThrowring" pointcut-ref="savePointcut"/> -->
            <!-- 最终增强 -->
            <!-- <aop:after method="after" pointcut-ref="savePointcut"/> -->
            <!-- 环绕增强 -->
            <aop:around method="around" pointcut-ref="savePointcut"/>
        </aop:aspect>
    </aop:config>

</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_4_0.xsd"
         version="4.0">

    <!--DispatchServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--乱码过滤-->
    <filter>
        <filter-name>characterEncodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--Session-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

    <!-- 首页 -->
    <!--<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>-->
</web-app>
4.AOP示例
import org.aspectj.lang.ProceedingJoinPoint;

public class Log {
    public void before(){
        System.out.println("一个用户被保存了--before");
    }

    public void afterReturning(){
        System.out.println("一个用户被保存了--afterReturning");
    }

    public void afterThrowring(){
        System.out.println("一个用户未被保存了--afterThrowring");
    }

    public void after(){
        System.out.println("一个用户被保存了--after");
    }

    public void around(ProceedingJoinPoint pjp){
        long start = System.currentTimeMillis();
        System.out.println("before");
        try {
            pjp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("after");
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms");
    }
}

至此SSM的框架搭建完毕,接下来只需要写业务逻辑即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值