服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java开发之ssm三大框架整合

Java开发之ssm三大框架整合

2022-12-15 15:37仰望星空的快乐 Java教程

SSM框架是spring MVC ,spring和mybatis框架的整合,是标准的MVC模式,将整个系统划分为表现层,controller层,service层,DAO层四层,使用spring MVC负责请求的转发和视图管理,spring实现业务对象管理,mybatis作为数据对象的持久化引擎

1.springmvc

和只有spring-mvc时一样,web.xml spring-mvc.xml

spring-mvc.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--    注解驱动-->
    <mvc:annotation-driven/>
<!--    静态资源过滤-->
<!--    开启jsp专用的视图控制器 internalresourceresoler-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--    设置前缀-->
        <property name="prefix" value="/WEB-INF/templates/"></property>
        <!--    设置后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
<!--    扫描 controller注解-->
    <context:component-scan base-package="com.hxut.rj1192.zyk.Controller"></context:component-scan>
</beans>

web.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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">
<!--    设置拦截器,解决参数乱码,一定要在设置HiddenHttpMethodFilter请求前,要在其他拦截器和servlet执行前设置编码-->
    <filter>
        <filter-name>paramencoding</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>
<!--        解决返回的请求数乱码  response-->
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
<!--拦截所有页面-->
    <filter-mapping>
        <filter-name>paramencoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
<!--servlet 将所有除了jsp的页面拦截,交给dispatcherservlet视图控制器,并设置dispatcherservlet的xml文件的位置-->
    <servlet-mapping>
        <servlet-name>all</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>all</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:Spring-springmvc.xml</param-value>
        </init-param>
    </servlet>
<!-- 拦截所有请求,并交给hiddenhttpmethodfilter 检测否是post请求,且_method不为空,如果是,就将请求类型改为_method的值-->
    <filter>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

2.spring-dao.xml与mybatis-config.xml

主要就是spring整合mybatis

spring整合mybatis

在上面的基础上,去掉成接口的实现类了,需要配置dao接口扫描包,我的理解是这个dao接口扫描包中有datasource,有mapper的扫描范围, 它会自动生成这些接口对应的mapper,并将接口的mapper放到xml文件中,所以在spring-service中,直接

<property name="bookmapper" ref="bookmapper"></property>

引用即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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.xsd
      http://www.springframework.org/schema/context
       https://www.springframwork.org/schema/context/spring-context.xsd">
    <!-- 读取数据库配置文件-->
    <context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
    <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}"/>
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>
    <bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
<!--配置dao接口扫描包 ,动态的实现了dao接口可以注入到spring容器中
  就是用来代替BookMapperImpl类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--        注入sqlsessionfactory-->
<!--个人理解,这个dao接口扫描包中有datasource,有mapper的扫描范围,
它会自动生成这些接口对应的mapper,并将接口的mapper放到xml文件中,所以在spring-service中,直接
   <property name="bookmapper" ref="bookmapper"></property> 引用即可-->
        <property name="sqlSessionFactoryBeanName" value="sqlsessionFactory"></property>
<!--        要扫描的dao包, 会自动生成包下的类的接口的实现类-->
        <property name="basePackage" value="com.hxut.rj1192.zyk"></property>
    </bean>
</beans>

mybatis-config.xml 详细在上面的mybatis整合spring的文章中,它做两件事,配置映射文件路径,配置接口扫描范围,它被import到 spring-dao.xml中。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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>
<!--    配置数据源交给spring了-->
    <!--    给类起别名-->
    <typeAliases>
        <package name="com.hxut.rj1192.zyk.mapper"/>
    </typeAliases>
    <!--    设置映射文件路径-->
    <mappers>
        <mapper resource="com/hxut/rj1192/zyk/mapper/Bookmapper.xml"></mapper>
    </mappers>
</configuration>

3.spring-service.xml

在这个文件中要进行事务的处理(事务本来就应该是在service层),要将service层的类全部放到ioc容器中,然后这些类中因为调用了dao层的类,然后因为刚才第二部配置了接口扫描包,直接ref获取mapper即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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.xsd
      http://www.springframework.org/schema/context
       https://www.springframwork.org/schema/context/spring-context.xsd">
<!--    开启注解驱动-->
    <context:component-scan base-package="com.hxut.rj1192.zyk.service"></context:component-scan>
    <bean id="booksServiceimpl" class="com.hxut.rj1192.zyk.service.BooksServiceimpl">
        <property name="bookmapper" ref="bookmapper"></property>
    </bean>
<!--    声明式事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--       注入数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

4.引用

将这些文件的引用放到一个大的xml文件中,这个文件只放引用,这样就很容易看

?
1
2
3
4
5
6
7
8
9
10
<?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.xsd">
    <import resource="applicationContext.xml"></import>
    <import resource="spring-dao.xml"></import>
    <import resource="Spring-Service.xml"></import>
    <import resource="Spring-springmvc.xml"></import>
</beans>

或者在project structure中设置 spring application context,效果是一样的

Java开发之ssm三大框架整合

到此这篇关于Java开发之ssm三大框架整合的文章就介绍到这了,更多相关Java ssm框架整合内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/sharesb/article/details/124789221

延伸 · 阅读

精彩推荐