MyBatis 在执行 SQL 时会将传递的参数自动放入一个 Map 集合中
sqlsession默认不自动提交事务,若需要自动提交事务可以使用sqlsessionFactory.opensession(true) ;
用$取值---直接把值赋值过去不会加引号
#会根据类型去适当匹配是否加' '
$全部不会
like 三种解决情况 1.用${}解决 select * from user where name like '%${name}%' 2.mysql字符串解决 select * from user where name like concat('%',#{param},'%') 3.用#{}解决 select * from user where name like "%"#{name}"%"
Mybatis获取参数的方式
多个字面量类型的参数
-
若mapper接口中的方法参数为多个时,此时MyBatis会自动将这些参数放在一个map集合中
1. 以arg0,arg1...为键,以参数为值; 2. 以param1,param2...为键,以参数为值;
因此只需要通过${}和#{}访问map集合的键就可以获取相对应的值,注意${}需要手动加单引号。
使用arg或者param都行,要注意的是,arg是从arg0开始的,param是从param1开始的
<!--User checkLogin(String username,String password);--> <select id="checkLogin" resultType="User"> select * from t_user where username = #{arg0} and password = #{arg1} </select><!--User checkLogin(String username,String password);--> <select id="checkLogin" resultType="User"> select * from t_user where username = '${param1}' and password = '${param2}' </select>
map集合类型的参数
-
若mapper接口中的方法需要的参数为多个时,此时可以手动创建map集合,将这些数据放在map中只需要通过${}和#{}访问map集合的键就可以获取相对应的值,注意${}需要手动加单引号
<!--User checkLoginByMap(Map<String,Object> map);--> <select id="checkLoginByMap" resultType="User"> select * from t_user where username = #{username} and password = #{password} </select>@Test public void checkLoginByMap() { SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); Map<String,Object> map = new HashMap<>(); map.put("usernane","admin"); map.put("password","123456"); User user = mapper.checkLoginByMap(map); System.out.println(user); }
l 、mapper接口方法的参数为单个的字面量类型 可以通过${}和#{}以任意的名称获取参数值,但是需要注意$(}的单引号问题
2、mapper接口方法的参数为多个时 此时MyBatis会将这些参数放在一个map集合中,以两种方式进行存储a>以arg0, arg1...为键,以参数为值 b>以param1 , param2...为键,以参数为值 因此只需要通过#{}和${}以键的方式访问值即可,但是需要注意${}的单引号问题
3、若mapper接口方法的参数有多个时,可以手动将这些参数放在一个map中存储只需要通过#{}和${}以键的方式访问值即可,但是需要注意${}的单引号问题
//********
最好就用这两种情况
4mapper接口方法的参数是实体类类型的参数,只需要通过#{}和${}以属性的方式访问属性值值即可,但是需要注意${}的单引号问题
< ! --void insertuser(User user);--> <insert id="insertuser"> insert into t_user values(null, #{username},#{password},#{age},#{sex},#{email}) </insert>
5.使用@param注解命名参数
a>以@Param注解的值为键,以参数为值
b>以param1 , param2...为健,以参数为值 因此只需要通过#{}和${}以键的方式访问值即可,但是需要注意${}的单引号问题
加上@param注解后mybatis会将参数放入map集合中,并以@param的值为键,以参数为值
MyBatis的各种查询功能:
1、若查询出的数据只有一条 a>可以通过实体类对象接收
b>可以通过list集合接收
c>可以通过map集合接收 结果: password=123456,sex=男, id=3,age=23,email=123450qq.com,username=admin}
2、若查询出的数据有多条
a>可以通过实体类类型的List集合接收
b>可以通过map类型的List集合接收
c>可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换的map集合作为值,以某个字段的值作为键,放在同一个map集合中
@MapKey("id")
public HashMap<String,Object> queryMap();
有多条结果集,给每条数据指定字段id,当做key加入,从而加入到map集合中
注意:一定不能通过实体类对象接收,此时会抛异常TooManyResultsException
获取自增的主键
void insertuser(User user); useGeneratedKeys:设置当前标签中的sql使用了自增的主键 keyProperty:将自增的主键的值赋值给传输到映射文件中参数的某个属性 <!--void insertUser(User user);--> <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> insert into t_user values (null,#{username},#{password},#{age},#{sex},#{email}) </insert>
通过resultMap设置自定义的映射关系
-
resultMap:设置自定义映射
-
属性:
-
id:表示自定义映射的唯一标识,不能重复
-
type:查询的数据要映射的实体类的类型
-
子标签:
-
id:设置主键的映射关系
-
result:设置普通字段的映射关系
-
子标签属性:
-
property:设置映射关系中实体类中的属性名
-
column:设置映射关系中表中的字段名
-
-
-
-
若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射,即使字段名和属性名一致的属性也要映射,也就是全部属性都要列出来
<resultMap id="UserMapper" type="com.sun.bean.User"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="password" column="password"></result> <result property="address" column="address"></result> <result property="phone" column="phone"></result> </resultMap>
处理多对一映射关系
对一对应对象,对多对应集合
多对一,首先在多里创建一的对象
1.方法一:级联方式处理映射关系
<resultMap id="one" type="com.sun.bean.User"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="password" column="password"></result> <result property="address" column="address"></result> <result property="phone" column="phone"></result> <result property="dept.did" column="did"></result> <result property="dept.deptname" column="deptname"></result> </resultMap>
方式二,通过association解决
<resultMap id="two" type="com.sun.bean.User"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="password" column="password"></result> <result property="address" column="address"></result> <result property="phone" column="phone"></result> <association property="dept" javaType="com.sun.bean.Dept"> <id property="did" column="did"></id> <result property="deptname" column="deptname"></result> </association> </resultMap> association:处理多对一的映射关系 property:需要处理多对的映射关系的属性名 javaType:该属性的类型
处理一对多映射关系
对一对应对象,对多对应集合
<resultMap id="deptAndUserResultMap" type="com.sun.bean.Dept"> <id property="did" column="did"></id> <result property="deptname" column="deptname"></result> <collection property="users" ofType="com.sun.bean.User"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="password" column="password"></result> <result property="address" column="address"></result> <result property="phone" column="phone"></result> </collection> </resultMap>
collection:处理一对多的映射关系
ofType:表示该属性所对应的集合中存储数据的类型
动态SQL :
1,if:根据标签中test属性所对应的表达式决定标签中的内容是否需要拼接到SQL中
-
where : 当where标签中有内容时,会自动生成where关键字,并且将内容前多余的and或or去掉当where标签中没有内容时,此时where标签没有任何效果
select id="getUserinfo" resultType="com.sun.bean.User"> select * from user <where> <if test="name !=null and name!='' "> name=#{name} </if> <if test="phone!=null and phone!=''"> and phone=#{phone} </if> </where> </select>
-
3.trim用于去掉或添加标签中的内容
-
常用属性
-
prefix:在trim标签中的内容的前面添加某些内容
-
suffix:在trim标签中的内容的后面添加某些内容
-
prefixOverrides:在trim标签中的内容的前面去掉某些内容
-
suffixOverrides:在trim标签中的内容的后面去掉某些内容
-
-
若trim中的标签都不满足条件,则trim标签没有任何效果,也就是只剩下
select * from t_emp
<!--List<Emp> getEmpByCondition(Emp emp);--> <select id="getEmpByCondition" resultType="Emp"> select * from t_emp <trim prefix="where" suffixOverrides="and|or"> <if test="empName != null and empName !=''"> emp_name = #{empName} and </if> <if test="age != null and age !=''"> age = #{age} and </if> <if test="sex != null and sex !=''"> sex = #{sex} or </if> <if test="email != null and email !=''"> email = #{email} </if> </trim> </select>
4,choose, when.otherwise,相当于if...else if...elsewhen至少要有一个,otherwise最多只能有一个
<select id="getUserinfo" resultType="com.sun.bean.User"> select * from user <where> <choose> <when test="name !=null and name !='' "> name=#{name} </when> <when test="phone !=null and phone !='' "> phone=#{phone} </when> <otherwise> id=1 </otherwise> </choose> </where> </select>
5.foreach标签遍历
批量删除
<delete id="deleteMoreUser"> delete from user where id in ( <foreach collection="ids" item="id" separator=","> #{id} </foreach> ) </delete>
或者
<delete id="deleteMoreUser"> delete from user where <foreach collection="ids" item="id" separator="or"> id=${id} </foreach> </delete>