0.包结构:
配置文件结构:
1.spring配置文件
applicationContext-dao.xml (配置连接池,mybatis会话工厂,扫描mybatis文件的包,扫描action包与扫描service包,事务的控制)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 0.连接池属性设置读取指定的properties文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 1.将连接池放入spring容器 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="driverClass" value="${jdbc.driver}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--2. 配置 Mybatis的会话工厂 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> <!-- 配置Mybatis的核心 配置文件所在位置 --> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> </bean> <!-- 3.1 mapper代理配置方法一 这种方法需要大量重复的配置代理对象 MapperFactoryBean:根绝mapper接口生成代理对象 <bean id="selectUser" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="cn.qlq.core.dao.SelectUser"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> --> <!-- 3.2通过MapperScannerConfigurer扫描进行批量生成代理对象 遵循规范:mapper.java和mapper.xml名字一样且在同一个目录下 自动扫描出来的代理对象的id为mapper类类名(首字母小写) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定扫描的包名,如果有多个,用半角逗号分隔 --> <property name="basePackage" value="danger.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 4.配置事务管理器 --> <!-- 事务核心管理器,封装了事务操作,依赖于连接池 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 5.开启注解管理aop事务 --> <tx:annotation-driven /> <!-- 与struts2整合的配置 --> <!-- 扫描Action基本包 --> <context:component-scan base-package="danger.action"></context:component-scan> <!-- 扫描service --> <context:component-scan base-package="danger.service"></context:component-scan> <!-- 事务模板对象,依赖于事务核心管理器 --> <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"></property> </bean> <!-- ················开始使用XML管理事务························ --> <!-- 配置事务通知(无论哪种方式都要用到事务的核心管理器) --> <tx:advice transaction-manager="transactionManager" id="firstTx"> <tx:attributes> <!--以方法为单位,指定方法应用事务什么属性 isolation:隔离级别 read-only:只读属性 propagation:传播行为 --> <!-- 企业中运用通配符命名规则。两套增删改查(8种) --> <tx:method name="save*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" /> <tx:method name="persist*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" /> <tx:method name="delete*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" /> <tx:method name="remove*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" /> <tx:method name="update*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" /> <tx:method name="modify*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" /> <tx:method name="get*" isolation="DEFAULT" read-only="true" propagation="REQUIRED" /> <tx:method name="find*" isolation="DEFAULT" read-only="true" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 配置织入 --> <aop:config> <!-- 配置切点表达式 --> <aop:pointcut expression="execution(* cn.xm.exam.service.*.*.*ServiceImpl.*(..))" id="texPc" /> <!-- 配置切面:切点+通知 advice-ref:通知名称 pointcut-ref:切点名称 --> <aop:advisor advice-ref="firstTx" pointcut-ref="texPc" /> </aop:config> </beans>
2.struts配置:
struts.xml (主配置文件,需要放在src下)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="devMode" value="true"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.action.extension" value="action" /> <constant name="struts.objectFactory" value="spring"></constant> <!-- 解决还乱全被struts拦截没匹配的action --> <package name="error" extends="struts-default"> <default-action-ref name="notFound" /> <action name="notFound" class="danger.action.sys.ErrorAction"> <result>/404.jsp</result> </action> </package> <!-- 乔利强写的 --> <include file="struts/struts_json.xml"></include> <include file="struts/queryView.xml"></include> <!-- 马非写的 --> <include file="struts/struts_rechecktable.xml"></include> <include file="struts/struts_sys.xml"></include> </struts>
struts_sys.xml (普通struts配置)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="user" extends="struts-default" namespace="/"> <action name="user_*" class="userAction" method="{1}"> <result name="login_success">/main/hdReview.jsp</result> <result name="login_error">/login/login.jsp</result> </action> </package> </struts>
struts_json.xml (JSON格式的配置)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true"></constant> <package name="json_package" extends="json-default"> <!-- 查询隐患详细信息 --> <action name="queryDetailInfo" class="danger.action.queryView.QueryDetailInfo"> <result name="success" type="json"> <param name="root">result</param> </result> </action> <!-- 查询统计隐患信息 --> <action name="queryDangerTongji" class="danger.action.queryView.TongjiDangerAction"> <result name="success" type="json"> <param name="root">result</param> </result> </action> </package> </struts>
3.mybatis配置
sqlMapConfig.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> <!-- 只需要定义个别名,这个应该有 --> <typeAliases> <package name="danger.mapper" /> </typeAliases> <!-- 动态代理也不需要配置这个扫描,留着也行 --> <mappers> <!-- 原始DAO开发使用这个手动加载xml --> <package name="danger.mapper" /> </mappers> </configuration>
4.使用方法:
mapper层
接口与xml放在同一包下
接口:
package danger.mapper.dangerManage.custom; import java.sql.SQLException; import java.util.List; import java.util.Map; import danger.bean.dangerManage.Danger; public interface DangerCustomMapper { /** * 组合条件查询隐患 * * @param map * @return * @throws Exception */ public List<Danger> findDangerByCondition(Map<String, Object> map) throws SQLException; /** * 根据条件查询满足条件的总数 * * @param map * @return * @throws Exception */ public Integer getDangerCountByCondition(Map<String, Object> map) throws SQLException; /** * 统计信息的时候根据开始时间和结束时间,单位和级别,类型查询满足条件的总数 * * @param map * 开始时间,结束时间,单位,级别,类型查询 * @return * @throws SQLException */ public Integer getDangerCountByCondition2(Map<String, Object> map) throws SQLException; /** * 统计信息的时候根据开始时间和结束时间,单位和级别,类型查询满足条件的记录 * * @param map * 开始时间,结束时间,单位,级别,类型查询 * @return * @throws Exception */ public List<Danger> findDangerByCondition2(Map<String, Object> map) throws SQLException; }
xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离 注意:使用mapper代理方法开发,namespace有特殊重要的作用 --> <mapper namespace="danger.mapper.dangerManage.custom.DangerCustomMapper"> <!-- 查询分页 --> <sql id="query_page_limit"> <if test="index!=null"> LIMIT #{index},#{currentCount} </if> </sql> <!-- 查询隐患的条件 --> <sql id="query_danger_where"> <if test="checkunit!=null"> and checkunit=#{checkunit} </if> <if test="manager!=null"> and manager=#{manager} </if> <if test="startTime!=null"> and findTime > #{startTime} </if> <if test="endTime!=null"> and findTime<#{endTime} </if> <if test="dangergrade!=null"> and dangergrade=#{dangergrade} </if> <if test="address!=null"> and address like ‘%${address}%‘ </if> <if test="content!=null"> and content like ‘%${content}%‘ </if> <if test="type!=null"> and type=#{type} </if> <if test="dangerstatus!=null"> and dangerstatus=#{dangerstatus} </if> </sql> <select id="findDangerByCondition" resultType="danger.bean.dangerManage.Danger" parameterType="hashmap"> SELECT * FROM `danger`.`danger` <where> <include refid="query_danger_where"></include> </where> <include refid="query_page_limit"></include> </select> <select id="getDangerCountByCondition" resultType="int" parameterType="hashmap"> SELECT count(dangerId) FROM `danger`.`danger` <where> <include refid="query_danger_where"></include> </where> </select> </mapper>
2.service使用
实现类上声明service层,内部注入mapper。接口声明service也可以@service("queryService") 相当于给service起个名字,注入可以用autowired,也可以用resource
3.action层使用(声明action,注入service对象,action要是多例)
时间: 2024-11-07 16:49:19