《Mybatis编程自学与面试指南》01-10:映射器Mapper

《Mybatis编程自学与面试指南》01-10:映射器Mapper


内容导航

  • 前言
  • 1、包
  • 2、XxxMapper.xml

前言

在MyBatis框架中,dao的实现通过配置mapping映射文件来完成。


1、包

src源码文件夹下创建mapping包,参考如下:

  • com.company.project.mapping

  • com.aitegu.project.mapping

  • com.goldentec.project.mapping

  • com.goldenunion.project.mapping

  • net.csdn.project.mapping

或者

  • com.company.project.mapping

  • com.aitegu.project.mapping

  • com.goldentec.project.mapping

  • com.goldenunion.project.mapping

  • net.csdn.project.mapping


2、XxxMapper.xml

2.1、根节点


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
						 "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<!-- 配置接口和映射器的关联关系 -->
<mapper namespace="com.company.project.dao.PlatUserMapper">

	

</mapper>

2.2、配置结果视图


	<!-- 配置结果视图 -->
	<resultMap type="PlatUser" id="baseResultSet">
		<id  column="PLATUSERID" property="platUserID" jdbcType="VARCHAR"/>
	
		<result column="LOGINNAME" property="loginName" jdbcType="VARCHAR" />
		<result column="CRYPTOGRAM" property="cryptogram" jdbcType="VARCHAR" />
		<result column="WECHATID" property="wechatID" jdbcType="VARCHAR" />
		<result column="EMAIL" property="email" jdbcType="VARCHAR" />
		
	</resultMap>

2.3、配置可复用SQL


	<!-- 配置可复用SQL -->
	<sql id="base_column_list">
		  platUserID , loginName , cryptogram , wechatID , email  
	</sql>

2.4、配置插入


	<!-- 插入 -->
	<insert id="insert" parameterType="PlatUser">
		insert into PlatUser values(
			#{platUserID , jdbcType=VARCHAR},
			#{loginName , jdbcType=VARCHAR},
			#{cryptogram , jdbcType=VARCHAR},
			#{wechatID , jdbcType=VARCHAR},
			#{email , jdbcType=VARCHAR}
		)
	
	</insert>

2.5、配置更新


	<!-- 更新 -->
	<update id="update" parameterType="PlatUser">
		update PlatUser  set  
			loginName = #{loginName , jdbcType=VARCHAR},
			cryptogram = #{cryptogram , jdbcType=VARCHAR},
			wechatID = #{wechatID , jdbcType=VARCHAR},
			email = #{email , jdbcType=VARCHAR}
			
		Where  platUserID = #{platUserID , jdbcType=VARCHAR}
	</update>

2.6、配置删除


	<!-- 删除 -->
	<delete id="delete" parameterType="java.lang.String">
		
		delete PlatUser 
		Where  platUserID = #{platUserID , jdbcType=VARCHAR}
	
	</delete>

2.7、配置主键查询


	<!-- 通过主键查询 -->
	<select id="findById" parameterType="java.lang.String" resultMap="baseResultSet">
		select <include refid="base_column_list"/>    
		
		from  PlatUser
		
		Where platUserID = #{platUserID , jdbcType=VARCHAR}
	
	</select>

2.8、配置查询所有


	<!-- 查询所有 -->
	<select id="findAll"  resultMap="baseResultSet">
		select  <include refid="base_column_list"/>    
		from  PlatUser
	
	</select>


上一篇:获取文件数据流+叠加byte数组(给byte数组加包头包尾)


下一篇:windows下Logstash6.5.3版本读取文件输入不生效、配置elasticsearch模板后数据入es报错:Rejecting mapping update to [hello-world-