Spring MVC + Spring + Mybitis是除了SSH外的另外一种常见的web框架组合。

Java web开发和普通的Java应用程序开发是不太一样的,下面是一个Java web开发在Eclipse EE中的目录结构:

普通的Java应用开发,我们的代码都在src目录下,不论是Java代码,配置文件,还是资源文件等。而Java web开发,我们关注的不是src目录,而是WebConent目录,发布项目时最终会将WebConent目录下的所有文件打成war包进行发布。

WebConent目录下面有META-INF目录,其下有一个MANIFEST.MF文件,这是记录版本信息的,和开发无关,打包集成时可以生成自己的MANIFEST文件用于记录版本信息。

WEB-INF目录下放的是classes文件和lib文件,classes下存的就是src目录下文件的编译结果,lib下的是工程所需要引用的jar包。WEB-INF目录下还有一个web.xml,这个很重要,这个是工程的总的配置文件。

   从上图 可以看到java web将src目录下编译的结果会放到WEB-INF目录下的classes文件夹中,普通的java工程src的编译结果放倒的是bin目录下。

这是一个最基本的结构,如果还有图片资源,js文件,css文件等,在WebConent目录下建立对应的子目录即可。

工程发布时需要的是war包,不是普通java程序的jar包。war包的制作也是很简单,工程上点右键,选择Export选择war包即可。

  先看web.xml,下面是一个Spring MVC + Spring + Mybitis开发的web.xml示例:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name> <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> <!-- 加载除了spring-mvc.xml外的其他xml文件,context-param中指定了所要加载的文件 -->
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml;classpath:spring-mybatis.xml</param-value>
</context-param> <!-- 防止内存溢出 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!-- DispatcherServlet将加载spring-mvc.xml,完成controller注入,jsp解析目录的指定 -->
<servlet>
<description>spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</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>springMvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <session-config>
<session-timeout>15</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

XML的编码格式建议使用UTF-8,<?xml version="1.0" encoding="UTF-8"?>,使用GB2312可能会存在问题。

下面这段的作用是在程序启动时就去加载spring.xml和spring-mybatis.xml。

<!-- 加载除了spring-mvc.xml外的其他xml文件,context-param中指定了所要加载的文件 -->
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml;classpath:spring-mybatis.xml</param-value>
</context-param> 下面定义了一个servlet,<load-on-startup>1</load-on-startup>说明程序启动时就会加载这个servlet,这个servlet处理的请求是<url-pattern>*.do</url-pattern>,而我们的示例JSP代码中的请求都是*.do,也就是JSP的请求都会交由这个servlet处理。
<param-value>classpath:spring-mvc.xml</param-value>指明servlet初始化时要加载spring-mvc.xml,spring-mvc.xml是控制器的配置文件,将制定JSP请求由哪些类去处理。
 <!-- DispatcherServlet将加载spring-mvc.xml,完成controller注入,jsp解析目录的指定 -->
<servlet>
<description>spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</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>springMvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

上面提到程序初始化时会加载spring.xml和spring-mybatis.xml,看看spring.xml和spring-mybatis.xml的内容,先是spring.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" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config.properties" /> <!-- 打开Spring的Annotation的支持 -->
<context:annotation-config />
<!-- 自动扫描(自动注入) -->
<context:component-scan base-package="com.bky.service..*" />
<!-- 将自动对标注 @Autowired 的 Bean 进行注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> </beans>

spring.xml是Spring的配置文件,主要是支持Ioc和注释,自动扫描。

这段的作用是引入一个配置文件:config.properties,我们用config.properties来存储参数初始化数据库连接池。

<!-- 引入属性文件 -->

<context:property-placeholder location="classpath:config.properties" />

config.properties的内容如下:

validationQuery=SELECT 1
jdbc_url=jdbc:mysql://localhost:13306/nnm5?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc_username=root
jdbc_password=OSSDB123

  下面是一些Spring的配置,支持Ioc,注解和自动扫描,具体的作用需要对Spring有基本的认识。

spring-mybatis.xml的内容如下,这里边用到了Spring的AOP,tx,数据库连接池等,内容如下:

<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
"> <!-- 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" /> <!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" /> <!-- 监控数据库 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean> <!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping目录, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:com/bky/mapping/*.xml" />
</bean> <!--从base 包中搜索所有下面所有 interface,并将其注册到 Spring Bean容器中,其注册的class bean是MapperFactoryBean-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bky.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 注解方式配置事务 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> <!-- 拦截器方式配置事务 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.bky.service..*Impl.*(..))" />
<!-- 除了事务,不建议使用aop:advisor,应该使用aop:aspect -->
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config> <!-- 配置druid监控spring jdbc -->
<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
<property name="patterns">
<list>
<value>com.bky.service.*</value>
</list>
</property>
</bean> <aop:config>
<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
</aop:config>
</beans>

  

  dataSource指明的是数据库操作所使用的数据源,这里使用的是阿里巴巴的Druid数据源,里面的username,password等参数的值是从config.properties中读取的。

  还需要在数据库中创建相应的数据表:

mysql> create table tadd(

-> id varchar(200) default '1',

-> tname varchar(30),

-> tpwd varchar(20),

-> primary key(id));

  下面是MyBatis的配置文件,完成dao类与Mapper.xml的映射关系,并使用上面配置的数据源进行CRUD操作。

<!-- myBatis文件 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

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

<!-- 自动扫描mapping目录, 省掉Configuration.xml里的手工配置 -->

<property name="mapperLocations" value="classpath:com/bky/mapping/*.xml" />

</bean> 

<!--从base 包中搜索所有下面所有 interface,并将其注册到 Spring Bean容器中,其注册的class bean是MapperFactoryBean-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.bky.dao" />

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

</bean>

<!-- 拦截器方式配置事务 -->

<tx:advice id="transactionAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="insert*" propagation="REQUIRED" />

<tx:method name="update*" propagation="REQUIRED" />

<tx:method name="modify*" propagation="REQUIRED" />

<tx:method name="delete*" propagation="REQUIRED" />

<tx:method name="*" propagation="SUPPORTS" />

</tx:attributes>

</tx:advice>

  下面的作用是打开事务,并配置哪些数据库操作需要在事务中进行,如需更为复杂的事务操作,需要按照实际业务配置。

  <!-- 配置事务管理器 -->

       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

</bean>

  配置了一个切面以支持事务,

  <aop:config>

<aop:pointcut id="transactionPointcut" expression="execution(* com.bky.service..*Impl.*(..))" />

<!-- 除了事务,不建议使用aop:advisor,应该使用aop:aspect -->

<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />

</aop:config>

Spring MVC + Spring + Mybitis开发Java Web程序基础的更多相关文章

  1. maven Spring+Spring MVC+Mybatis+mysql轻量级Java web开发环境搭建

    之前一直在做的一个GIS系统项目,采用了jsp+servlet框架,数据传输框架采用了apache的thrift框架,短时多传的风格还不错,但是较其他的java web项目显得有点太臃肿了,现在给大家 ...

  2. Spring Boot 2.X(三):使用 Spring MVC + MyBatis + Thymeleaf 开发 web 应用

    前言 Spring MVC 是构建在 Servlet API 上的原生框架,并从一开始就包含在 Spring 框架中.本文主要通过简述 Spring MVC 的架构及分析,并用 Spring Boot ...

  3. SpringMVC内容略多 有用 熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器、过滤器等Web组件以及MVC架构模式进行Java Web项目开发的经验。

    熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器.过滤器等Web组件以及MVC架构 ...

  4. SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。

    SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...

  5. @Java Web 程序员,我们一起给程序开个后门吧:让你在保留现场,服务不重启的情况下,执行我们的调试代码

    一.前言 这篇算是类加载器的实战第五篇,前面几篇在这里,后续会持续写这方面的一些东西. 实战分析Tomcat的类加载器结构(使用Eclipse MAT验证) 还是Tomcat,关于类加载器的趣味实验 ...

  6. Spring MVC -- Spring MVC入门

    本篇博客首先介绍Spring MVC的优点,然后介绍Spring MVC的基本组件,包括DispatcherServlet,并学习如何开发一个“传统风格”的控制器,这是在Spring 2.5版本之前开 ...

  7. Spring MVC+Spring +Hibernate配置事务,但是事务不起作用

    最近做项目,被一个问题烦恼了很久.使用Spring MVC+Spring +Hibernate开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据.怀疑是配 ...

  8. Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建

    目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...

  9. Java Web程序工作原理

    Web开发的最重要的基本功能是HTTP:Java Web开发的最重要的基本功是Servlet Specification.HTTP和Servlet Specitication对于Web Server和 ...

随机推荐

  1. UVA424高精度加法

    One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of po ...

  2. 《TensorFlow深度学习应用实践》

    http://product.dangdang.com/25207334.html 内容 简 介 本书总的指导思想是在掌握深度学习的基本知识和特性的基础上,培养使用TensorFlow进行实际编程以解 ...

  3. sublime汉化教程

    转自: http://www.cnblogs.com/marsggbo/p/6622960.html 如何给sublime text3安装汉化包?so easy 哦 这是我本人亲身测试过的,肯定有效, ...

  4. 了解前端中的SPA

    单页Web应用(single page web application,SPA),就是只有一张Web页面的应用,是加载单个HTML 页面并在用户与应用程序交互时动态更新该页面的Web应用程序. 单页W ...

  5. Linux 用户组及用户管理

    查看所有组的信息:(信息保存在/etc/group文件中) 其中每段信息用:分割 ,每段的含义如下: 用户名组名:密码:用户组的id:用户组所包含的用户(多个用户用,分割) 查看所有的用户信息:(信息 ...

  6. ECharts插件的使用

    ECharts插件:官网下载echarts.js开发者可以选择源码.下载地址:http://echarts.baidu.com/download.html 下载之后,echarts.js放在js文件夹 ...

  7. Spark算子--union、intersection、subtract

    转载请标明出处http://www.cnblogs.com/haozhengfei/p/252bcc1d1ab30c430d347279d5827615.html union.intersection ...

  8. 使用vue框架运行npm run dev 时报错解决

    使用使用vue框架运行npm run dev 时报错 如下: 原因: localhost:8080 有可能其他软件占用了,导致其他问题的出现 我们可以动态修改地址 解决: 进入项目文件的config文 ...

  9. Python自建logging模块

    本章将介绍Python内建模块:日志模块,更多内容请从参考:Python学习指南 简单使用 最开始,我们用最短的代码体验一下logging的基本功能. import logging logger = ...

  10. servlet多线程问题

    Servlet本身是单实例的,这样当多个用户同时访问某个Servlet时,会访问该唯一的Servlet实例中的成员变量,如果对成员变量进行写入工作,那就会导致Servlet的多线程问题,即数据不一致. ...