主工程模块yycgproject三层构建
来源:互联网 发布:js随机函数 编辑:程序博客网 时间:2024/06/06 03:10
基础架构:Springmvc+mybaits
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>
- 主工程模块yycgproject三层构建
- maven构建多模块工程
- Maven构建多模块Java工程
- maven 分模块 分工程构建项目
- IDEA多模块工程的构建
- 利用structs2 + felix 快速构建动态模块工程
- Maven Archetypes Part 3: 怎样构建一个多模块工程?
- 三层的构建
- 【三层架构】搜索模块
- 构建模块
- JSP/Servlet构建三层管理信息系统
- JSP/Servlet构建三层管理信息系统
- JSP/Servlet构建三层管理信息系统
- JSP/Servlet构建三层管理信息系统
- JSP/Servlet构建三层管理信息系统
- 用JSP构建三层管理信息系统
- Cmake构建Qt工程导致“模块计算机类型“X86”与目标计算机类型“x64”冲突
- ECLIPSE中MAVEN构建spring 多模块工程jar包不能自动扫描问题解决办法
- Docker 清理命令集锦
- php curl函数理解
- RedHad系统安装窗口界面开发库NEWT
- 仿iPhone滑动选择器(PickAddressAndBirth)
- Linux添加用户启动项
- 主工程模块yycgproject三层构建
- C#、VB.NET 使用System.Media.SoundPlayer播放音乐
- 初学C语言--字符串语法及函数
- Opencv3.0.0静态库调用必须opencv_ffmpeg300.dll的问题!
- 初学C语言----结构体
- Queue depth 增益(SSD/flash,etc)
- 初学C语言----枚举
- 【Android studio】Rendering Problems:classes could not be instantiated 解决方法
- 简单排列组合