Mybatis基礎: 常見問題與FAQ,mybatisfaq

來源:互聯網
上載者:User

Mybatis基礎: 常見問題與FAQ,mybatisfaq
Mybatis基礎: #{...} 和 ${...} 的區別MyBatis將 #{…} 解釋為JDBC prepared statement 的一個參數標記。而將 ${…} 解釋為字串替換。理解這兩者的區別是很有用的, 因為在某些SQL語句中並不能使用參數標記(parameter markers)。

比如,我們不能在表名(table name)的位置使用參數標記。
假設有下面的代碼:

Map<String, Object> parms = new HashMap<String, Object>();parms.put("table", "foo"); // 表名parms.put("criteria", 37); // 查詢過濾條件List<Object> rows = mapper.generalSelect(parms);
<select id="generalSelect" parameterType="map">  select * from ${table} where col1 = #{criteria}</select>

MyBatis產生的SQL語句(prepared statement)如下所示:
select * from foo where col1 = ?

重要提示: 請注意,使用 $ {…} (字串替換)時可能會有SQL注入攻擊的風險。另外,字串替換在處理複雜類型也可能常常發生問題,如日期類型。由於這些因素,我們建議您儘可能地使用 #{…} 這種方式。

要使用LIKE語句該怎麼寫?


有兩種使用LIKE的方法。(推薦使用)第一種方法是,在Java代碼中添加SQL萬用字元。

樣本一:
String wildcardName = "%Smi%";List<Name> names = mapper.selectLike(wildcardName);
<select id="selectLike">  select * from foo where bar like #{value}</select>

第二種方式是在SQL語句中拼接萬用字元。這種方法相對來說安全性要低一些,因為可能會被SQL注入攻擊。
樣本二:
String wildcardName = "Smi";List<Name> names = mapper.selectLike(wildcardName);
<select id="selectLike">  select * from foo where bar like '%' || '${value}' || '%'</select>

重要提示: 請注意兩種方式中 $# 的使用!

如何執行批量插入?
首先,建立一個簡單的insert語句:
<insert id="insertName">  insert into names (name) values (#{value})</insert>

然後在Java代碼中像下面這樣執行批處理插入:
List<String> names = new ArrayList<String>();names.add("Fred");names.add("Barney");names.add("Betty");names.add("Wilma");// 注意這裡 ExecutorType.BATCHSqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);try {  NameMapper mapper = sqlSession.getMapper(NameMapper.class);  for (String name : names) {    mapper.insertName(name);  }  sqlSession.commit();} finally {  sqlSession.close();}

如何擷取自動產生的(主)索引值?
insert 方法總是返回一個int值 - 這個值代表的是插入的行數。而自動產生的索引值在 insert 方法執行完後可以被設定到傳入的參數對象中。
樣本:
<insert id="insertName" useGeneratedKeys="true" keyProperty="id">  insert into names (name) values (#{name})</insert>
Name name = new Name();name.setName("Fred");int rows = mapper.insertName(name);// 完成後,id已經被設定到對象中System.out.println("rows inserted = " + rows);System.out.println("generated key value = " + name.getId());

在mapper中如何傳遞多個參數?Java的反射機制並不能讓架構擷取到參數的名字(方法簽名中只有參數類型,可以說是為了最佳化,也可以說設計就是如此,總之名字無意義), 所以MyBatis預設的命名為: param1,param2……
如果想給他們指定名稱,可以使用 @param 註解:
import org.apache.ibatis.annotations.Param;public interface UserMapper {   User selectUser(@Param("username") String username,                    @Param("hashedPassword") String hashedPassword);}

然後,就可以在xml像下面這樣使用(推薦封裝為一個Map,作為單個參數傳遞給Mapper):
<select id=”selectUser” resultType=”User”>  select id, username, hashedPassword  from some_table  where username = #{username}  and hashedPassword = #{hashedPassword}</select>

原文連結: What is the difference between #{...} and ${...}?

原文日期: 2013-11-10
翻譯日期: 2014-09-28
翻譯人員: 鐵錨


spring+mybatis架構從基礎到深入講解

深?怎麼深,有多深?你有多長,就有多深。
 
spring與mybatis整合出現下面的錯誤

檢查 這個classpath:mybatis-config.xml 裡面涉及到的XML 檔案是否有問題
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.