目录

  • 古老的SSM企业级应用

    • 框架搭建步骤

古老的SSM企业级应用

Author:SimpleWu

目前Spring+SpringMVC+Mybatis也算是一套非常流行的配套开发框架。

  1. spring核心ioc、aop技术,ioc解耦,使得代码复用,可维护性大幅度提升,aop提供切面编程,同样的增强了生产力。提供了对其他优秀开源框架的集成支持
  2. spring mvc是对比struts2等mvc框架来说的,不说struts2爆出的那么多安全漏洞,而且是类拦截,所有Action变量共享,同时是filter入口的,而spring mvc是方法拦截,controller独享request response数据,采用的serlvet入口,与spring无缝对接。开发而言,spring mvc更加轻量和低入门。
  3. mybatis轻量级半自动化框架,sql由开发者编写可对语句进行调优,并且mybatis使用XML方式JAVA代码与SQL可以解耦并且支持动态SQL语句,学习成本低。

框架搭建步骤

导包

  1. 导入Spring+SpringMVC(如果不会选全倒进去就行了)
  2. 导入mybatis包(如果需要用到日志可将mybatis依赖包导入)
  3. 导入mybatis-spring-1.3.1.jar(整合必须又这个包)
  4. 导入c3p0(当然你也可以使用其他连接池)
  5. 导入数据库驱动

配置log4j.properties

由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。

#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

配置WEB.xml

1.设置编码过滤器

<filter><description>字符集过滤器</description><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><description>字符集编码</description><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param>
</filter>
<filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>

2.添加Spring配置文件位置(等下我们创建spring-context.xml)

<!-- 配置加载Spring-context文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-context.xml</param-value></context-param>
<!--添加Spring的监听器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

3.DispatcherServlet配置

<!-- SprigMVC配置 -->
<servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><description>springmvc 配置文件</description><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern>
</servlet-mapping>

4.添加PUT DELETE支持

<!-- 添加PUT DELETE支持 --><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>

5.配置Sessin过期时间

<!-- 配置session超时时间,单位分钟 -->
<session-config><session-timeout>15</session-timeout>
</session-config>

spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 --><context:component-scan base-package="com.simple.ssm.controller"><!-- 只扫描@Controller与@ControllerAdvice修饰的类 --><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/></context:component-scan><!-- 加入静态资源与动态资源支持 --><mvc:default-servlet-handler/><mvc:annotation-driven/><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/" /><property name="suffix" value=".jsp"></property></bean></beans>

spring-context.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:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"><!--引入外部数据库连接信息文件--><context:property-placeholder location="classpath:db.properties" /><!-- 扫描所有除@Controller ,@ControllerAdvice修饰的bean --><context:component-scan base-package="com.simple.ssm"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /><context:exclude-filter type="annotation"expression="org.springframework.web.bind.annotation.ControllerAdvice" /></context:component-scan><!-- 配置c3p0连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${mysql.driverClass}" /><!-- jdbc:mysql://localhost/mybatis?characterEncoding=utf8&amp;serverTimezone=UTC --><property name="jdbcUrl" value="${mysql.jdbcUrl}" /><!-- 连接用户名 --><property name="user" value="${mysql.user}" /><property name="password" value="${mysql.password}" /><!-- 连接密码 --><!-- 队列中的最小连接数 --><property name="minPoolSize" value="15" /><!-- 队列中的最大连接数 --><property name="maxPoolSize" value="25" /><!-- 当连接耗尽时创建的连接数 --><property name="acquireIncrement" value="15" /><!-- 等待时间 --><property name="checkoutTimeout" value="10000" /><!-- 初始化连接数 --><property name="initialPoolSize" value="20" /><!-- 最大空闲时间,超出时间连接将被丢弃 --><property name="maxIdleTime" value="20" /><!-- 每隔60秒检测空闲连接 --><property name="idleConnectionTestPeriod" value="60000" /></bean><!-- 配置事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 拦截器方式配置事物 --><!-- <tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED" /><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="save*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="remove*" propagation="REQUIRED" /><tx:method name="find*" propagation="SUPPORTS" /><tx:method name="load*" propagation="SUPPORTS" /><tx:method name="search*" propagation="SUPPORTS" /><tx:method name="*" propagation="SUPPORTS" /></tx:attributes></tx:advice>--><!-- 配置切面 --><!-- <aop:config> 事务入口(Service的包路径) <aop:pointcut id="transactionPointcut" expression="execution(* com.simple.ssm.service.*.*(..))" /> 将事务通知与切入点组合 <aop:advisor pointcut-ref="transactionPointcut" advice-ref="txAdvice" /> </aop:config> --><!-- 使用注解来控制事务 --><tx:annotation-driven transaction-manager="transactionManager" /><!-- 配置mybatis, 绑定c3p0 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 配置mybatis配置文件所在位置 --><property name="configLocation" value="classpath:mybatis-config.xml" /><!-- 配置实体类XML映射所在位置 --><property name="mapperLocations" value="classpath:com/simple/ssm/dao/mapper/*.xml" /></bean><!-- 扫描生成所有dao层 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 指定持久化接口包位置 --><property name="basePackage" value="com.simple.ssm.dao"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean>
</beans>

mybaits-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><settings><!-- 开启驼峰式命名规则 --><setting name="mapUnderscoreToCamelCase" value="true"/><!-- 开启二级缓存 --><setting name="cacheEnabled" value="true"/><!-- 开启懒加载 --><setting name="lazyLoadingEnabled" value="true"/></settings><typeAliases><package name="com.simple.ssm.entitys"/></typeAliases>
</configuration>

db.properties(可内置)

mysql.driverClass=com.mysql.jdbc.Driver
mysql.jdbcUrl=jdbc:mysql://localhost/mybatis?characterEncoding=utf8&serverTimezone=UTC
mysql.user=root
mysql.password=root

到这里其实我们的SSM已经整合完成,如果我们需要其他功能可以在加,不要忘记导入包。

转载于:https://www.cnblogs.com/SimpleWu/p/9792466.html

古老的SSM企业级应用相关推荐

  1. Java程序员进阶架构师必备学习文档:SSM+微服务+分布式+Nginx+MySQL

    SSM企业级战 SSM框架是以Spring为核心,整合Spring MVC和Mybatis的轻量级框架技术的组合.利用SSM整合框架可以开发出分层.易扩展.易维护的企业级应用系统,能够极大地满足企业需 ...

  2. 学习 瑞吉外卖项目——总结

    本文为个人学习黑马<瑞吉外卖>项目后进行的项目总结,更偏向于对自己编写文本能力的锻炼以及对项目知识点的简短记录.因为个人能力问题,其中可行性分析和测试部分只进行了小标题的陈列,并没有进行编 ...

  3. 工资倒挂 你怎么看?工作三年薪资不如毕业生!

    总有人吐槽公司校招和社招薪资倒挂,有的 Java 工程师好几年工作经验的还不如校招工资高. 工资倒挂,是很多公司都有的现象.这有啥好吐槽的,有的人工作个两三年就开始晃荡,不再进行自我提升,抱着那点不值 ...

  4. Alibaba大牛常读的10本Java实战书籍,(Java开发进阶必备书单),可以白嫖了

    关乎于程序员,除了做项目来提高自身的技术,还有一种提升自己的专业技能就是:多!看!书! 毕竟,书是学习的海洋呢!So,Java程序员你们准备好了吗?双手奉上Java程序员必读之热门书单. 在下面这 1 ...

  5. 阿里大人都在读的10本Java实战书籍,Java开发进阶必备书单

    关乎于程序员,除了做项目来提高自身的技术,还有一种提升自己的专业技能就是:多!看!书! 毕竟,书是学习的海洋呢!So,Java程序员你们准备好了吗?双手奉上Java程序员必读之热门书单. 在下面这 1 ...

  6. Java开发进阶10本必备书单

    书是学习的海洋 除了做项目来提高自身的技术,还有一种提升自己的专业技能就是多看书! <SSM企业级框架实战> 框架( Framework)的本质为某种应用的半成品,即把不同应用程序中的共性 ...

  7. 阿里P8推荐的10本Java实战书籍,Java开发进阶必备书单

    关乎于程序员,除了做项目来提高自身的技术,还有一种提升自己的专业技能就是:多!看!书! 毕竟,书是学习的海洋呢!So,Java程序员你们准备好了吗?双手奉上Java程序员必读之热门书单. 在下面这 1 ...

  8. 987页的Java面试宝典,看完才发现,应届生求职也没那么难

    前言 现在已经九月底,金九银十也已经过去了一大半,很明显今年的面试季明显不如往年火热,对于求职者来说,也更难了一些.马上迎来国庆节,假期一过,十月份又过去了三分之一,综合来看今年确实不是面试的最佳时期 ...

  9. 女大学生第一次面试就入职美团,只因这份987页Java面试宝典,看完才发现,应届生求职也没那么难

    前言 现在已经七月中,金九银十马上就要到来了,很明显今年的面试季明显不如往年火热,对于求职者来说,也更难了一些.综合来看今年确实不是面试的最佳时期,不过趁今年所剩的时间来好好准备,明年的金三银四或许会 ...

最新文章

  1. 广东时代互联---网络管理面试
  2. Intel Core Enhanced Core架构/微架构/流水线 (6) - 指令预译码/指令队列/指令译码
  3. 多数据源与动态数据源的权衡
  4. nlp基础—4.搜索引擎中关键技术讲解
  5. Asp.net WebApi版本控制
  6. kafaka,activityMQ,rabbitMQ消息中间件对比
  7. Maven实战 PDF 许晓斌
  8. 如何用Java写一个规范的http接口?
  9. 超好用的后台管理的框架
  10. ALS算法介绍(协同过滤算法介绍)
  11. Labelme直接生成灰度图
  12. 读书 - 《许三观卖血记》
  13. 计算机字号调整,解答如何调整电脑字体大小
  14. 大数据行业前景如何?就业岗位有哪些?
  15. python递归函数例题_递归案例python
  16. 2019年‘泰迪杯’数据分析职业技能大赛A题——个人代码分享
  17. python-DataFrame练习
  18. 生活细语:送给每一个热爱生活的人
  19. 使用vector创建一个二维数组(一)
  20. 终极解码2013春节版在windows7上使用

热门文章

  1. CentOS 7上源码编译安装和配置LNMP Web+phpMyAdmin服务器环境
  2. 《社交网站界面设计(原书第2版)》——2.10 自我反省式的出错信息
  3. SERU最佳需求分析方法
  4. 移动端高清、多屏适配方案
  5. 基于redis AE异步网络架构
  6. 开始做事了...............
  7. oracle tns 代理配置_OGG实现Oracle到MySQL数据平滑迁移
  8. oracle期末重点,oracle期末复习题及答案
  9. Metasploit save命令技巧
  10. 文件包含漏洞检测工具fimap