配置:
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]
转载于:https://blog.51cto.com/xinzhilian/2088630