手把手教你整合SSM框架(基于课工厂+MyEclipse 2017 CI 10)

整合思路:因为spring和spring mvc同源,可以无缝整合,故先整合spring+mybatis,然后配置web.xml、spring mvc、log4j等相关配置文件 步骤1:myeclipse创建项目,导入spring框架 右击项目名&n...

整合思路:因为spring和spring mvc同源,可以无缝整合,故先整合spring+mybatis,然后配置web.xml、spring mvc、log4j等相关配置文件

步骤1:myeclipse创建项目,导入spring框架

右击项目名  ——  Configure Facets  ——  Install Spring Facet

一路Next到Configuer Project Libraries后,勾选Spring Persistence,然后Finsh

Finsh后会自动在src下创建applicationContext.xml文件,该文件为spring框架的配置文件

步骤2:开始整合Spring+Mybatis

首先导入整合所需要的jar包:mybatis-3.4.6.jar、mybatis-spring-1.3.1.jar、mysql-connector-java-5.1.20-bin.jar(版本自行选择),并建立Source Folder文件夹命名为resource,用以存放相关的配置源文件

建立好Source Folder文件夹后,把spring的配置文件applicationContext拖到此文件夹中,进行统一管理

1.建立数据源配置文件

建立file文件,并命名为database.properties,建立完成后,双击进入选择Source开始编写数据库的相关配置属性

注意:相关参数要根据自己的情况进行更改

database.propertidriver=com.mysql.jdbc.Driver
url
=jdbc:mysql://localhost:3306/your database username=root password=your passwrod

 2.在applicationContext.xml中进行spring+mybatis的整合:

同样,配置信息里的相关包名也要根据自己的情况进行更改,下同

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

    <!-- 读取数据库配置文件 -->
    <context:property-placeholder location="classpath:database.propertise"/>

    <!-- 获取数据源(使用dbcp连接池) -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close" scope="singleton">
            
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password} " />          
    </bean>
    
    <!-- 配置Mybatis的SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 引用数据源组件 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 引用MyBatis配置文件中的配置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    
    <!-- 配置MyBatis的MapperScannerConfigurer,自动生成Mapper的对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.xin.dao" />
    </bean>
    
    <!-- 配置service,扫描注解定义的业务Bean -->
    <context:component-scan base-package="cn.xin.service" />
    
</beans>

 步骤3:进行Spring MVC和一些相关文件的配置

1.在web.xml中配置DispatcherServlet和字符编码过滤器:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Test_SSM</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- springmvc拦截请求 -->
    <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:springmvc-servlet.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>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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
</web-app>

2.在resource文件夹下建立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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        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-4.1.xsd">

    <!-- 扫描带注解的包 -->
    <context:component-scan base-package="cn.xin.controller" />

    <!-- 引入静态资源 -->
    <mvc:annotation-driven />
    <mvc:default-servlet-handler />

    <!-- 配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

3.在resource文件夹下,建立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>

    <typeAliases>
        <package name="cn.xin.pojo" />
    </typeAliases>
        
</configuration>

4.建立并编写log4j.propertise文件:

# Global loggin configuration
log4j.rootLogger=DEBUG,stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

至此,SSM框架的整合已经基本完成,下面进行编写测试代码

步骤4:进行SSM整合的测试 

测试思路:jsp+a标签携带参数  ——>  controller  ——>  service  ——>  dao  ——>  数据库  ——>  jsp显示查询结果

  • 发表于 2019-04-26 00:00
  • 阅读 ( 514 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除