单表操作

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();
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 11,165评论 0 4
  • MyBatis单表操作 前言 在前面一小节中,介绍了MyBatis以及MyBatis的简单操作,并且简单地分析了M...
    颜洛滨阅读 3,398评论 0 7
  • 关于MyBatis这里不再介绍,直接开始动手。编辑器:InteIIiJ IDEA, 数据库:mysql 1.新建一...
    忧郁的小码仔阅读 5,000评论 0 1
  • 1、什么是Mybatis? Mybatis是一个半ORM框架,封装了JDBC,开发时只要关注SQL语句本身,不需要...
    落地生涯阅读 8,687评论 0 1
  • 请来一场洗礼。 无论如何犀利, 或许令人忧伤。 我只不愿那颓废的表情, 再来占据我的视角。 我更不想这不温不火的风...
    1介书生阅读 3,813评论 4 8

友情链接更多精彩内容