配置:
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。