Passando più argomenti in un SELECT senza l'utilizzo di un oggetto complesso
Domanda
Sto cercando di passare startSequenceId
, stopSequenceId
, orderNumber
nella mappa SQL, però, non voglio usare un oggetto tipizzato, cioè parameterType="com.abc.Order"
, posso farlo?
<select id="getSequenceIdByOrderNumber" parameterType="?" resultType="int">
select *
from log
where seq_id
between #{startSequenceId} and #{stopSequenceId}
and order_no = #{orderNumber}
and rownum = 1
</select>
Soluzione
@Chin vi posterò ciò che avevo digitato in ogni caso con un semplice esempio se avete trovato quello che cercate. Il mio esempio utilizzando iBatis 2.3.4
<select id="retrieveTestXXX" parameterClass="java.util.Map" resultClass="java.lang.Integer">
SELECT
example_table.id
FROM example_table
WHERE example_table.xx_id = #testId# AND example_table.xx_id = #test2Id#
</select>
Spero che questo aiuti.
Altri suggerimenti
È possibile utilizzare il costruito nel tipoParametro 'mappa' ad esempio
Map<String, Object> parms = new HashMap<String, Object>();
parms.put("name", "abc");
parms.put("phone", "123");
parms.put("email", "123@email.com");
List<Contact> list = myBatis.selectList("getContacts",parms);
<!-- in xml mapper -->
<select id="getContacts" parameterType="map" resultMap="Contact">
SELECT * FROM CONTACT
WHERE CONTACT_NAME = ${name}
AND CONTACT_PHONE = ${phone}
AND CONTACT_MAIl = ${email}
</select>
In MyBatis 3, è possibile utilizzare @Param
annotazione nella classe mapper (interfaccia) Metodo:
public getSequenceIdByOrderNumber(@Param("seqId") int sequenceId,@Param("orderId") int orderNo);
Quindi è possibile utilizzare #{seqId}
, #{orderId}
in SQL senza utilizzare l'attributo tipoParametro.
posso fare commenti,
Trovato la risposta, grazie.
http://code.google.com/p/mybatis/wiki/HowToSelectMultipleParams
questo link è rotto, attualmente corretta è https://github.com/mybatis/mybatis-3/wiki/FAQ#how-do-i-use-multiple-parameters-in-a-mapper
copia + incolla da questa wiki
Java riflessione non fornisce un modo per conoscere il nome di un parametro di metodo in modo MyBatis nomi li per default come: param1, param2 ... Se si vuole dare loro un nome usare l'annotazione @param in questo modo:
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
User selectUser(@Param("username") String username, @Param("hashedPassword") String hashedPassword);
}
Ora è possibile utilizzarli nella vostra xml come segue:
<select id=”selectUser” resultType=”User”>
select id, username, hashedPassword
from some_table
where username = #{username}
and hashedPassword = #{hashedPassword}
</select>
tl; dr
quando si dichiara il metodo nell'interfaccia invece di struttura di Java Standard
Type methodName (
ParamType1 paramName1,
ParamType2 paramName2,
… ); (this is interface so no method body)
uso (aggiungere @param)
Type methodName (
@Param("name1 available in xml") ParamType1 paramName1,
@Param("name2 available in xml") ParamType2 paramName2,
…); (this is still interface so no method body)
Trovato la risposta, grazie.
http://code.google.com/p/mybatis/wiki/HowToSelectMultipleParams