美文网首页
单表操作

单表操作

作者: 林深雾雨 | 来源:发表于2020-02-10 14:33 被阅读0次

mapper中的class文件中的方法名字和要对应的xml文件中的id要一致

mapper的xml中可以不指定传入参数类型 但是结果类型必须要写

静态SQL


多个参数模糊查询
///mapper中的Java文件代码j
public List<Customer> findCustomerByConditions(@Param("name")String username,@Param("jobs")String job);


//mapper中的xml文件代码
<select id="findCustomerByConditions" resultType="com.wk.po.Customer">
        select * from t_customer where username like "%"#{username}"%" and jobs = #{job};
</select>

在mapper的Java文件中不允许写同名的方法 (Mybatis判别不了)
@param映射名字可以随意设置

一个对象入参 对象的属性中有值
///mapper中的Java文件代码
public List<Customer> findCustomerByConditions2(Customer customer);

##在mapper的xml文件中 可以不写parameterType  上面的例子就没有写
<select id="findCustomerByConditions2" resultType="com.wk.po.Customer">
        select * from t_customer where username like "%"#{username}"%" and jobs = #{jobs};
</select>
//这里{}里面的名字要和po中的属性名一致  (默认参数绑定为对象的属性值)

需要在测试时将对象设置值 并传入对象

Customer customer2 = new Customer();    
customer2.setUsername("a");
customer2.setJobs("teacher");
List<Customer> list=customerMapper.findCustomerByConditions2(customer2);
Map入参 (当有多个值来自不同的对象 就不能放到同一个对象中 这时候需要用到map)
##mapper中的Java文件代码
public List<Customer> findCustomerByConditions3(Map<String,Object> map);

##在mapper的xml文件中 可以不写parameterType  上面的例子就没有写
<select id="findCustomerByConditions3" resultType="com.wk.po.Customer">
        select * from t_customer where username like "%"#{username_key}"%" and jobs = #{jobs_key};
    </select>
//(默认参数绑定为map集合中的key值)

##test中代码
HashMap<String,Object> map = new HashMap<>();
        map.put("username_key", "a");
        map.put("jobs_key", "teacher");
List<Customer> list=customerMapper.findCustomerByConditions3(map);
对于表的增删改统一的返回值为void
增加
##mapper中的Java文件代码
/*
     * insert 
 */
public void insertCustomer(Customer c) throws SQLException;
 
##xml中的代码
<insert id="insertCustomer">
        insert into t_customer values(null,#{username},#{jobs},#{phone});
              <selectKey keyProperty="id" order="AFTER" resultType="int">
               <!---id指的是将insert之后的id值插入到customer类中的id属性中 类型为int->
            select last_insert_id();
            <!-- SQL中select 和 from必须要  这个语句只是省略了from  -->
        </selectKey>
</insert>

##test中代码
Customer c = new Customer();
c.setJobs("solds"); 
c.setUsername("牛逼");
c.setPhone("12333231"); 
try {
    customerMapper.insertCustomer(c);
    sqlSession.commit();
System.out.println(c.getId());
System.out.println(c.getId());
} catch (SQLException e) {
    e.printStackTrace();
    sqlSession.rollback();
}   
sqlSession.close();
删除
/*
* delete
*/
       
public void deleteCustomer(Integer id) throws SQLException;

##xml中的代码
<delete id="deleteCustomer" parameterType="int">
       delete from t_customer where id = #{id};
</delete>

##test中代码
try {
           customerMapper.deleteCustomer(5);
           sqlSession.commit();
           
       } catch (SQLException e) {
           e.printStackTrace();
           sqlSession.rollback();
       }
       sqlSession.close();

##xml中的代码
<insert id="insertCustomer">
       insert into t_customer values(null,#{username},#{jobs},#{phone});
             <selectKey keyProperty="id" order="AFTER" resultType="int">
              <!---id指的是将insert之后的id值插入到customer类中的id属性中 类型为int->
           select last_insert_id();
           <!-- SQL中select 和 from必须要  这个语句只是省略了from  -->
       </selectKey>
</insert>

##test中代码
Customer c = new Customer();
c.setJobs("solds"); 
c.setUsername("牛逼");
c.setPhone("12333231"); 
try {
   customerMapper.insertCustomer(c);
   sqlSession.commit();
System.out.println(c.getId());
System.out.println(c.getId());
} catch (SQLException e) {
   e.printStackTrace();
   sqlSession.rollback();
}   
sqlSession.close();
更新
///mapper中的Java文件代码
/*
     * update
 */
    
public void updateCustomer(Customer c) throws SQLException;

##xml中的代码
<update id="updateCustomer" parameterType="com.wk.po.Customer">
    update t_customer set jobs = #{jobs} where id = #{id};
 </update>

##test中代码
Customer c = new Customer();
c.setId(4);
c.setJobs("2651616");
try {
   customerMapper.updateCustomer(c);
   sqlSession.commit(); 
  } catch (SQLException e) {
    e.printStackTrace();
    sqlSession.rollback();
}
    sqlSession.close();

动态SQL


##mapper中的Java文件
/*
* 动态查询
*/
public List<Customer> getCustomerDynamic(Customer c) throws SQLException;

##mapper中的xml文件
<select id="getCustomerDynamic" resultType="com.wk.po.Customer">
    select * from t_customer
    <where>
        <if test="id!=null">
            id = #{id}
        </if> 
         <if test="username!=null and username!=''">
            and username = #{username}
         </if>
        </where>
    order by username desc
</select>
##mapper中的Java文件
/*
    * 动态更新
*/
public void updateCustomerDynamic(Customer c) throws SQLException;

##mapper中的xml文件
<update id="updateCustomerDynamic">
    update t_customer 
    <set>
        <if test="username!=null and username!=''">
            username =#{username} ,
        </if>
    </set>
        
    <set>
        <if test="jobs!=null and jobs!=''">
            jobs =#{jobs} ,
        </if>
    </set>
        
    <set>
        <if test="phone!=null and phone!=''">
            phone =#{phone} ,
        </if>
    </set>
    where id = #{id}
</update>
##test中代码
CustomerMapper customerMapper=sqlSession.getMapper(CustomerMapper.class);
Customer c = new Customer();
c.setId(4);
c.setUsername("牛逼拉拉");
try {
    customerMapper.updateCustomerDynamic(c);
    sqlSession.commit();
  } catch (SQLException e) {
    e.printStackTrace();
    sqlSession.rollback();
}
sqlSession.close();

相关文章

  • 单表操作

    mapper中的class文件中的方法名字和要对应的xml文件中的id要一致 mapper的xml中可以不指定传入...

  • Python ORM 语句

    单表操作 多表操作(一对多): 多表操作(多对多): -------了不起的双下划线(__)之单表条件查询 ---...

  • MySQL学习笔记(三)修改操作、查询数据表

    操作数据表中的记录 插入操作 单表更新 单表删除 查询记录 where 条件表达式: 查询结果分组 GROUP B...

  • Oracle基础总结

    Oracle开发基础重点 Oracle一般操作 表空间 用户 表 表数据 Oracle 查询操作 单表查询 多表查...

  • 单表CRUD操作

    存储:把对象模型变成关系模型加载:关系模型变成对象模型(把数据库里的记录变成实体对象) 一、get和load区别 ...

  • Hibernate单表操作

    一、单一主键 1、两种主键生成策略 assigned:由java应用程序负责生成(手工赋值) native:又底层...

  • hibernate单表操作

    1.单一主键 assigned由java应用程序负责生成(手工赋值)(2)native 由底层数据库自动生成标示符...

  • springboot中通用mapper结合mybatis gen

    通用mapper是为了方便开发人员对单表进行crud操作而产生的一套通用方法。 通用mapper只支持单表操作,可...

  • mysql,库管理与表管理1

    关于数据库的操作 (以下操作均在MySQL中进行) 表管理 单表查询 有条件的查询:

  • 线性表

    1.线性表 1.1 顺序表(顺序存储) 静态分配 动态分配 1.2 单链表 单链表定义 不带头节点的单链表插入操作...

网友评论

      本文标题:单表操作

      本文链接:https://www.haomeiwen.com/subject/xlssnctx.html