mybatis入門

配置:

Mybatis配置分兩部分,java與sql

Sql:

<!-- mybatis文件配置,掃描所有mapper.xml文件 -->

    <bean id="sqlSessionFactory"

          class="org.mybatis.spring.SqlSessionFactoryBean"

          p:dataSource-ref="dataSource"

          p:configLocation="classpath:/mybatis/mybatis-config.xml" <!-- mybatis自身的配置,spring mvc中不需要設定 -->

          p:mapperLocations="classpath:/mapper/*.xml"/> <!-- mybatis的xml文件位置 -->

    Java:

<!-- spring與mybatis整合配置,掃描所有mapper.java -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"

        p:basePackage="com.xxx.mapper" <!-- mybatis的java接口文件位置 -->

        p:sqlSessionFactoryBeanName="sqlSessionFactory"/>


使用:

mapper標籤:指定相應的Java接口 Mapper.java

insert標籤:

  <insert id="insert" parameterType="com.dig4j.lsh.model.callIn.LshCustProfileInfo" useGeneratedKeys="true" keyProperty="ccId" >

  </insert>

   <!-- useGeneratedKeys="true"把新增加的主鍵賦值到自己定義的keyProperty(id)中 -->

delete標籤:

  <delete id="deleteByXXX" parameterType="Long" >

  </delete>

select標籤:

  <select id="selectXXXList" parameterType="XXXBean" resultType="XXXBean" >

  </select>

update標籤:

  <update id="updateXXXByXXX" parameterType="XXXBean" >

  </update>

if標籤:

<if test="list != null">

AND xxx IN ('XXX')

</if>

foreach標籤:

<foreach collection="list" item="item" index="index"  open="(" separator="," close=")">  

#{item} 

</foreach>

例子:

	<mapper namespace="XXXMapper">
	  <!-- 插入 -->
	  <insert id="insert" parameterType="com.dig4j.lsh.model.callIn.LshCustProfileInfo" useGeneratedKeys="true" keyProperty="ccId" >
	    INSERT INTO
	      XXX
	    (
	      remark,
	      create_date,
	      update_date
	    ) VALUES (
	      #{remark},
	      DATE_ADD(now(),INTERVAL 8 HOUR),
	      DATE_ADD(now(),INTERVAL 8 HOUR)
	    )
	  </insert>
	  
	  <insert id="insertBatch" parameterType="list">
	    INSERT INTO
	      XXX
	    (
	      remark,
	      create_date,
	      update_date
	    ) 
	    VALUES
		<foreach collection="list" item="item" index="index" separator=",">
		(
			#{item.remark,jdbcType=VARCHAR},
			DATE_ADD(now(),INTERVAL 8 HOUR),
			DATE_ADD(now(),INTERVAL 8 HOUR)
		)
		</foreach>
	  </insert>
	  
	  <delete id="deleteByXXX" parameterType="Long" >
	    DELETE
	    FROM
			XXX
	    WHERE
			XXX = #{XXX}
	  </delete>
	  
	  <update id="updateXXXByXXX" parameterType="XXXBean" >
	    UPDATE
	      XXX
	    SET
	      date = DATE_ADD(now(),INTERVAL 8 HOUR)
	    WHERE
			XXX = #{XXX}
	  </update>
	  
	  <select id="selectCount" parameterType="XXXBean" resultType="int" >
	    SELECT
	      COUNT(0)
	    FROM
	    WHERE
			XXX = #{XXX}
	  </select> 
	  
	  <select id="selectXXXList" parameterType="XXXBean" resultType="XXXBean" >
	    SELECT 
	    FROM
	    WHERE
	      1 = 1
	      <if test="XXX != null and XXX != ''">
	        AND XXX = #{XXX}
	      </if>     
	  </select>
	
	  <select id="selectXXXList" parameterType="java.util.List" resultType="XXXBean" >
		SELECT
			XXX
		FROM
			XXX
		WHERE
		  1=1
		<if test="list != null">
			AND xxx IN
			<foreach collection="list" item="item" index="index"  open="(" separator="," close=")">  
				#{item} 
			</foreach>
		</if>
	  </select>
	</mapper>

        

錯誤:

MyBatis 3.3.1 批量插入多行回寫自增id

mybatis Parameter 'marge_id' not found. Available parameters are [list]


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章