当前位置: 移动技术网 > IT编程>开发语言>Java > 手把手教你整合SSM框架(基于课工厂+MyEclipse 2017 CI 10)

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

2019年04月26日  | 移动技术网IT编程  | 我要评论

我们都爱笑20140412,生殖器疱疹 治疗,虚与委蛇的意思

整合思路:因为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显示查询结果

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网