SQL映射文件
增删改标签的全部属性:
实现获取到插入数据后的自增id(useGeneratedKeys、keyProperty)
1、使用自增的属性:useGenerateKeys="true"<br /> 2、将刚才自增的id封装给哪个属性:keyProperty="属性名"
<insertid="insertUser"parameterType="pojo.User"useGeneratedKeys="true"keyProperty="id">insert into user (id,username,password,telephone,role,reginstertime) values(#{id},#{username},#{password},#{testphone},#{role},#{reginstertime})</insert>int result = sqlSession.insert("xxx.xxx.xxx", new xxx(null, "hhh", "111111", "110", "test", "2021-03-21"));System.out.println("刚才插入的id:" + xxx.getId());sqlSession.commit();
参数的各种取值:—-> #{参数}
现象:<br /> **1)、单个参数**<br /> 基本类型,<br /> 取值,#{随便写}<br /> 传入pojo<br /> **2)、多个参数:**<br /> public User getUser(Integer id, String username)<br /> 取值,#{参数名}是 无效了<br /> 可用,0,1(参数的索引)或者param1,param2(第几个参数paramN)<br /> select * from user where id = #{0} and username = #{1};<br /> 原因,只要传入了多个参数,mybatis会自动将参数封装在map中,封装时使用的key就是参数的索引和参数的第几个表示<br /> Map<String,Object> map = new HashMap<>();<br /> map.put("1",传入的值);map.put("2","传入的值"),<br /> #{key}就是从map中取值,<br /> 使用注解方式指定参数名:<br /> @Param("")我们可以让MyBatis封装map的时候,用我们指定的key<br /> public User getUser(@Param("id")Integer id,@Param("username")String username);<br /> **3)、传入map:将多个要使用的参数封装起来**<br /> 取值,#{key}<br /> Map<String, Object> map = new HashMap<String, Object>();<br /> map.put("id", 1);<br /> map.put("username", "张三");<br /> userDao.getUser(map);<br /> **4)、传入了pojo(javaBean)**<br /> 取值,#{pojo的属性名}扩展,多个参数:自动装map,<br /> method01(@Param("id)Integer id, String username, User user);<br /> Integer id -> #{id}<br /> String username -> #{param2}<br /> User user(去除这个里面的email) -> #{param3.email}<br /> <br /> 无论传入什么参数都要能正确的取出值<br /> #{key/属性名}
两种取值方式的区别:#{}、${}
#{属性名},是参数预编译的方式,参数都是用?代替,参数后来都是预编译设置进去的,安全,不会有sql注入问题<br /> ${属性名},不是参数预编译,而是直接和sql语句进行拼串,不安全<br /> id=1 and 1=1 or and username=<br /> 传入'1 or 1=or'<br /> 使用场景:sql语句只有参数位置是支持预编译的,可以在表名使用这个取值
查询返回LIst
<!-- public List<User> getAllUser(); --><!-- resultType="",如果返回的是集合,写的集合里面元素的类型 --><select id="getAllUser" resultType="bean.User">select * from user;</select>
查询返回Map
查询单条记录封装map:列名作为key,值作为value
<!-- punlic Map<String, Object> getUserByIdReturnMap(Integer id); --><select id="getUserByIdReturnMap" resultType="map">select * from user where id=#{id}</select>
查询多条记录封装map:把查询的记录的id的值作为key封装这个map,value就是这条记录封装好的对象<br /> 返回类型要写pojo类型
<!--@MapKey("id")public Map<String, User> getAllUser();--><select id="getUserByIdReturnMap" resultType="Bean.User">select * from user</select>
resultMap自定义封装规则
默认MyBatis自动封装结果集,<br /> 1)、按照列名和属性名一一对应的规则(不区分大小写);<br /> 2)、如果不一一对应,<br /> 1)、开启驼峰命名法(满足驼峰命名规则 aaa_bbb aaaBbb)<br /> 2)、起别名<br /> 3)、自定义结果集,自己定义每一列数据和javaBean的映射规则
<!--type="":指定为哪个javaBean自定义封装规则id="":唯一表示,让别名在后面引用--><resultMap type="pojo.User" id="myuser"><!-- 指定主键列的对应规则column="id":指定哪一栏是主键列property="":指定user的哪个属性封装id这一列数据 --><id property="id" column="id"/><!-- 指定普通列的对应规则 --><result property="username" column="uname"/><result property="password" column="pwd"/></resultMap><!-- 引用自定义封装规则 --><select id="getUserByIdReturnMap" resultType="Bean.User" resultMap="myuser">select * from user where id = #{id}</select>
