【Java Web】【5】SSM spring+struts+mybatis整合(二)

1.    spring作为容器,整合struts和mybatis
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="   
          http://www.springframework.org/schema/beans    
          http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
          http://www.springframework.org/schema/aop    
          http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
          http://www.springframework.org/schema/tx    
          http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	<!-- 组织机构业务 -->
	<bean id="companyDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
        <property name="mapperInterface" value="com.shdl.hterp.business.company.CompanyDao" />  
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    <bean id="companyService" class="com.shdl.hterp.business.company.CompanyServiceImpl">
		<property name="companyDao" ref="companyDao"></property>
	</bean>
</beans>
2.     mybatisti提供持久层服务 
<?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"> 

<mapper namespace="com.shdl.hterp.business.company.CompanyDao">
  
    <select id="getList" resultType="companyBean" parameterType="companyBean">
		select *  from  (
			select M.*,ROWNUM RN from (
		       select 
		       	c.*
		       from companyinfo c
		       where deletetime is null 
		       <if test="qm != null and qm != ''">
			       	<if test="qm.companyname != null and qm.companyname != ''">
		       			AND companyname like '%${qm.companyname}%'
	       			</if>
	       			<if test="qm.companytype != null and qm.companytype != ''">
		       			and companytype = #{qm.companytype}
		      		</if>
				</if>
		       order by companyorderno
			)M 
		)N
		where N.RN > #{start} and N.RN <= #{end}
	</select>
   <!--  更改sql:目前会导致查询条件下出现分页不对的现象 -->
    <select id="getListCount" parameterType="companyBean" resultType="int">
	   SELECT count(*) FROM companyinfo where deletetime is null
	   		<if test="qm != null and qm != ''">
		       	<if test="qm.companyname != null and qm.companyname != ''">
       				AND companyname like '%${qm.companyname}%'
       			</if>
       			<if test="qm.companytype != null and qm.companytype != ''">
		       			and companytype = #{qm.companytype}
		      	</if>
			</if>
	</select>
	
	<update id="updateByField" parameterType="hashmap">
		UPDATE companyinfo SET companycode = companycode
		<foreach collection="updatelist" item="u">
			<choose>
				<when test="u.name == 'deletetime'">
					, ${u.name} = ${u.value}
				</when>
				<otherwise>
					, ${u.name} = #{u.value}
				</otherwise>
			</choose>
		</foreach>
		WHERE 1 = 1
		<foreach collection="wherelist" item="w">
			AND ${w.name} = #{w.value}
		</foreach>
	</update>
	
	<select id="infoByField" parameterType="hashmap" resultType="companyBean">
		SELECT 
			l.*
		FROM companyinfo l
		WHERE deletetime is null AND companytype = '项目公司'
		<if test="name != null and name != ''">
			AND ${name} = #{value}
		</if>
	</select>
	
	<select id="info" parameterType="String" resultType="companyBean">
		SELECT 
			l.*
		FROM companyinfo l
		WHERE deletetime is null and companyid = #{value}
	</select>
	
	<!-- 新建公司信息-->
	<insert id="add" parameterType="companyBean">
		INSERT INTO companyinfo (
			companyid, companyname, companycode, 
			companyaddress, companyaccount, companytel,
			companyremark, companytype, companyorderno
		)VALUES(
			sys_guid(), #{companyname}, #{companycode}, 
			#{companyaddress}, #{companyaccount}, #{companytel},
			#{companyremark}, #{companytype}, #{companyorderno}
		)
	</insert>
	
	<update id="modify" parameterType="companyBean">
		UPDATE companyinfo SET
			companyid = #{companyid}, 
			companyname = #{companyname}, 
			companycode = #{companycode}, 
			companyaddress = #{companyaddress}, 
			companyaccount = #{companyaccount}, 
			companytel = #{companytel},
			companyremark = #{companyremark},
			companytype = #{companytype}, 
			companyorderno = #{companyorderno}
		WHERE
			companyid = #{companyid}
	</update>
	
	<!-- 获取项目公司 -->
	<select id="getProduct" resultType="companyBean">
		SELECT * FROM companyinfo WHERE deletetime is null AND companytype = '项目公司'
	</select>
    
</mapper>
3.     struts:action和mmybatis结合
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.i18n.encoding" value="UTF-8"/>  
	<package name="company" extends="struts-default">
	   	<action name="company_*" class="com.shdl.hterp.business.company.CompanyAction" method="{1}">
	  	</action>
	</package>
</struts>

      

    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值