主工程模块yycgproject三层构建
2015-10-25 17:43
603 查看
基础架构:Springmvc+mybaits
配置文件
1. Log4j.properties—》日志配置文件
Db.properties—-》连接数据库 配置连接数据库的参数
Mybatis/SqlMapConfig.xml—》 mybatis的核心配置文件 Mybatis配置项
Spring/applicationContext.xml -spring的核心配置文件 配置公用的内容:数据源、事务管理
(加载数据库配置文件 、 数据库连接池、 事务管理器 )
Spring/applicationContext- base-dao.xml 配置dao 配置SqlSessionFactory,dao(mapper)
( 加载数据源 配置SqlMapConfig.xml 配置 mapper自动扫描器和SqlSessionFactory(bean名称就是mapper类型(首字母小写))
Spring/applicationContext-base-service.xml - 配置service 配置业务接口
Spring/Springmvc.xml—》配置springmvc
组件扫描(扫描所有@controller标记类 即扫描action包)
处理器映射器
处理器适配器
视图解析器
拦截器
….
1.1.2 整合Dao:
达到目标:将spring和mybatis整合,将spring管理dao接口。
让spring对SqlSessionFactory管理,再由SqlSessionFactory自动生成sqlSession,SqlSession就是dao接口中要用的。
配置:applicationContext.xml
配置applicationContext-base-dao.xml
1.1.2..1 Spring和mybatis整合,dao开发方法1:
程序员创建dao接口及dao接口的实现类,
Dao接口实现类继承SqlSessionDaoSupport
在spring容器中配置dao接口,并将SqlSessionFactory注入到dao接口实现中,
在dao接口方法中调用this.getSqlSession()获取SqlSession。
1.1.2..2 Mapper动态代理方法:
程序员只需要编写mapper接口(相当于dao接口),不需要编写 mapper接口的实现类,mybatis提供根据mapper接口和mapper.xml(映射文件)生成mapper接口动态代理对象(mapper接口的实现)。
具备什么规则生成mapper代理对象:
Mapper.xml中的namespace等于mapper接口的地址。
Mapper.xml中定义的sql的id(mapped statement的id)等于mapper.java中方法名
Mapper.xml中定义的statement的parametertype等于mapper.java中方法的形参类型。
Mapper.xml中定义的statement的resultType等于mapper.java中方法的返回值类型。
开发两个文件:mapper.java和mapper.xml
方式1:使用MapperFactoryBean
在spring容器中进行配置mapper:
使用MapperFactoryBean,根据mapper接口生成代理对象。
注意:mapper.xml和mapper.java同名且在一个目录 ,不需要在SqlMapConfig.xml中加载mapper文件。
测试方法:
获得spring容器,从容器中得sysuserCustomMapper
使用MapperFactoryBean需要在spring容器对每个mapper进行配置,麻烦。
方式2:使用mapper自动扫描器(推荐)
使用mybaits和spring整合包中提供的mapper扫描器,自动扫描mapper,生成动态代理对象,在spring容器注册。
配置mapper自动扫描器
开发两个文件:mapper.java和mapper.xml,注意:mapper.xml和mapper.java同名且在一个目录
测试:
获得spring容器,从容器中得SysuserCustomMapper
本系统采用扫描器方法创建mapper对象。
1.1.3 整合service
整合目标:
让spring统一管理service接口,在service中调用mapper。
在service层实现事务控制,使用声明式事务配置方法
编写service 接口和实现类:
applicationContext-service.xml配置:
1.1.4 整合action
整合目标:
在action中调用service接口,从service中获取数据传到页面。
实现系统首页(使用jquery Easyui)。
配置:Springmvc.xml
Base:业务基础模块
主要就是系统管理功能配置文件
1. Log4j.properties—》日志配置文件
Db.properties—-》连接数据库 配置连接数据库的参数
Mybatis/SqlMapConfig.xml—》 mybatis的核心配置文件 Mybatis配置项
Spring/applicationContext.xml -spring的核心配置文件 配置公用的内容:数据源、事务管理
(加载数据库配置文件 、 数据库连接池、 事务管理器 )
Spring/applicationContext- base-dao.xml 配置dao 配置SqlSessionFactory,dao(mapper)
( 加载数据源 配置SqlMapConfig.xml 配置 mapper自动扫描器和SqlSessionFactory(bean名称就是mapper类型(首字母小写))
Spring/applicationContext-base-service.xml - 配置service 配置业务接口
Spring/Springmvc.xml—》配置springmvc
组件扫描(扫描所有@controller标记类 即扫描action包)
处理器映射器
处理器适配器
视图解析器
拦截器
….
1.1.2 整合Dao:
达到目标:将spring和mybatis整合,将spring管理dao接口。
让spring对SqlSessionFactory管理,再由SqlSessionFactory自动生成sqlSession,SqlSession就是dao接口中要用的。
配置:applicationContext.xml
<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" 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-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" > <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 开发阶段建议最大连接数据尽量少,够用即可 --> <property name="maxActive" value="${jdbc.maxActive}"/> <property name="maxIdle" value="${jdbc.maxIdle}"/> </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="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- 切面 --> <aop:config proxy-target-class="true"> <aop:advisor advice-ref="txAdvice" pointcut="execution(* yycg.*.service.impl.*.*(..))"/> </aop:config> </beans>
配置applicationContext-base-dao.xml
<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" 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-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-- 配置SqlSessionFactory 从spring和mybatis的整合包中获取 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载数据源 --> <property name="dataSource" ref="dataSource"/> <!-- 配置SqlMapConfig.xml --> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/> </bean> <!-- 使用MapperFactoryBean 生成mapper的代理对象 在mybatis和spring的整合包中 --> <!-- <bean id="sysuserCustomMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 配置mapper接口 <property name="mapperInterface" value="yycg.base.dao.mapper.SysuserCustomMapper"/> 配置sqlSessionFactory <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> --> <!--配置 mapper自动扫描器 bean名称就是mapper类型(首字母小写) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 配置扫描包路径 ,如果扫描多个包路径,中间使用半角逗号分隔--> <property name="basePackage" value="yycg.base.dao.mapper"/> <!-- 配置SqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
1.1.2..1 Spring和mybatis整合,dao开发方法1:
程序员创建dao接口及dao接口的实现类,
Dao接口实现类继承SqlSessionDaoSupport
在spring容器中配置dao接口,并将SqlSessionFactory注入到dao接口实现中,
在dao接口方法中调用this.getSqlSession()获取SqlSession。
1.1.2..2 Mapper动态代理方法:
程序员只需要编写mapper接口(相当于dao接口),不需要编写 mapper接口的实现类,mybatis提供根据mapper接口和mapper.xml(映射文件)生成mapper接口动态代理对象(mapper接口的实现)。
具备什么规则生成mapper代理对象:
Mapper.xml中的namespace等于mapper接口的地址。
Mapper.xml中定义的sql的id(mapped statement的id)等于mapper.java中方法名
Mapper.xml中定义的statement的parametertype等于mapper.java中方法的形参类型。
Mapper.xml中定义的statement的resultType等于mapper.java中方法的返回值类型。
开发两个文件:mapper.java和mapper.xml
方式1:使用MapperFactoryBean
在spring容器中进行配置mapper:
使用MapperFactoryBean,根据mapper接口生成代理对象。
注意:mapper.xml和mapper.java同名且在一个目录 ,不需要在SqlMapConfig.xml中加载mapper文件。
测试方法:
获得spring容器,从容器中得sysuserCustomMapper
使用MapperFactoryBean需要在spring容器对每个mapper进行配置,麻烦。
方式2:使用mapper自动扫描器(推荐)
使用mybaits和spring整合包中提供的mapper扫描器,自动扫描mapper,生成动态代理对象,在spring容器注册。
配置mapper自动扫描器
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 配置扫描包路径 ,如果扫描多个包路径,中间使用半角逗号分隔--> <property name="basePackage" value="yycg.base.dao.mapper"/> <!-- 配置SqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
开发两个文件:mapper.java和mapper.xml,注意:mapper.xml和mapper.java同名且在一个目录
测试:
获得spring容器,从容器中得SysuserCustomMapper
本系统采用扫描器方法创建mapper对象。
1.1.3 整合service
整合目标:
让spring统一管理service接口,在service中调用mapper。
在service层实现事务控制,使用声明式事务配置方法
编写service 接口和实现类:
applicationContext-service.xml配置:
<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" 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-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-- 用户管理 --> <bean id="userService" class="yycg.base.service.impl.UserServiceImpl"/> </beans>
1.1.4 整合action
整合目标:
在action中调用service接口,从service中获取数据传到页面。
实现系统首页(使用jquery Easyui)。
配置:Springmvc.xml
<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" 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-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-- 组件扫描 扫描所有标记@Controller类,由于使用自动扫描所以action类不用在spring配置文件中配置 --> <context:component-scan base-package="yycg.**.action" /> <!-- 处理器映射器和适配器,可以使用mvc注解驱动 --> <mvc:annotation-driven/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 将jstl的jar包加入工程,默认支持jstl --> <!-- 前缀和后缀可以去掉的,为了方便开发才加的 --> <property name="prefix" value="/WEB-INF/jsp" /> <property name="suffix" value=".jsp" /> </bean> </beans>
相关文章推荐
- 深入浅析mybatis oracle BLOB类型字段保存与读取
- 浅析Mybatis 在CS程序中的应用
- Java Mybatis框架入门基础教程
- Java简单实现SpringMVC+MyBatis分页插件
- mybatis的动态sql详解(精)
- MyBatis入门学习教程(一)-MyBatis快速入门
- 解决springmvc+mybatis+mysql中文乱码问题
- Spring+Mybatis+Mysql搭建分布式数据库访问框架的方法
- SpringMVC+MyBatis项目总结(一)
- SpringMVC+MyBatis项目总结(二)
- MyBatis返回结果不稳定
- MyBatis通过反射建立一个对象的过程。
- springMVC+spring+Mybatis
- SpringMVC+mybatis+Spring框架整合+简单实现的demo
- Spring+mybatis+shiro+freemarker+ehcache+ldap+mongo
- 继续钟爱JdbcTemplate
- 基于全注解的Spring3.1 mvc、myBatis3.1、Mysql的轻量级项目
- Mybatis的几点小结
- 懒到极致:对mybatis的进一步精简