SSM框架整合(MyEclipse 2017 CI 10)

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

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

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

SSM框架整合(MyEclipse 2017 CI 10)_第1张图片

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

SSM框架整合(MyEclipse 2017 CI 10)_第2张图片

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,用以存放相关的配置源文件

SSM框架整合(MyEclipse 2017 CI 10)_第3张图片

 

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

1.建立数据源配置文件

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

SSM框架整合(MyEclipse 2017 CI 10)_第4张图片

 

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

driver=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.properties"/>

    
    <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>
    
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    bean>
    
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.xin.dao" />
    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_SSMdisplay-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>
  
  
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc-servlet.xmlparam-value>
        init-param>

        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

    
    <filter>
        <filter-name>CharacterEncodingfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>CharacterEncodingfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
  
web-app>

2.在resource文件夹下建立springmvc-servlet.xml,并配置相关信息:

SSM框架整合(MyEclipse 2017 CI 10)_第5张图片

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并配置相关信息:

SSM框架整合(MyEclipse 2017 CI 10)_第6张图片

 

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.properties文件:

# 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显示查询结果

或者可以直接访问index.jsp,访问的到说明ssm整合基本成功

 

你可能感兴趣的:(SSM框架整合(MyEclipse 2017 CI 10))