SSM框架整合环境搭建

news/2024/7/6 13:40:35 标签: mybatis, spring, mvc

一、前言 (摘自博客作者“Roobtyan”点击查看原文)

在这个小的web项目中,大的目录分成了java、resources、webapp三部分。

  • java中有bean,这个目录是存放和数据库对应的基本类的;controller,这里面是存放控制器类的,这就类似于之前Servlet的作用;dao,这是存放数据库映射文件的,功能和之前的JDBC相同,最后是service,服务层,controller负责处理逻辑操作,service负责的是和dao层交互,实现具体的功能,比如插入一条数据之类的。
  • resources,资源路径,多数的配置文件和Mybatis的映射文件都放在这里,具体的作用在后面就会见分晓了。
  • webapp,这里面的内容和之前的开发一样,只是多出来一个SpringMVC的配置文件

知道了上面目录结构的功能,就能看下面这张结构图了:
在这里插入图片描述
对象调用的过程
前端JSP ——> Controller ——> Service ——> Dao ——> 数据库

我们将整体的结构分成了五个部分,分别是前端页面、Spring容器、数据库连接池、数据库(这两部分其实可以合成一部分)、Maven依赖管理。
完成一个web请求的个过程大致如下:

  • 前端页面发起一个http请求:localhost:8080/user
  • http请求被Tomcat服务器得到,接着到Servlet中寻找映射路径,当然有了SpringMVC就无需去执行复杂的配置了,我们可以像Servlet3.0那样使用注解开发,SpringMVC前端控制器的注解是@RequestMapping("/user"),通过请求的url,寻找映射路径,找到对应Controller类的方法
  • 在Controller中,注入了诸多的Service,Controller可以直接调用这些service进行操作
  • controller调用Service后,Service就要执行对应的方法,在Service中同样注入了dao层的mapper,即可调用相应的mapper方法执行数据库操作
  • mybatis中,dao层分成两部分,分别是接口和mapper映射文件,调用mapper接口方法的时候,就会去找到对应方法的映射,这些映射就是执行数据库操作的语句,本质上还是sql语句
  • 此项目肯定是有很多的依赖文件的,比如springframwork,现在有了Maven就不需要我们手动导入了,只需要在Maven中配置即可。
  • 完成controller操作以后,再通过SpringMVC前端控制器控制页面跳转或者重定向之类的操作,但是现在一般使用ajax技术,提高前端页面的性能

整个请求过程经历的各个模块,我想我已经说的很清楚了。其中最为核心的是Spring,Spring管理着这些类,供其他类完成依赖注入。

二、配置

1.创建好对应的模块
在这里插入图片描述
2.导入jar包
Spring整合Mybatis和SpringMVC所需jar包

3.在resource目录下新建 applicationContext.xml文件,用于整合MyBatis与Spring

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="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.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 开启注解扫描,管理service和dao -->
    <context:component-scan base-package="ssm.service"></context:component-scan>
    <context:component-scan base-package="ssm.dao"></context:component-scan>

    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置连接池 -->
    <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}" />
    </bean>
    <!-- 把交给IOC管理 SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 传入PageHelper的插件 -->
        <property name="plugins">
            <array>
                <!-- 传入插件的对象 -->
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="helperDialect">oracle</prop>
                            <prop key="reasonable">true</prop>  <!--逻辑判断使得页码不会越界-->
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!-- 扫描dao接口 -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="ssm.dao"/>
    </bean>


    <!-- 配置Spring的声明式事务管理 -->
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

4.在resource目录下新建 db.properties文件配置连接池信息

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.179.129:1521:orcl
jdbc.username=ssm
jdbc.password=ssm

5.在resource目录下新建spring-mvc.xml文件,用于整合Spring和SpringMVC

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
           ">

    <!-- 扫描controller的注解,别的不扫描 -->
    <context:component-scan base-package="ssm.controller">
    </context:component-scan>

    <!-- 配置视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- JSP文件所在的目录 -->
        <property name="prefix" value="/pages/" />
        <!-- 文件的后缀名 -->
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 设置静态资源不过滤 -->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/img/" mapping="/img/**" />
    <mvc:resources location="/js/" mapping="/js/**" />
    <mvc:resources location="/plugins/" mapping="/plugins/**" />

    <!-- 开启对SpringMVC注解的支持 -->
    <mvc:annotation-driven />

    <!--
        支持AOP的注解支持,AOP底层使用代理技术
        JDK动态代理,要求必须有接口
        cglib代理,生成子类对象,proxy-target-class="true" 默认使用cglib的方式
    -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

</beans>

6.在web.xml文件配置控制器、监听器等,包含springSecurityFilterChain配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <!-- 配置加载类路径的配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml,classpath*:spring-security.xml</param-value>
  </context-param>

  <!-- 配置监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

  <!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- 解决中文乱码过滤器 -->
  <filter>
    <filter-name>characterEncodingFilter</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>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <error-page>
    <error-code>403</error-code>
    <location>/403.jsp</location>
  </error-page>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

</web-app>

7.在resource路径下添加log4j.properties文件

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Encoding=UTF-8
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
# log4j.appender.LOGFILE=org.apache.log4j.FileAppender
# log4j.appender.LOGFILE.File=d:\axis.log
# log4j.appender.LOGFILE.Append=true
# log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
# log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

8.在resource下新建spring-security.xml文件,配置安全框架

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:security="http://www.springframework.org/schema/security"
       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
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security.xsd">

    <security:global-method-security pre-post-annotations="enabled" secured-annotations="enabled"></security:global-method-security>

    <!-- 配置不拦截的资源 -->
    <security:http pattern="/login.jsp" security="none"/>
    <security:http pattern="/failer.jsp" security="none"/>
    <security:http pattern="/css/**" security="none"/>
    <security:http pattern="/img/**" security="none"/>
    <security:http pattern="/plugins/**" security="none"/>

    <!--
    	配置具体的规则
    	auto-config="true"	不用自己编写登录的页面,框架提供默认登录页面
    	use-expressions="false"	是否使用SPEL表达式(没学习过)
    -->
    <security:http auto-config="true" use-expressions="true">
        <!-- 配置具体的拦截的规则 pattern="请求路径的规则" access="访问系统的人,必须有ROLE_USER的角色" -->
        <security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>

        <!-- 定义跳转的具体的页面 -->
        <security:form-login
                login-page="/login.jsp"
                login-processing-url="/login.do"
                default-target-url="/index.jsp"
                authentication-failure-url="/failer.jsp"
                authentication-success-forward-url="/pages/main.jsp"
        />

        <!-- 关闭跨域请求 -->
        <security:csrf disabled="true"/>

        <!-- 退出 -->
        <security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp" />

    </security:http>

    <!-- 切换成数据库中的用户名和密码 -->
    <security:authentication-manager>
        <security:authentication-provider user-service-ref="userService">
            <!-- 配置加密的方式 -->
            <security:password-encoder ref="passwordEncoder"/>
        </security:authentication-provider>
    </security:authentication-manager>

    <!-- 配置加密类 -->
    <bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

    <!-- 提供了入门的方式,在内存中存入用户名和密码
    <security:authentication-manager>
    	<security:authentication-provider>
    		<security:user-service>
    			<security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
    		</security:user-service>
    	</security:authentication-provider>
    </security:authentication-manager>
    -->

</beans>

注: 此文章只为记录开发过程中的错误,学习,如发现侵权请私信删除


http://www.niftyadmin.cn/n/1691627.html

相关文章

深度学习知识一:使用到的激活函数种类和优缺点解释!

1、几种常见的激活函数 Sigmoid. Sigmoid(也叫逻辑激活函数) 非线性激活函数的形式是σ(x)1/(1e−x)&#xff0c;其图形如上图左所示。之前我们说过&#xff0c;sigmoid函数输入一个实值的数&#xff0c;然后将其压缩到0~1的范围内。特别地&#xff0c;大的负数被映射成0&#…

为什么Java类只能单继承

本文转载自简书作者“Jadyn”&#xff0c;原文链接https://www.jianshu.com/p/014a40085e35 首先我们要明确一个事实&#xff0c;在Java语言中类只能单继承与某个类&#xff0c;却可以多集成接口。同时接口与接口之间可以多继承。 为什么Java类只能单继承&#xff1f; “Java…

深度学习知识九:深度网络里白化、Batch Normalization、LRN的使用区别!!其中Batch Normalization机制可以提供很好的模型剪枝操作。

参考大神的讲解地址&#xff1a; 深度学习&#xff08;二十九&#xff09;Batch Normalization 学习笔记 BN算法&#xff08;Batch Normalization&#xff09;其强大之处如下&#xff1a; (1)你可以选择比较大的初始学习率&#xff0c;让你的训练速度飙涨。以前还需要慢慢调整…

java基本类型和包装类型的区别

本文转载自博客作者“十一月的天蝎兽”&#xff0c;原文链接https://blog.csdn.net/cynthia9023/article/details/17413375 Java的类型分为两部分&#xff0c;一个是基本类型&#xff08;primitive&#xff09;&#xff0c;如int、double等八种基本数据类型&#xff1b; 另一个…

BREW学习笔记-IBitmap 接口(二)

IBITMAP_BltIn()说明&#xff1a;此函数可以将对应于像素矩形的数据从指定的源位图位块传输到此位图。 源中每个像素均与目标中的相应像素关联。 此函数会为每对源和目标像素执行逻辑操作&#xff0c;并将结果写入目标像素。 原型&#xff1a;int IBITMAP_BltIn (IBitmap * po,…

深度学习知识十四 yolo v2 损失函数源码(训练核心代码)解读和其实现原理、网络的输出格式

前提说明&#xff1a; 1, 关于 yolo 和 yolo v2 的详细解释请移步至如下两个链接&#xff0c;或者直接看论文&#xff08;我自己有想写 yolo 的教程&#xff0c;但思前想后下面两个链接中的文章质量实在是太好了_(:з」∠)_&#xff09; yolo: https://zhuanlan.zhihu.com/p/2…

BREW学习笔记-IBitmap 接口(三)

IBITMAP_FillRect()说明&#xff1a;此函数用于绘制指定颜色的实心矩形。 原型&#xff1a;int IBITMAP_FillRect (IBitmap * po, const AEERect *prc, NativeColor color, AEERasterOp rop ) IBITMAP_DrawHScanline()说明&#xff1a;此函数用于绘制水平线段。 原型&#xff…

什么是节点流和处理流?

本文转载自博客作者“浪漫的程序员”&#xff0c;原文链接https://blog.csdn.net/qq_40406929/article/details/84202772 按照流是否直接与特定的地方(如磁盘、内存、设备等)相连&#xff0c;分为节点流和处理流两类。 节点流&#xff1a;可以从或向一个特定的地方(节点)读写数…