SSM 的整合使用

SSM 的基础与使用总算是囫囵吞枣的学完了,跟的是 江南一点雨 的教程(博客、视频),框架这种侧重于练习的东西肯定是一天不用生疏一点,所以这里对 SSM 的整合使用做一个记录,将来忘了再过来找找感觉

阅读原文

使用 Maven 进行依赖的配置,主要包括:

spring-webmvc:包含 Spring
mybatis
mybatis-spring:Spring 控制 MyBatis
mysql-connector-java
jackson-databind:将查询到的数据使用 JSON 的形式返回
spring-jdbc:正常用线程池,这里为了操作简单

  1. 在 pom.xml 中加入 <packaging>war</packaging> 手动制造一个 web 项目(Maven 模板用不惯)
  2. 创建配置文件,如:Spring 的 applicationContext.xml、SpringMVC 的 spring-servlet.xml、数据库连接配置文件 db.properties
  3. 对 web.xml 进行最基础的配置,自动扫描 applicationContext.xml 信息、servlet 的配置、UTF-8 编码过滤器解决中文乱码
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<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:spring-servlet.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>encodingFilter</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>forceRequestEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>forceResponseEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
  1. 配置各文件信息

db.properties 中主要配置,username、password、url、drive
applicationContext.xml 中配置包扫描、数据库信息写入、SqlSessionFactoryBean、MapperScannerConfigurer

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

<context:component-scan base-package="top.aidan">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
    <property name="username" value="${db.username}"/>
    <property name="password" value="${db.password}"/>
    <property name="url" value="${db.url}"/>
    <property name="driverClassName" value="${db.drive}"/>
</bean>

<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="top.aidan.model"/>
    <property name="mapperLocations">
        <list>
            <value>classpath*:top/aidan/mapper/*.xml</value>
        </list>
    </property>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
    <property name="basePackage" value="top.aidan.mapper"/>
</bean>

spring-servlet.xml 中配置对 Controller 的包扫描

<context:component-scan base-package="top.aidan" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven/>

最后编写 model 类和 Mapper 配置,如果 Mapper 放在 Java 包下,需要在 pom.xml 中进行 Build 的配置,然后测试

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>

源码地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值