實際上以前的示例使用的就是這種方法,今天介紹它的另一種寫法。還是以教師映射為例,修改映射文件TeacherMapper.xml如下(點擊此處進入嵌套resultMap形式的示例源碼下載頁面。注:本示例代碼是在修改本系列的上篇博文示例代碼的基礎上完成的,用到了MapperScannerConfigurer和注解等知識。對這些知識不熟悉的讀者,可參考上篇博文:http://legend2011.blog.51cto.com/3018495/980150):
<?xmlversion="1.0"encoding="utf8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--與以前一樣,namespace的值是對應的映射器接口的完整名稱-->
<mappernamespace="com.abc.mapper.TeacherMapper">
<!--TeacherMapper接口中getById方法對應的SQL語句。
查詢教師及其指導的學生的信息。由于教師、學生都有
id、name、gender等屬性,因此給教師的字段都起了別名-->
<selectid="getById"parameterType="int"resultMap="supervisorResultMap">
select t.id t_id, t.name t_name, t.gender t_gender,
t.research_area t_research_area, t.title t_title,
s.id,s.name, s.gender,s.major,s.grade
from teacher t,student s where t.id=#{id}
and s.supervisor_id = t.id
</select>
<!--教師實體映射-->
<resultMapid="supervisorResultMap"type="Teacher">
<idproperty="id"column="t_id"/>
<resultproperty="name"column="t_name"/>
<resultproperty="gender"column="t_gender"/>
<resultproperty="researchArea"column="t_research_area"/>
<resultproperty="title"column="t_title"/>
<!--需要注意的是,上面的select語句中學生的字段名/別名應與
下面的column屬性一致。ofType指collection包含的元素的類型,
此屬性不可少-->
<collectionproperty="supStudents"ofType="Student">
<idproperty="id"column="id"/>
<resultproperty="name"column="name"/>
<resultproperty="gender"column="gender"/>
<resultproperty="major"column="major"/>
<resultproperty="grade"column="grade"/>
<!--映射學生的指導教師屬性,用到了
supervisorResultMap本身-->
<associationproperty="supervisor"
resultMap="supervisorResultMap"/>
</collection>
</resultMap>
</mapper>
運行程序結果如下:
與以前的寫法相比,這種寫法的缺點是學生實體映射被嵌入到教師實體映射中,因此學生實體映射不能被重用。
二、嵌套的select語句
這種方式是使用一條單獨的select語句來加載關聯的實體(在本例中就是學生實體),然后在collection元素中引用此select語句(注:此方法會產生N+1問題,關于這個問題可參考本系列博客中的“MyBatis中的N+1問題”)。首先修改TeacherMapper.xml如下(點擊此處進入嵌套select語句形式示例源碼下載頁面):
<?xmlversion="1.0"encoding="utf8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--與以前一樣,namespace的值是對應的映射器接口的完整名稱-->
<mappernamespace="com.abc.mapper.TeacherMapper">
<!--TeacherMapper接口中getById方法對應的SQL語句。
查詢教師的信息。-->
<selectid="getById"parameterType="int"resultMap="supervisorResultMap">
select * from teacher where id=#{id}
</select>
<!--教師實體映射-->
<resultMapid="supervisorResultMap"type="Teacher">
<idproperty="id"column="id"/>
<resultproperty="name"column="name"/>
<resultproperty="gender"column="gender"/>
<resultproperty="researchArea"column="research_area"/>
<resultproperty="title"column="title"/>
<!--ofType指collection包含的元素的類型,此屬性不可少。
column屬性指把上述的getById的select語句中的教師id列的值作為參數
傳遞給將要引用到的下述的getStudents的select語句,此屬性不可少。
引用的形式為:命名空間.select語句id-->
<collectionproperty="supStudents"column="id"ofType="Student"
select="com.abc.mapper.StudentMapper.getStudents"/>
</resultMap>
</mapper>
在這里把根據指導教師id查詢學生信息的SQL語句寫在StudentMapper.xml中,并引用其中的學生實體映射studentResultMap。修改StudentMapper.xml如下:
<?xmlversion="1.0"encoding="utf8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="com.abc.mapper.StudentMapper">
<resultMapid="studentResultMap"type="Student">
<idproperty="id"column="id"/>
<resultproperty="name"column="name"/>
<resultproperty="gender"column="gender"/>
<resultproperty="major"column="major"/>
<resultproperty="grade"column="grade"/>
<!--在這里引用supervisorResultMap和getById,亦采用
命名空間名.相關元素id的形式。column="supervisor_id"
屬性不可少-->
<associationproperty="supervisor"
resultMap="com.abc.mapper.TeacherMapper.supervisorResultMap"
select="com.abc.mapper.TeacherMapper.getById"column="supervisor_id"/>
</resultMap>
<!--根據指導教師id查詢學生信息-->
<selectid="getStudents"parameterType="int"
resultMap="studentResultMap">
select * from student where supervisor_id = #{id}
</select>
</mapper>
執行結果如下:
最近在工作中遇到了一個需求
在執行數據庫操作時需要先判斷指定的數據是否存在,如果不存在則插入,存在則更新
最開始使用的是三條SQL語句:
后來leader提示還有新的方法,一條SQL語句就能搞定:
后來在網上看到的,執行update語句的條件是insert語句的執行會造成唯一鍵的重復。
所以,在創建表的時候還要加上唯一鍵的約束:
這樣就能達到目的。
大家都在為項目開發成功而喜悅,但可不知成功的路上是會經常出錯的,下面是我碰到的一些錯誤集合!
【錯誤信息】
01-16 17:16:18.945: I/magh(979): org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1:8080 refused
在android模擬器連接本機訪問web時報這錯,把127.0.0.1改成localhost也是一樣的
原因:【錯誤信息】
java.lang.IllegalArgumentException: The key must be an application-specific resource id.
原因及解決辦法:
mRadioButton.setTag(1,sQuestionItem.get(i).getToNext());//設置監聽 ToNext:下 一題目mRadioButton.setTag(2,sQuestionItem.get(i).getToEnd());//設置監聽 ToEnd: 是否終止 拋出IllegalArgumentException的原因就在于key不唯一,正確代碼如下:
mRadioButton.setTag(R.id.tag_tonext,sQuestionItem.get(i).getToNext());// 設置監聽 ToNext:下一題目 mRadioButton.setTag(R.id.tag_toend,sQuestionItem.get(i).getToEnd());//設置 監聽 ToEnd:是否終止
【錯誤信息】
點擊Debug 運行 結果模擬器總是會彈出Waiting for Debugger 然后程序又可以正常運行
如果你想調試的時候去掉 Waiting for Debugger 提示
原因及解決辦法:
重啟啟動機器就OK
本文出自 “java之路” 博客,請務必保留此出處http://2402766.blog.51cto.com/2392766/1102373
管理提醒: 本帖被 kasim 執行置頂操作(2010-04-11)
管理提醒: 本帖被 kasim 執行加亮操作(2010-04-11)
首先說一下Json數據的最基本的特點,Json數據是一系列的鍵值對的集合,和XML數據來比,Json數據的體積更加小,傳輸效率高,易解析,不過可讀性不高;
因為這次要從服務器端得到Json數據,并且通過解析之后把解析后的數據顯示在Android客戶端中,首先部署服務器端代碼(直接使用Jsp/Servlet):
構造的Json數據如下:
[{"name":"張三","address":"北京","age":20},{"name":"李四","address":"上海","age":30},{"name":"王五","address":"深圳","age":35}]
[一]服務器端(Person.java省略):
①:數據構造JsonService.java
main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/iv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/longshuai" <!-- longshuai.png為圖片的名稱,記在資源文件里頭,不用文件名后綴-->
/>
</LinearLayout>
注意:強調一下,資源文件的圖片命名規則比較嚴格,由[a-z]和數字和“_”組成,而且不能數字開頭,我就常犯傻,命名老是數字或者大寫字母開頭,這種錯誤——囧。。
我們要把longshuai.png導入到res中,最簡單的方式就是直接找到這個文件夾,復制進去
之后右鍵更新,我們就可以在res中看到自己的圖片了
不用寫代碼。。直接用自動生成的代碼。。
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
效果如下:
2、加載本地圖片(其實主要是SdCard中圖片)
關于SdCard的使用,可以參見 http://longshuai2007.blog.163.com/blog/static/1420944142011611103950500/
xml文件同上面的是一樣的,并不需要修改
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image1 = (ImageView) findViewById(R.id.iv1); //獲得ImageView對象
/*為什么圖片一定要轉化為 Bitmap格式的!! */
Bitmap bitmap = getLoacalBitmap("/sdcard/tubiao.jpg"); //從本地取圖片(在cdcard中獲取) //
image1 .setImageBitmap(bitmap); //設置Bitmap
}
/**
* 加載本地圖片
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis); ///把流轉化為Bitmap圖片
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
顯示效果如下: