mybatis執行sql腳本
A. java使用mybatis執行sql腳本,怎麼獲取sql腳本的查詢結果
<selectid="DAO介面方法名稱"parameterType="參數類型"resultType="返回結果類型">
select*from表where。。。
</select>
resultType 可以是任意Object對象,如果多條數據,這這個方法返回的是List<Object?>,
如果確認是單條數據,可以直接 Object? ***(**); 。
沒有封裝成對象時,默認返回的是List<Map<欄位名稱String,列值Object>>這樣的數據。
Dao介面:
List<Map<String,Object>>list(Integerid);
SQL:
<selectid="list"parameterType="Integer"resultType="Map">
select*fromaaa
<where>
<iftest="null!=id">
id>#{id}
</if>
</where>
</select>
以上示例中表示查詢id>某個數值的所有結果,返回類型為MAP
執行腳本後沒有返回結果的吧,看ScriptRunner源碼,沒有提供任何返回結果的。
privatevoidexecuteStatement(Stringcommand)throwsSQLException,UnsupportedEncodingException{
booleanhasResults=false;
Statementstatement=connection.createStatement();
statement.setEscapeProcessing(escapeProcessing);
Stringsql=command;
if(removeCRs)
sql=sql.replaceAll(" "," ");
if(stopOnError){
hasResults=statement.execute(sql);
}else{
try{
hasResults=statement.execute(sql);
}catch(SQLExceptione){
Stringmessage="Errorexecuting:"+command+".Cause:"+e;
printlnError(message);
}
}
printResults(statement,hasResults);
try{
statement.close();
}catch(Exceptione){
//
}
}
...
有結果時,最後調用了這個方法列印出來而已。
privatevoidprint(Objecto){
if(logWriter!=null){
logWriter.print(o);
logWriter.flush();
}
}
你可以調用
publicvoidsetLogWriter(PrintWriterlogWriter){
this.logWriter=logWriter;
}
傳入你自己的Writer。
B. mybatis級聯查詢,sql語句是怎麼執行的
你的語句拼錯了,密碼的查詢前面多了一個where,應該是
1
String
sql
=
"select
*
from
users
where
readername='"+name+"'"+"and
password='"+password+"'";
C. mybatis 怎麼執行sql語句
只要在一個sqlSession里就可以執行多個SQL語句,不知道你具體想要什麼效果?
D. java使用mybatis執行sql腳本,怎麼獲取sql腳本的結果
<select id="DAO介面方法名稱" parameterType="參數類型" resultType="返回結果類型">
select * from 表 where 。。。
</select>
resultType 可以是任意Object對象,如果多條數據,這這個方法返回的是List<Object?>,
如果確認是單條數據,可以直接 Object? ***(**); 。
沒有封裝成對象時,默認返回的是List<Map<欄位名稱String,列值Object>>這樣的數據。
Dao介面:
List<Map<String,Object>> list(Integer id);
SQL:
<select id="list" parameterType="Integer" resultType="Map">
select * from aaa
<where>
<if test="null!=id">
id >#{id}
E. mybatis執行SQL查不到數據,在資料庫執行可以查到
datasource或者jdbc中的配置url不對 , 設置編碼格式,表創建時候的編碼格式需要在url中聲明,並且傳入的數據也應該是相同編碼格式的(這個沒注意,一直用的utf8).
比如 你創建一張表 使用的是engine innodb charset utf8;那麼你的數據源url連接就應該是 jdbc:mysql://(資料庫地址+埠)/(資料庫名)?useUnicode=true&characterEncoding=utf8(可能還會有別的參數需要指定);
究其原因 還是因為編碼格式的問題,你的李四 到資料庫的前,會在myabtis做一些處理,做這些處理的時候,李四就可能不在是李四了 所以使用mybatis查詢不到,但是在mysql中直接查詢是查詢得到的
F. mybatis 怎麼寫sql語句
mybatis的sql和你在資料庫客戶端執行的sql是一樣的,但是在mybatis中調用的sql一般都是動態的,所以用到了參數傳遞。這個mybatis有對應的標簽以及相應的變數來實現。你可以搜索下mybatis標簽。同時給你一個參考的你看看,這個是一個查詢用戶的
<select
id="queryUsers"
parameterType="map"
resultType="xx.xx.xx.bean.UserBean">
<![CDATA[
select
ID,
LOGIN_NAME
AS
loginName,
PASSWORD,
REAL_NAME
AS
realName,
POSITION,
(SELECT
D.POSITION_NAME
FROM
UNIT_POSITION
D
WHERE
D.POSITION_CODE=T.POSITION)
POSITIONNAME,
USER_TYPE
AS
userType,
SEX,
PID,
TO_CHAR(T.BIRTHDAY,'YYYY-MM-DD')
BIRTHDAY,
EMAIL,
CONTACT_TEL
AS
contactTel,
CONTACT_MOBILE
AS
contactMobile,
CONTACT_FAX
AS
contactFax,
CONTACT_ZIP
AS
contactZip,
CONTACT_ADDR
AS
contactAddr,
STATUS,
EDUCATION,
(SELECT
D.EDUCATION_NAME
FROM
UNIT_EDUCATION
D
WHERE
D.EDUCATION_CODE=T.EDUCATION
AND
D.STATUS=0)
EDUCATIONNAME,
NATION,
POLITICAL,
REMARK,
TO_CHAR(T.CREATE_DATE,'YYYY-MM-DD
HH24:MI:SS')
createDate,
(SELECT
D.REAL_NAME
FROM
UNIT_USER
D
WHERE
D.ID=
T.CREATE_USER_ID)
createUserId,
TO_CHAR(T.UPDATE_DATE,'YYYY-MM-DD
HH24:MI:SS')
updateDate,
(SELECT
D.REAL_NAME
FROM
UNIT_USER
D
WHERE
D.ID=
T.UPDATE_USER_ID)
updateUserId
from
UNIT_USER
T
]]>
<where>
T.STATUS='1'
<if
test="realName
!=null
and
realName
!=''">
and
T.REAL_NAME
like
'%${realName}%'
</if>
<if
test="nexusDpartment
!=null">
AND
T.ID
IN
(SELECT
DISTINCT
D.USER_ID
FROM
UNIT_USER_DEPT
D
WHERE
D.DEPT_CODE
IN
(${nexusDpartment}))
</if>
<if
test="deptCode
!=null
and
deptCode
!=''">
AND
T.ID
IN
(SELECT
DISTINCT
D.USER_ID
FROM
UNIT_USER_DEPT
D
WHERE
D.DEPT_CODE
=
#{deptCode})
</if>
</where>
<if
test="sort
!=
null
and
sort
!=
''">
order
by
${sort}
<if
test="direction
!=
null
and
direction
!=
''">
${direction}
</if>
</if>
</select>
G. 如何用java的mybatis執行一連串sql語句
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.sql.Statement;publicclassxxxx{publicstaticvoidmain(String[]args){Connectioncon=null;Statementstmt=null;tr
H. mybatis怎麼執行sql語句值
例如在一個 XXMapper.xml 中:
?
1
2
3
<select id="executeSql" resultType="map">
${_parameter}
</select>
你可以如下調用:
?
1
sqlSession.selectList("executeSql", "select * from sysuser where enabled = 1");
或者你可以在 XXMapper.java 介面中定義如下方法:
?
1
List<Map> executeSql(String sql);
然後使用介面調用方法:
?
1
xxMapper.executeSql("select * from sysuser where enabled = 1");
上面這些內容可能都會,下面在此基礎上再復雜一點。
假如像上面SQL中的enabled = 1我想使用參數方式傳值,也就是寫成 enabled = #{enabled},如果你沒有遇到過類似這種需求,可能不明白為什麼要這么寫,舉個例子,要實現一種動態查詢,可以在前台通過配置 SQL,提供一些查詢條件就能實現一個查詢的功能(為了安全,這些配置肯定是開發或者實施做的,不可能讓用戶直接操作資料庫)。
針對這個功能,使用 MyBatis 實現起來相當容易。配置 SQL 肯定要執行,用上面講的這種方式肯定可以執行 SQL,如何提供參數呢?參數就是enabled = #{enabled}中的#{enabled}部分。如果再多一些條件,一個配置好的 SQL 如下:
?
1
2
3
select * from sysuser
where enabled = #{enabled}
and userName like concat('%',#{userName},'%')
這種情況下,該怎麼用 MyBatis 實現呢?
首先 XML 中修改如下:
?
1
2
3
<select id="executeSql" resultType="map">
${sql}
</select>
介面中的方法修改為:
?
1
List<Map> executeSql(Map map);
然後調用方法:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
Map map = new HashMap();
//這里的 sql 對應 XML 中的 ${sql}
map.put("sql", "select * from sysuser "
+ " where enabled = #{enabled} "
+ " and userName like concat('%',#{userName},'%')");
//#{enabled}
map.put("enabled", 1);
//#{userName}
map.put("userName", "admin");
//介面方式調用
List<Map> list = xxMapper.executeSql(map);
//sqlSession方式調用
I. MyBatis如何執行超復雜的sql語句
返回值無法固定,是否可以考慮分情況執行不同的sql語句了,
所謂的超復雜的sql就算能寫出來,如果有問題,排查起來是不是也不容易,還有性能,
以後的維護數據邏輯太復雜,表也太大,這樣寫運行速度要快一些