Mybatis整理系列(01)————传入参数方式以及#{}与${}的区别

时间:2022-03-30 05:08:32

一、在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);