关于Mybatis的一些随笔

2019-04-28 08:22:14来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Mapper.xml头文件

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

Mybatis配置头文件

<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">

Mybatis配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    <settings>
        <!-- 设置resultMap的自动映射级别 -->
        <setting name="autoMappingBehavior" value="FULL"/>
        <!-- 设置全局性延迟加载 -->
        <setting name="lazyLoadingEnabled" value="false"/>
    </settings>
    
    <!-- 设置实体类别名 -->
    <typeAliases>
        <package name="cn.xin.pojo" />
    </typeAliases>
    
    
</configuration>

Sql映射文件

<?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="cn.xin.dao.user.UserMapper">
     <select id="queryLogin" resultType="User">
         select * from smbms_user where userCode=#{userCode}
     </select>
     
     <select id="userCount" resultType="int">
         select count(*) from smbms_user,smbms_role 
         where smbms_user.userRole=smbms_role.id
         <if test="userName!=null and userName!=''">
             and smbms_user.userName=#{userName}
         </if>
         <if test="roleName!=null and roleName!=''">
             and smbms_role.roleName=#{roleName}
         </if>
     </select>
     
     <select id="getUserList" resultMap="userlist">
         select * from smbms_user,smbms_role where smbms_user.userRole=smbms_role.id
         <trim suffixOverrides="and">
             <if test="userName!=null and userName!=''">
                 and smbms_user.userName=#{userName}
             </if>
             <if test="roleName!=null and roleName!=''">
                 and smbms_role.roleName=#{roleName}
             </if>
         </trim>
         order by smbms_user.id limit #{from},#{pageSize}
     </select>
     <resultMap type="UserList" id="userlist">
         <id property="id" column="id"/>
         <result property="userCode" column="userCode"/>
         <association property="role" javaType="Role">
             <id property="id" column="r_id"/>
             <result property="roleName" column="roleName"/>
         </association>
     </resultMap>
     
     <insert id="addUser" parameterType="User">
         insert into smbms_user(userCode,userName,userPassword,gender,birthday,phone,address,userRole,idPicPath,workPicPath)
         value(#{userCode},#{userName},#{userPassword},#{gender},#{birthday},#{phone},#{address},#{userRole},#{idPicPath},#{workPicPath}) 
     </insert>
     
     <select id="getUserById" resultType="User">
         select * from smbms_user where id=#{id};
     </select>
     
     <update id="modifyUserById" parameterType="User">
         update smbms_user 
         set userName=#{userName},gender=#{gender},birthday=#{birthday},phone=#{phone},address=#{address},userRole=#{userRole}
         where id=#{id}
     </update>
     
     <select id="queryUserById" parameterType="int" resultType="User">
         select * from smbms_user where id=#{id}
     </select>
     
     <delete id="delUserById" parameterType="int">
         delete from smbms_user where id=#{id}
     </delete>
     
     <select id="getUserByuserCode" >
         select * from smbms_user where userCode=#{userCode}
     </select>
     
 </mapper>

Mybatis随笔

1.多参数入参:
  Mapper接口参数:(@Param("id")Integer id,@Param("password")String pwd);Mapper.xml无需填写parameterType
2.@Param注解:
  Mybatis的参数类型为Map,使用@Param注解的参数,会记录指定的参数名为key;否则默认为"param"+序号作为Map的key,这种情况可能会引起sql语句中获取不到#{参数名},从而报错
3.resultMap的作用:
  描述数据库结果集合对象的对应关系,属性名与字段名不同的匹配、association处理一对一关联关系、collection处理一对多关联关系、自动映射级别:NONE:禁止自动匹配,PARTIAL(默认):自动匹配所有属性,有内部嵌套的除外,FULL:自动匹配所有属性,包括内部嵌套
4.动态SQL:
  if+where、if+trim、if+set、foreach、choose(when/otherwise)、where 1=1、分页:oreder by id limit #{start}},#{count}(查询结果根据id排序,从start开始,每次显示5个查询结果)
 foreach:迭代对象为数组,collection=array、迭代对象为List,collection=list、迭代对象为Map,collection=key
select * from tb_member where id in <foreach collection="array" item="id" open="(" separator="," close=")">#{id}</foreach>

 


原文链接:https://www.cnblogs.com/Hotaru-Xin/p/10778570.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Redis主从同步要深入理解?一篇文章足矣!

下一篇:支撑千万级,大型电商分布式架构解析