1、传入多个参数到sql映射文件
注意:
①多个参数会封装成一个map,如果没有指定的话sql映射文件中参数的值应写为#{param1}/#{param2}…
②如果不指定而且不把映射文件中的参数值写为#{paramX},就会报org.apache.ibatis.binding.BindingException
2、通过POJO:传入多个参数到sql映射文件
使用POJO指定参数key的步骤:
①在Mapper类的对应方法的入参前面 使用@Param(“xx”)修饰入参 如:public Info getByMutiParams(@Param(“id”)int id,@Param(“name”)String name);
②在sql映射文件的sql语句中,使用@Param指定的key来作为sql参数的key 如:select * from info where id = #{id} and name=#{name}
3、通过Map:传入多个参数到sql映射文件
使用Map指定参数key的步骤:
①在Mapper类的方法中使用Map类型来定义入参
②sql映射文件的sql中,直接使用#{“xxx”}
③在调用映射类方法的时候定义一个Map类型参数,对应sql映射文件中的#{“xxx”}来指定Map数据的key
4、传入参数注总结:(大写XX代表@Param的属性名)
①没用@Param:sql映射文件用#{param1}取值
②使用@Param:sql映射文件用#{param1}或者#{“XX”}取值
如果入参是一个类对象:(小写xxx代表Bean的参数属性名,大写XX代表@Param的属性名)
①没用@Param:#{param1.xxx}
②使用@Param:#{param1.xxx}或者#{XX.xxx}
如果入参是一个Collection类型(List,Set)或者数组类型
①Collection类型 :#{collection[0]}
②List类型 :#{list[0]}
③数组类型 :#{array[0]}
5、sql映射文件中KaTeX parse error: Expected 'EOF', got '#' at position 4: {}和#̲{}取值区别 相同:都可以获取…{}是取出值,然后拼接到sql中. 可能出现安全问题
使用场景:
①#{}:正常情况下都使用
②${}:原生jdbc不支持占位符的地方,如:分表、排序、拆分等需要动态定义部分sql语句的内容
如: select * from ${xxx}_info
select * from info order by ${column name} ${sort}
6、#{}取值时候指定参数规则
作用:为sql配置文件中的sql的某个参数指定规则,规定应该接受什么样的数据
使用方法:在sql配置文件中的sql语句里面的#{}标签内添加, 如:select * from info where id=#{id,javaType=Integer}
支持指定的属性:
①javaType(java类型)
②jdbcType(数据库类型)
③mode (储存过程)
④numericScale (保留几位数字)
⑤resultMap(封装的结果类型)
⑥typeHandler (处理数据的类型处理器)
⑦jdbcTypeName (数据库类型名)
7、注意:jdbcType通常需要在某种特定条件下被设置,如数据为null的时候,有些数据库不能识别mybatis对null的默认处理(如:oracle),会报错说无效的列类型
解决方法一:#{XX,jdbcType=OTHER}
解决方法二:全局配置中settings标签中添加<setting name="jdbcTypeForNull" value="NULL"></setting>
8、mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="dbConfig.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="InfoMapper.xml" />
</mappers>
</configuration>
9、InfoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.ssmtest.InfoMapper">
<insert id="add" parameterType="com.demo.ssmtest.Info" useGeneratedKeys="true" keyProperty="id">
insert into info (name,password,description) values (#{name},#{password},#{description})
</insert>
<delete id="deleteById">
delete from info where id=#{id}
</delete>
<update id="updateById">
update info set name=#{name},password=#{password},description=#{description} where id=#{id}
</update>
<select id="getByMutiParams" resultType="com.demo.ssmtest.Info">
select * from info where id = #{id} and name=#{name}
</select>
<select id="getByMap" resultType="com.demo.ssmtest.Info">
select * from info where id = #{id} and name=#{name}
</select>
</mapper>
10、dbConfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis
jdbc.username=root
jdbc.password=
11、Info.java
package com.demo.ssmtest;
public class Info {
Integer id;
String name;
String password;
String description;
public Info() {
super();
}
public Info(Integer id, String name, String password, String description) {
super();
this.id = id;
this.name = name;
this.password = password;
this.description = description;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Info [id=" + id + ", name=" + name + ", password=" + password + ", description=" + description + "]";
}
}
12、InfoMapper.java
package com.demo.ssmtest;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
public interface InfoMapper {
public Integer add(Info info);
public Integer deleteById(int id);
public Info getByMutiParams(@Param("id")int id,@Param("name")String name);
public Info getByMap(Map<String, Object>map);
public Integer updateById(Info info);
}
13、TestMain.java
package com.demo.ssmtest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class TestMain {
public static void main(String[] args) throws Exception {
test();
}
public static void test() throws Exception {
//传入多个参数到sql映射文件
//注意:
//①多个参数会封装成一个map,如果没有指定的话sql映射文件中参数的值应写为#{param1}/#{param2}......
//②如果不指定而且不把映射文件中的参数值写为#{paramX},就会报org.apache.ibatis.binding.BindingException
//1、通过POJO:传入多个参数到sql映射文件
//使用POJO指定参数key的步骤:
//①在Mapper类的对应方法的入参前面 使用@Param("xx")修饰入参 如:public Info getByMutiParams(@Param("id")int id,@Param("name")String name);
//②在sql映射文件的sql语句中,使用@Param指定的key来作为sql参数的key 如:select * from info where id = #{id} and name=#{name}
//2、通过Map:传入多个参数到sql映射文件
//使用Map指定参数key的步骤:
//①在Mapper类的方法中使用Map类型来定义入参
//②sql映射文件的sql中,直接使用#{"xxx"}
//③在调用映射类方法的时候定义一个Map类型参数,对应sql映射文件中的#{"xxx"}来指定Map数据的key
//传入参数注总结:(大写XX代表@Param的属性名)
// ①没用@Param:sql映射文件用#{param1}取值
// ②使用@Param:sql映射文件用#{param1}或者#{"XX"}取值
//如果入参是一个类对象:(小写xxx代表Bean的参数属性名,大写XX代表@Param的属性名)
// ①没用@Param:#{param1.xxx}
// ②使用@Param:#{param1.xxx}或者#{XX.xxx}
//如果入参是一个Collection类型(List,Set)或者数组类型
// ①Collection类型 :#{collection[0]}
// ②List类型 :#{list[0]}
// ③数组类型 :#{array[0]}
//3、sql映射文件中${}和#{}取值区别
//相同:都可以获取map中的值或者pojo对象属性的值
//不同:
//①#{}是以预编译的形式,讲参数设置到sql语句中. 可以防止sql注入
//②${}是取出值,然后拼接到sql中. 可能出现安全问题
//使用场景:
//①#{}:正常情况下都使用
//②${}:原生jdbc不支持占位符的地方,如:分表、排序、拆分等需要动态定义部分sql语句的内容
// 如: select * from ${xxx}_info
// select * from info order by ${column name} ${sort}
//4、#{}取值时候指定参数规则
//作用:为sql配置文件中的sql的某个参数指定规则,规定应该接受什么样的数据
//使用方法:在sql配置文件中的sql语句里面的#{}标签内添加, 如:select * from info where id=#{id,javaType=Integer}
//支持指定的属性:
//①javaType (java类型)
//②jdbcType (数据库类型)
//③mode (储存过程)
//④numericScale (保留几位数字)
//⑤resultMap (封装的结果类型)
//⑥typeHandler (处理数据的类型处理器)
//⑦jdbcTypeName (数据库类型名)
//注意:jdbcType通常需要在某种特定条件下被设置,如数据为null的时候,有些数据库不能识别mybatis对null的默认处理(如:oracle),会报错说无效的列类型
// 解决方法一:#{XX,jdbcType=OTHER}
// 解决方法二:全局配置中settings标签中添加<setting name="jdbcTypeForNull" value="NULL"></setting>
String resource = "mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession();
try {
InfoMapper MutiParamQueryMapper = openSession.getMapper(InfoMapper.class);
Info MutiParamInfo = MutiParamQueryMapper.getByMutiParams(1,"nameA");
System.out.println(MutiParamInfo);
InfoMapper MapQueryMapper = openSession.getMapper(InfoMapper.class);
Map<String, Object> map = new HashMap<String,Object>();
map.put("id", 1);
map.put("name", "nameA");
Info MapQueryInfo = MapQueryMapper.getByMap(map);
System.out.println(MapQueryInfo);
} finally {
openSession.close();
}
}
}
14、项目目录
15、demo
https://download.youkuaiyun.com/download/qq_22778717/10714890