單表操作

mapper中的class文件中的方法名字和要對應(yīng)的xml文件中的id要一致

mapper的xml中可以不指定傳入?yún)?shù)類型 但是結(jié)果類型必須要寫

靜態(tài)SQL


多個參數(shù)模糊查詢
///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映射名字可以隨意設(shè)置

一個對象入?yún)? 對象的屬性中有值
///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中的屬性名一致  (默認參數(shù)綁定為對象的屬性值)

需要在測試時將對象設(shè)置值 并傳入對象

Customer customer2 = new Customer();    
customer2.setUsername("a");
customer2.setJobs("teacher");
List<Customer> list=customerMapper.findCustomerByConditions2(customer2);
Map入?yún)? (當有多個值來自不同的對象 就不能放到同一個對象中 這時候需要用到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>
//(默認參數(shù)綁定為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);
對于表的增刪改統(tǒng)一的返回值為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();

動態(tài)SQL


##mapper中的Java文件
/*
* 動態(tài)查詢
*/
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文件
/*
    * 動態(tài)更新
*/
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();
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. 簡介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優(yōu)秀的...
    笨鳥慢飛閱讀 6,238評論 0 4
  • MyBatis單表操作 前言 在前面一小節(jié)中,介紹了MyBatis以及MyBatis的簡單操作,并且簡單地分析了M...
    顏洛濱閱讀 540評論 0 7
  • 關(guān)于MyBatis這里不再介紹,直接開始動手。編輯器:InteIIiJ IDEA, 數(shù)據(jù)庫:mysql 1.新建一...
    憂郁的小碼仔閱讀 1,307評論 0 1
  • 1、什么是Mybatis? Mybatis是一個半ORM框架,封裝了JDBC,開發(fā)時只要關(guān)注SQL語句本身,不需要...
    落地生涯閱讀 3,517評論 0 1
  • 請來一場洗禮。 無論如何犀利, 或許令人憂傷。 我只不愿那頹廢的表情, 再來占據(jù)我的視角。 我更不想這不溫不火的風(fēng)...
    1介書生閱讀 676評論 4 8

友情鏈接更多精彩內(nèi)容