Mybatis整理系列(01)————傳入參數方式以及#{}與${}的區別
一、在MyBatis的select、insert、update、delete這些元素中都提到了parameterType這個屬性。MyBatis現在可以使用的parameterType有基本數據類型和JAVA復雜數據類型
- 基本數據類型:包含int,String,Date等。通過#{參數名},只能傳入一個參數;通過#{0}、#{1}……索引方式,可以傳入多個參數;如果通過#{參數名}傳多個值,又不想使用索引方式,可以使用@param()註解。
- 復雜數據類型:包含JAVA實體類、Map。通過#{屬性名}或#{map的KeyName}即可獲取傳入的值
1、#{參數名},傳入一個參數
DAO方法:
public List<User> selectUserByOrgId(String orgId);
- 1
Mapper.xml:
<select id="selectUserByOrgId" parameterType="java.lang.String" resultType="user">
select * from user where org_id = #{orgId}
</select>
- 1
- 2
- 3
service:
List<User> users = userDao.selectUserByOrgId("1");
- 1
2、#{0}、#{1}……索引方式,傳入多個參數
DAO方法:
public User selectUserByNameAndAge(String name,int age);
- 1
Mapper.xml:
<select id="selectUserByNameAndAge" resultType="user">
select * from user where name = #{0} and age = #{1}
</select>
- 1
- 2
- 3
service:
User user = userDao.selectUserByNameAndAge("lucy",18);
- 1
3、#{參數名},傳入多個參數,並且參數用@param註解
DAO方法:
public User selectUserByNameAndAge(@param("name")String name,@param("age")int age);
- 1
Mapper.xml:
<select id="selectUserByNameAndAge" resultType="user">
select * from user where name = #{name} and age = #{age}
</select>
- 1
- 2
- 3
service:
User user = userDao.selectUserByNameAndAge("lucy",18);
- 1
4、傳入多個基本類型參數,參數用map封裝,通過#{mapKey}取值
DAO方法:
public User selectUserByNameAndAge(Map map);
- 1
Mapper.xml:
<select id="selectUserByNameAndAge" parameterType="Map" resultType="user">
select * from user where name = #{name} and age = #{age}
</select>
- 1
- 2
- 3
service:
Map<String,Object> map=new HasMap<String,Object>();
map.put("name","lucy");
map.put("age",18);
User user = userDao.selectUserByNameAndAge(map);
- 1
- 2
- 3
- 4
5、使用map封裝實體類,通過通過#{mapKey.attributeName}取值
DAO方法:
public User selectUserByNameAndAge(Map map);
- 1
Mapper.xml:
<select id="selectUserByNameAndAge" parameterType="Map" resultType="user">
select * from user where name = #{userParam.name} and age = #{userParam.age}
</select>
- 1
- 2
- 3
service:
User userParam = new User("lucy",18);
Map<String,Object> map=new HasMap<String,Object>();
map.put("user",userParam);
User user = userDao.selectUserByNameAndAge(map);
- 1
- 2
- 3
- 4
- 5
6、直接傳入實體參數,通過#{屬性名}取值
DAO方法:
public User selectUserByNameAndAge(User userParam);
- 1
Mapper.xml:
<select id="selectUserByNameAndAge" parameterType="User" resultType="user">
select * from user where name = #{userParam.name} and age = #{userParam.age}
</select>
- 1
- 2
- 3
service:
User userParam = new User("lucy",18);
User user = userDao.selectUserByNameAndAge(userParam);
- 1
- 2
二、#{}與${}的區別
#{}
拿到值之後,拼裝sql,會自動對值添加引號” ${}
則把拿到的值直接拼裝進sql,如果需要加單引號”,必須手動添加,一般用於動態傳入表名或字段名使用,同時需要添加屬性statementType=”STATEMENT”,使用非預編譯模式。
註:statementType:STATEMENT(非預編譯),PREPARED(預編譯)或CALLABLE中的任意一個,這就告訴 MyBatis 分別使用Statement,PreparedStatement或者CallableStatement。默認:PREPARED。
使用${}傳參取值實例:
DAO方法:
public List<User> selectUserByOrgId(String orgId);
- 1
Mapper.xml:
<select id="selectUserByOrgId" parameterType="java.lang.String" resultType="user" statementType="STATEMENT">
select * from user where org_id = ${orgId}
</select>
- 1
- 2
- 3
service:
String orgId = "100";
orgId = "‘" + orgId + "‘";
List<User> users = userDao.selectUserByOrgId(orgId);
Mybatis整理系列(01)————傳入參數方式以及#{}與${}的區別