一、加载jar包
<!--mybatis配置--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency>
二、配置文件
<!--读取db.properties文件--> <!--<context:property-placeholder location="classpath:db.properties"/>--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:db.properties"/> </bean> <!--配置dbcp连接池--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="initialSize" value="1"/> <property name="maxActive" value="2"/> <property name="maxIdle" value="1"/> <property name="minIdle" value="0"/> <property name="maxWait" value="60000"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.test.common.model,com.test.open.model,com.test.secure.model"/> <property name="mapperLocations" value="classpath:com/test/**/mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.test.**.mapper"/> </bean>
三、mapper文件节点分析
<?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="cn.tisson.ipran.open.authentication.persistence.SysUserMapper"></mapper><resultMap id="sysUserResultMap" type="cn.tisson.ipran.open.authentication.domain.SysUser"> <id property="userId" column="USER_ID" jdbcType="VARCHAR"/> <result property="userName" column="USER_NAME" jdbcType="VARCHAR"/> <result property="password" column="PASSWORD" jdbcType="VARCHAR"/> <result property="email" column="EMAIL" jdbcType="VARCHAR"/> <result property="isValid" column="IS_VALID" jdbcType="CHAR"/> <result property="createDate" column="CREATE_DATE" jdbcType="DATE"/> <result property="updateDate" column="UPDATE_DATE" jdbcType="DATE"/> <result property="passErrorCount" column="PASSERRORCOUNT" jdbcType="NUMERIC"/> <association property="sysRole" column="ROLE_ID" javaType="SysRole" select="getSysRoleById" /> <collection property="sysRights" column="ROLE_ID" ofType="SysRight" select="getSysRightsByRoleId"/> </resultMap>
<select id="countSysScript" resultType="int" parameterType="cn.tisson.ipran.secure.sysautojob.domain.SysScriptBean"> select count(1) from ( <include refid="sql"/><include refid="whereSql"/>)</select><sql id="whereSql"> where 1=1 <if test="actionId != null and actionId != ''"> and s.action_id = #{actionId} </if> order by s.action_id,s.save_date </sql>
<insert id="addSysScript" parameterType="cn.tisson.ipran.secure.sysautojob.domain.SysScriptBean"> insert into ACTION_MODEL_SCRIPT_RELATION (biz_type,action_id, action_sub_id,model_id, script_id,des, net_type, save_date) values (#{bizTypeId,jdbcType=VARCHAR},#{actionId,jdbcType=VARCHAR},#{actionSubId,jdbcType=VARCHAR}, #{modelId,jdbcType=VARC HAR}, #{scriptId,jdbcType=VARCHAR}, #{scriptDescribe,jdbcType=VARCHAR}, #{netType,jdbcType=VARCHAR}, sysdate) </insert>
<delete id="deleteSysScript" parameterType="cn.tisson.ipran.secure.sysautojob.domain.SysScriptBean"> delete from res_tunnel_biz_script where action_id=#{actionId,jdbcType=VARCHAR} and model_id=#{modelId,jdbcType=VARCHAR} and action_sub_id= #{actionSubId,jdbcType=VARCHAR} </delete><update id="updateSysScript" parameterType="cn.tisson.ipran.secure.sysautojob.domain.SysScriptBean"> update ACTION_MODEL_SCRIPT_RELATION <set> des = #{scriptDescribe,jdbcType=VARCHAR} </set> where biz_type=#{bizTypeId,jdbcType=VARCHAR} and action_id=#{actionId,jdbcType=VARCHAR} and model_id=#{modelId,jdbcType=VARCHAR} and action_sub_id= #{actionSubId,jdbcType=VARCHAR} </update>
四、分页节点
<select id="selectPageNewSysScript" resultMap="BaseResultMap" parameterType="cn.tisson.ipran.secure.sysautojob.domain.SysScriptBean"> <include refid="cn.tisson.ipran.common.page.persistence.NumberMapper.Pagination_Head_Clause"/> <include refid="newSql"/> <include refid="cn.tisson.ipran.common.page.persistence.NumberMapper.Pagination_Tail_Clause"/> </select>
<?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="cn.tisson.ipran.common.page.persistence.NumberMapper"> <sql id="Pagination_Head_Clause"> <if test="fromNumber != null and toNumber != null"> <![CDATA[select * from ( select tdata.*, ROWNUM ROWNUM_ from ( ]]> </if> </sql> <sql id="Pagination_Tail_Clause" > <if test="fromNumber != null and toNumber != null"> <![CDATA[) tdata where rownum <= #{toNumber} ) where rownum_ > #{fromNumber}]]> </if> </sql> </mapper>