您好,登錄后才能下訂單哦!
配置:
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中不需要設(shè)定 -->
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標(biāo)簽:指定相應(yīng)的Java接口 Mapper.java
insert標(biāo)簽:
<insert id="insert" parameterType="com.dig4j.lsh.model.callIn.LshCustProfileInfo" useGeneratedKeys="true" keyProperty="ccId" >
</insert>
<!-- useGeneratedKeys="true"把新增加的主鍵賦值到自己定義的keyProperty(id)中 -->
delete標(biāo)簽:
<delete id="deleteByXXX" parameterType="Long" >
</delete>
select標(biāo)簽:
<select id="selectXXXList" parameterType="XXXBean" resultType="XXXBean" >
</select>
update標(biāo)簽:
<update id="updateXXXByXXX" parameterType="XXXBean" >
</update>
if標(biāo)簽:
<if test="list != null">
AND xxx IN ('XXX')
</if>
foreach標(biāo)簽:
<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>
錯(cuò)誤:
MyBatis 3.3.1 批量插入多行回寫自增id
mybatis Parameter 'marge_id' not found. Available parameters are [list]
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。