myBaits SQL

myBaits SQL

增:

void addService(Service service);

<insert id="addService" parameterType="com.dsl.entity.Service">
   insert into service_DSL (service_id,status,account_id,cost_id,unix_host,os_username,login_passwd)
   values (service_seq_DSL.nextval,1,#{account_id},#{cost_id},#{unix_host},#{os_username},#{login_passwd})
</insert>

 

void save(Cost cost);

<insert id="save" parameterType="com.dsl.entity.Cost">
<!-- #{}中的字符串是实体对象中的属性名,不是表中的字段名
   当传入参数为null时,MyBatis无法判断其类型,那么在处理参数值时就会有错误。
   要想解决这个问题,需要给这些可以为null的参数明确设置类型,格式是jdbcType=类型(大写)。
   注:一般表中字段太多了,挨个去判断是否可以为null麻烦,索性都写上jdbcType算了。-->
   insert into cost_DSL values(cost_seq_DSL.nextval,
                        #{name,jdbcType=VARCHAR},
                        #{base_duration,jdbcType=INTEGER},
                        #{base_cost,jdbcType=DOUBLE},
                        #{unit_cost,jdbcType=DOUBLE},
                        #{status,jdbcType=CHAR},
                        #{descr,jdbcType=VARCHAR},
                        #{creatime,jdbcType=TIMESTAMP},
                        #{startime,jdbcType=TIMESTAMP},
                        #{cost_type,jdbcType=CHAR})
</insert>

 

void addAdmin(Admin admin);

<insert id="addAdmin" parameterType="com.dsl.entity.Admin">
   <selectKey keyProperty="admin_id" order="BEFORE" resultType="Integer">
       select admin_seq_DSL.nextval from dual
    </selectKey>
   insert into admin_info_DSL (admin_id,admin_code,password,name,telephone,email)
   values (#{admin_id},#{admin_code},#{password},#{name},#{telephone},#{email})
</insert>

 

int insertSelective(User record);

<insert id="insertSelective" parameterType="com.cn.hnust.pojo.User" >
  insert into user_t
  <trim prefix="(" suffix=")" suffixOverrides="," >
    <if test="id != null" >
      id,
    </if>
    <if test="userName != null" >
      user_name,
    </if>
    <if test="password != null" >
      password,
    </if>
    <if test="age != null" >
      age,
    </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides="," >
    <if test="id != null" >
      #{id,jdbcType=INTEGER},
    </if>
    <if test="userName != null" >
      #{userName,jdbcType=VARCHAR},
    </if>
    <if test="password != null" >
      #{password,jdbcType=VARCHAR},
    </if>
    <if test="age != null" >
      #{age,jdbcType=INTEGER},
    </if>
  </trim>
</insert>

 

 

删:

void delete(Integer id);

<delete id="delete" parameterType="Integer">
   delete from cost_DSL where cost_id=#{id}
</delete>

 

 

 

改:

void modify(Cost cost);

<update id="modify" parameterType="com.dsl.entity.Cost">
   update cost_DSL set name=#{name},
                  cost_type=#{cost_type},
                  base_duration=#{base_duration,jdbcType=INTEGER},
                  base_cost=#{base_cost,jdbcType=DOUBLE},
                  unit_cost=#{unit_cost,jdbcType=DOUBLE},
                  descr=#{descr,jdbcType=VARCHAR}
    where cost_id=#{cost_id}
</update>

 

void modifyAccount(Account account);

<update id="modifyAccount" parameterType="com.dsl.entity.Account">
   update account_DSL
   <set>
      <if test='real_name!=null &amp;&amp; !real_name.equals("")'>
         real_name=#{real_name,jdbcType=VARCHAR},
      </if>
      <if test='login_passwd!=null &amp;&amp; !login_passwd.equals("")'>
         login_passwd=#{login_passwd,jdbcType=VARCHAR},
      </if>
      <if test='telephone!=null &amp;&amp; !telephone.equals("")'>
         telephone=#{telephone,jdbcType=VARCHAR},
      </if>
      <if test='recommender_id!=null &amp;&amp; !recommender_id.equals("")'>
         recommender_id=#{recommender_id,jdbcType=INTEGER},
      </if>
      <if test='occupation!=null &amp;&amp; !occupation.equals("")'>
         occupation=#{occupation,jdbcType=VARCHAR},
      </if>
      <if test='mailaddress!=null &amp;&amp; !mailaddress.equals("")'>
         mailaddress=#{mailaddress,jdbcType=VARCHAR},
      </if>
      <if test='zipcode!=null &amp;&amp; !zipcode.equals("")'>
         zipcode=#{zipcode,jdbcType=VARCHAR},
      </if>
      <if test='qq!=null &amp;&amp; !qq.equals("")'>
         qq=#{qq,jdbcType=VARCHAR},
      </if>
   </set> where account_id=#{account_id}
</update>

 

void resetPwd(Map<String,Object> map);

<update id="resetPwd" parameterType="hashmap">
   update admin_info_DSL set password=#{defaultPwd} where admin_id in
   <foreach collection="pwdList"
      item="pwd"
      open="("
      separator=","
      close=")">
      #{pwd}
   </foreach>
</update>

 

 

查:

Cost findById(Integer id);

<select id="findById" parameterType="Integer" resultType="com.dsl.entity.Cost">
   select * from cost_DSL where cost_id=#{id}
</select>

 

List<Account> findByPage(AccountPage page);

<select id="findByPage" parameterType="com.dsl.entity.page.AccountPage" resultType="com.dsl.entity.Account">
   select * from(select a.*,row_number() over(order by account_id)p from account_DSL a
   <where>
      <if test='idcardNo!=null &amp;&amp; !idcardNo.equals("")'>
         and idcard_no=#{idcardNo}
      </if>
      <if test='realName!=null &amp;&amp; !realName.equals("")'>
         and real_name=#{realName}
      </if>
      <if test='loginName!=null &amp;&amp; !loginName.equals("")'>
         and login_name=#{loginName}
      </if>
      <if test='status!=null &amp;&amp; !status.equals("") &amp;&amp; !status.equals("-1")'>
         and status=#{status}
      </if>
   </where>)where p between #{begin} and #{end}
</select>

 

List<Admin> findAdminByPage(AdminPage page);

<select id="findAdminByPage" parameterType="com.dsl.entity.page.AdminPage" resultMap="adminMap">
   select * from(select a.*,row_number() over(order by admin_id)p from admin_info_DSL a
   where admin_id in (
            select ai.admin_id from admin_info_DSL ai
            left join admin_role_DSL ar on ar.admin_id=ai.admin_id
            left join role_info_DSL ri on ri.role_id=ar.role_id
            left join role_module_DSL rm on rm.role_id=ri.role_id
            left join module_info_DSL mi on mi.module_id=rm.module_id
            <where>
               <if test='roleName!=null&amp;&amp;!roleName.equals("")'>
                  and ri.name like '%'||#{roleName}||'%'
               </if>
               <if test="moduleId!=null">
                  and mi.module_id=#{moduleId}
               </if>
            </where>
         )) where p between #{begin} and #{end}
</select>
<resultMap id="adminMap" type="com.dsl.entity.Admin">
   <id column="admin_id" property="admin_id"/>
   <collection select="findRoleByAdminId"
            column="admin_id"
            javaType="arraylist"
            ofType="com.dsl.entity.Role"
            property="roles">
   </collection>
</resultMap>
<select id="findRoleByAdminId" parameterType="Integer" resultType="com.dsl.entity.Role">
   select * from role_info_DSL where role_id in(select role_id from admin_role_DSL where admin_id=#{admin_id})
</select>

 

User selectByPrimaryKey(Integer id);

<resultMap id="BaseResultMap" type="com.cn.hnust.pojo.User" >
  <id column="id" property="id" jdbcType="INTEGER" />
  <result column="user_name" property="userName" jdbcType="VARCHAR" />
  <result column="password" property="password" jdbcType="VARCHAR" />
  <result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
  id, user_name, password, age
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  select
  <include refid="Base_Column_List" />
  from user_t
  where id = #{id,jdbcType=INTEGER}
</select>

 

转载于:https://my.oschina.net/dslcode/blog/1591219

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值