配置:
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]
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。