`
qingxing30
  • 浏览: 32071 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论
阅读更多
框架整合中需要各个软件版本
Spring : spring-flex-1.0.3.RELEASE.zip
DWR: dwr-3.0.0.116.rc1-src.zip
mybatis: mybatis-3.0.6-bundle.zip
框架整合后的配置文件结构图




说明:
mybatis-config.xml 放置在src目录下,配置mybatis的参数
applicationContext.xml 放置WEB-INF目录下,配置spring的基础配置和数据源等
db.properties 放置WEB-INF目录下,配置数据库的连接参数
web.xml 配置web应用,如springMVC中的前端控制器DispatcherServlet和ContextLoaderListener  spring监听器等。
Web.xml详细配置和说明
配置Spring的启动监听
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
配置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>/WEB-INF/config/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
配置DWR前端控制器

<servlet-mapping> 
        <servlet-name>springmvc</servlet-name> 
        <url-pattern>/dwr/*</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>

applicationContext.xml配置
配置 以注解方式扫描springBean
<!—以注解方式扫描Spring Bean -->
<context:annotation-config />
<!—配置扫描SpringBean的时候 都需要扫描哪些目录下 -->
<context:component-scan base-package="com"></context:component-scan>
配置数据源

<!-- 这个bean 由spring提供,用来加载properties文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>WEB-INF/db.properties</value>
</property>
</bean>
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${username}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
<!-- 数据库驱动类 -->
<property name="driverClassName">
<value>${driverClassName}</value>
</property>
<!-- 连接池的最大数据库连接数。设为0表示无限制。 -->
<property name="maxActive">
<value>${maxActive}</value>
</property>
<!-- 最大的空闲连接数,这里取值为30,表示即使没有数据库连接时依然可以保持30个空闲的连接,而不被清除,随时处于待命状态。设为0表示无限制。 -->
<property name="maxIdle">
<value>${maxIdle}</value>
</property>
<!-- 最大建立连接等待时间(毫秒)。如果超过此时间将接到异常。设为-1表示无限制 -->
<property name="maxWait">
<value>${maxWait}</value>
</property>
<!--指定数据库的默认自动提交 -->
<property name="defaultAutoCommit">
<value>${defaultAutoCommit}</value>
</property>
<!--是否自动回收超时连接 -->
<property name="removeAbandoned">
<value>${removeAbandoned}</value>
</property>
<!--超时时间(以秒数为单位) -->
<property name="removeAbandonedTimeout">
<value>${removeAbandonedTimeout}</value>
</property>
<property name="logAbandoned">
<value>${logAbandoned}</value>
</property>
<property name="testOnBorrow">
<value>true</value>
</property>
</bean>
db.properties文件
# dataSource config
#Oracle
#url=jdbc:oracle:thin:@localhost:1521:orcl
#MySql
url=jdbc:mysql://127.0.0.1:3306/framework
username=root
password=root
#Oracle
#driverClassName=oracle.jdbc.driver.OracleDriver
#MySql
driverClassName=com.mysql.jdbc.Driver
maxActive=100
maxIdle=30
maxWait=3000
defaultAutoCommit=true
removeAbandoned=true
removeAbandonedTimeout=60
logAbandoned=true

配置mybstis SqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
<property  name="mapperLocations"  value="classpath*:com/**/*.xml"/> 
</bean>
说明:
<property  name="mapperLocations"  value="classpath*:com/**/*.xml"/>
指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件,mapperLocations和configLocation有一个即可,当需要为实体类指定别名时,可指定configLocation属性,再在mybatis总配置文件中采用mapper引入实体类映射文件。
配置事务
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config>
<aop:advisor pointcut="execution(* com.sanling..*Service.*(..))" advice-ref="txAdvice" />
<aop:advisor pointcut="execution(* com.sanling.utils..*Service.*(..))" advice-ref="txAdvice" />
</aop:config>

<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="is*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

springmvc.xml
配置springMVC的调转规则

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
配置DWR

<context:annotation-config />
<context:component-scan base-package="com" />
<dwr:annotation-scan scanRemoteProxy="true" scanDataTransferObject="true" base-package="com" /> 
<dwr:annotation-config /> 
<dwr:url-mapping /> 
<dwr:controller id="dwrController" debug="true" /> 
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
<property name="order" value="1" /> 
</bean> 
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> 
<property name="order" value="2" /> 
</bean> 
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="order" value="3" />  
      <property value="true" name="alwaysUseFullPath"></property> 
      <property name="mappings"> 
        <props> 
          <prop key="/dwr/**">dwrController</prop> 
        </props> 
     </property> 
    </bean>  
说明:
DWR配置
<dwr:annotation-scan>
扫描JavaBean的转换,@DataTransferObject对JavaBean进行转换,@RemoteProperty,必须加在get方法上。
<context:annotation-config /> 
要求DWR在Spring容器中检查拥有@RemoteProxy 和 @RemoteMethod注解的类。注意它不会去检查Spring容器之外的类。
<dwr:url-mapping />
要求DWR将util.js和engine.js映射到dwrController
<dwr:configuration />
此标签在这个例子中不是必须的,如果你想配置Spring容器之外的类,就需要它了
<dwr:controller id="dwrController" debug="true" /> 
部署项目时, 请把debug设为false
<context:component-scan base-package="com.myapp.web.controller" />
多个包名用逗号隔开, 但不能有空格
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
添加下面的Bean,就可以通过http://localhost:8080/myApp/dwr来访问test page
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
order值越小, 优先级越高,必须有这项配置,否则SpringMVC和DWR就不能同时使用注解
对于<context:annotation-config /> 在applicationContext.xml中已经有过配置,则可以把applicationContext.xml中的去掉,保留springmvc.xml中的就可以。

BaseDao

@Repository
public class BaseDao extends SqlSessionDaoSupport{
private SqlSessionFactory sqlSessionFactory;
@Resource
public void setSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
public BaseDao() {
System.out.println("--BaseDao---------");
}

说明:
BaseDao 为dao层,定义了基础的数据库操作
BaseDao层,自动注入了mybatis的SqlSessionFactory,BaseDao层中数据库的相关操作,都可以通过SqlSessionFactory来完成
JavaBean
@DataTransferObject
public class Region implements Serializable {

private int id;
private int pid;
private String name;
private String code;
}
说明:
注解@DataTransferObject的作用是在DWR调用Java,返回JavaBean对象的时候,JavaBean对象和JS对象进行转换。
JavaBean的映射文件region.mapper.xml

<mapper namespace="region">
<resultMap type="com.sanling.business.region.entity.Region" id="resultMap">
<id property="id" column="id" />
<result property="pid" column="pid" />
<result property="code" column="code" />
<result property="name" column="name" />
<result property="codeOrder" column="codeOrder" />
<result property="treeLevel" column="treeLevel" />
<result property="innerCode" column="innerCode" />
</resultMap>
<insert id="save" parameterType="com.sanling.business.region.entity.Region">
<selectKey resultType="int" keyProperty="id">   
<!--
要返回保存数据的ID
orcale:SELECT LOGS_SEQ.nextval AS ID FROM DUAL
MYSQL: SELECT LAST_INSERT_ID()
-->
     SELECT LAST_INSERT_ID()    
</selectKey>
insert into
tb_region(pid,code,name,codeOrder,treeLevel,innerCode)
values(#{pid},#{code},#{name},#{codeOrder},#{treeLevel},#{innerCode})
</insert>
</mapper>
Service
@Service
public class RegionService {

private IDao dao;
@Resource
public void setDao(BaseDao dao) {
this.dao = dao;
}
public int saveRegion(Region bean) throws Exception{
this.dao.save("region.save", bean);
return bean.getId();
}

说明:
RegionService层自动注入dao层,提供数据库访问操作
Action
@Controller
@RemoteProxy
@RequestMapping("/regionAction")
public class RegionAction {
private RegionService regionService ;

@Resource
public void setRegionService(RegionService regionService) {
this.regionService = regionService;
}

@RequestMapping("/testHttp")
public void testHttp(){
System.out.println(" http test !");
}
@RemoteMethod
public Region addRegion(Region bean){
if(bean!=null){
String innerCode = bean.getInnerCode()+bean.getCode();
bean.setInnerCode(innerCode);
try {
int id = this.regionService.saveRegion(bean);
bean.setId(id);
} catch (Exception e) {
e.printStackTrace();
}
}
return bean ;
}
}
说明:
类注解@RemoteProxy:表明允许DWR框架调用该后台类
方法注解@RemoteMethod:表明允许DWR调用后台java类中的方法
类注解和方法注解@RequestMapping("/regionAction"):表明该类为SpringMVC类,访问路径/regionAction/testHttp.action 就能访问到类RegionAction中的testHttp方法(页面怎么调转,这里不做详细说明)
测试
测试SpringMVC是否能正常访问:
http://localhost:8080/项目名/regionAction/testHttp.action 看看程序是否能进入到testHttp()方法即可。
测试DWR是否正常
访问:http://localhost:8080/项目名/dwr 能看到如下界面

注意,只要在类上注解@RemoteProxy,则在测试DWR的时候,都能看到这些类名。
  • 大小: 5.7 KB
分享到:
评论

相关推荐

    Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码.zip

    Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+...

    基于Spring+SpringMVC+Mybatis架构的博客系统.zip

    基于Spring+SpringMVC+Mybatis架构的博客系统:博客管理、图表数据、日志分析、访问记录、图库管理、资源管理、友链通知等。良好的页面预加载,无限滚动加载,文章置顶,博主推荐等。提供 用户端+管理端 的整套系统...

    完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip

    完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统...

    Spring+SpringMVC+Mybatis框架整合例子(SSM) 下载

    3. 配置SpringMVC:创建SpringMVC的配置文件,如servlet-context.xml,配置DispatcherServlet、ViewResolver、HandlerMapping等。 4. 配置Mybatis:创建mybatis的全局配置文件,mybatis-config.xml,配置数据源、...

    SpringMVC+Mybatis demo

    接下来,我们将深入探讨这两个框架以及它们在"SpringMVC+Mybatis demo"中的应用。 **SpringMVC** SpringMVC是Model-View-Controller架构模式的一种实现,用于构建Web应用程序。它的主要组件包括DispatcherServlet...

    基于SSM(Spring+SpringMVC+Mybatis)的新闻管理系统源码+数据库.zip

    基于SSM(Spring+SpringMVC+Mybatis)的新闻管理系统源码+数据库.zip 基于SSM(Spring+SpringMVC+Mybatis)的新闻管理系统源码+数据库.zip 基于SSM(Spring+SpringMVC+Mybatis)的新闻管理系统源码+数据库.zip 基于SSM...

    springmvc+mybatis+bootstrap框架+oracle数据库

    springmvc+mybatis+bootstrap框架+oracle数据库 1、兼容BootStrap,兼容Jquery UI。所以可以用bootstrap和jqueryui的功能。当然还有jquery了。 2、图标使用font awesome 3.2,可以使用字体图标 3、表格可以用...

    SpringMVC+MYBatis企业应用实战.pdf

    《SpringMVC+MYBatis企业应用实战》电子版,pdf文件。

    基于SpringMVC+Spring+MyBatis个人技术博客系统源码.zip

    基于SpringMVC+Spring+MyBatis个人技术博客系统源码.zip 完整代码,可运行 项目描述 基于SSM实现的一个个人博客系统,适合初学SSM和个人博客制作的同学学习。有了这个源码,直接买了阿里云或腾讯服务器,就可以部署...

    ZooKeeper+dubbo+springMvc+Mybatis+Mysql 例子

    ZooKeeper+dubbo+springMvc+Mybatis+Mysql实例,项目是由maven搭建的 整合Dubbo\spring\springMvc\Mybatis,整个压缩包中有两个项目分别是提供者和消费,启动方式是打成WAR形式放到tomcat中启动。

    图书管理系统SpringMvc+mybatis

    《图书管理系统SpringMvc+Mybatis实现详解》 在IT领域,构建高效、稳定的软件系统是至关重要的。本项目“图书管理系统”就是这样一个实例,它利用了SpringMvc和Mybatis两大主流框架,为图书管理提供了全面的解决...

    spring+springmvc+mybatis

    《Spring+SpringMVC+MyBatis:三位一体的Java企业级开发框架》 在Java企业级应用开发领域,Spring、SpringMVC和MyBatis是三个不可或缺的重要组件,它们共同构建了一个强大的、灵活的和可扩展的应用框架。这篇文章将...

    仓库管理系统,Spring+SpringMVC+Mybatis

    仓库管理系统,前端使用BootStrap+JQuery+JSP,后端使用Spring+SpringMVC+Mybatis,数据库使用MySQL,开发平台IntelliJ IDEA+open JDK1.8 amd64

    spring+springMVC+mybatis+quartz动态定时任务创建

    在IT行业中,构建高效、可扩展的Web应用是至关重要的,而"spring+springMVC+mybatis+quartz动态定时任务创建"就是一个常见的技术栈,用于实现这样的目标。这个组合充分利用了各组件的优势,提供了强大的后端服务支持...

    Spring+SpringMVC+MyBatis SSM框架整合工程实例 完整版源码.zip

    Spring+SpringMVC+MyBatis整合工程实例 完整版源码,这个SSM框架整合工程是基于IntelliJ IDEA完成的的,工程里面配置文件均有注释,可直接拷贝使用(工程代码可导入IDEA中直接运行),可供学习设计参考。

    springMVC+mybatis+shiro+redis 项目整合demo

    这个"springMVC+mybatis+shiro+redis 项目整合demo"就是一个实例,展示了如何将这些技术集成到一个项目中,以实现高效的数据处理、用户认证授权和缓存管理。 首先,`SpringMVC` 是 Spring 框架的一部分,它是一个...

    Springmvc+mybatis+spring 系统demo下载

    Springmvc+mybatis+spring 系统demo下载,基于myeclipse + tomcat 开发完成,下载后根据一份简单的使用说明就可以直接运行,代码实现简单的数据增删改查,希望给初学者参考

    基于SpringBoot+Spring+SpringMvc+Mybatis+Shiro+Redis 开发单点登录管理系统源码

    基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + ...

    Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统.rar

    项目描述 在上家公司自己集成的一套系统,用了两个多月的时间完成的:Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级开发系统 Springboot作为容器,使用mybatis作为持久层框架 使用官方推荐的thymeleaf做为...

    spring+springMvc+MyBatis+注解

    标题中的"spring+springMvc+MyBatis+注解"提到了四个关键点:Spring、SpringMVC、MyBatis以及注解。这四者构成了一个经典的Java Web开发框架组合,通常被称为SSM(Spring、SpringMVC、MyBatis)。下面将详细解释这些...

Global site tag (gtag.js) - Google Analytics