MyBatis初阶
概述mybatis就是使用注释就能够完成JDBC需要大量做的事情但终归会有不能SQL语句,参数赋值,结果映射Mapper public interface UserInfoMapper { Select(select * from user_info) ListUserInfo selectAll(); }此时需要测试一下是否成功只要有mybatis依赖,就需要添加数据库信息基础操作打印日志打印出来的信息可以比作调试,能够看到sql语句的执行、执行传递的参数以及执行结果.在配置文件中进行配置mybatis: configuration: # 配置打印 MyBatis⽇志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl参数传递以及赋值方法中的参数传递到此方法之后,使用#{}获取方法中的参数,进行赋值Select(select * from user_info where id #{id}) ListUserInfo selectById(Integer id);参数重命名#{}必须和Param中的名字一致Select(select * from user_info where id #{id} and age #{age}) ListUserInfo selectByIdAndAge(Param(id) Integer id, Integer age);增Insert(insert into user_info (username,password,age,gender) values (#{username},#{password},#{age},#{gender})) Integer insertUser(UserInfo userInfo);//添加Param注释之后userInfo就不能自动赋值了,生成参数 Insert(insert into user_info (username,password, age ,gender) values (#{uerInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})) Integer insertUser2(Param(userInfo) UserInfo userInfo);返回主键//插入想要得到id,keyProperty需要一个属性来接收 Options(useGeneratedKeys true,keyProperty id) Insert(insert into user_info (username,password,age,gender) values (#{username},#{password},#{age},#{gender})) Integer insertUser(UserInfo userInfo);删Delete(delete from user_info where id#{id}) Integer deleteById(Integer id);改Update(update user_info set username#{username} where id#{id}) void updateUsername(String username,Integer id);查使用SQL语句查询时,通过测试方法发现, 发现有一些字段没有进行赋值,只有Java对象属性和数据库字段一模一样时,才会赋值,那么我们通过什么方法才能够赋值?对SQL语句起别名// Select(select id,username,password,age,gender,phone,delete_flag as deleteFlag, // create_time as createTime,update_time as updateTime // from user_info)结果映射数据库查询到的结果与java对象属性进行映射Results({ Result(column delete_flag ,property deleteFlag), Result(column create_time ,property createTime), Result(column update_time ,property updateTime) }) Select(select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info) ListUserInfo selectAll();如果其他SQL也想复用这个映射关系就需要:Results(id resultMap,value { Result(column delete_flag ,property deleteFlag), Result(column create_time ,property createTime), Result(column update_time ,property updateTime) }) Select(select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info) ListUserInfo selectAll(); ResultMap(value resultMap) Select(select * from user_info where id #{id}) ListUserInfo selectById(Integer id);开启驼峰命名数据库一般时蛇形命名,而Java属性一般通过驼峰命名,怎么才能够自动启动映射呢?D:在配置文件添加mybatis: configuration: map-underscore-to-camel-case: true #配置驼峰⾃动转换MyBatisXMl配置文件mybatis开发有两种方式:1.使用注解2.使用XML配置文件MyBatis XML方式需要两步1.配置连接字符串(和注解的配置文件一样)和MyBatismybatis: mapper-locations: classpath:mybatis/**Mapper.xml2.写持久层代码方法定义:interfaceMapper public interface UserInfoXmlMapper { ListUserInfo selectAll(); ListUserInfo selectById(Integer id); ListUserInfo selectByIdAndGender(Integer id,Integer gender); Integer insertUser(UserInfo userInfo); Integer insertUser2(Param(userInfo) UserInfo userInfo); Integer deleteById(Integer id); void updateUsername(String username,Integer id); }方法实现:XXX.xml?xml version1.0 encodingUTF-8? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.Kon.mybatis.mapper.UserInfoXmlMapper !--只有select需要对象映射,resultType -- select idselectAll resultTypecom.Kon.mybatis.entity.UserInfo select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info /select select idselectById resultTypecom.Kon.mybatis.entity.UserInfo select * from user_info where id #{id} /select select idselectByIdAndGender resultTypecom.Kon.mybatis.entity.UserInfo select * from user_info where id #{id} and gender #{gender} /select insert idinsertUser useGeneratedKeystrue keyPropertyid insert into user_info (username,password,age,gender) values (#{username},#{password},#{age},#{gender}) /insert insert idinsertUser2 insert into user_info (username,password,age,gender) values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender}) /insert delete iddeleteById delete from user_info where id#{id} /delete update idupdateUsername update user_info set username#{username} where id#{id} /update /mapperXML起别名和注释是非常相似的#{}和${}的区别#{}是预编译SQL,${}是即时SQL性能即时SQL每次都需要进行SQL语法解析、SQL优化、SQL编译,而预编译SQL再进行编译参数传递为String时,#{}会加引号,而${}不会安全使用${}可能会导致SQL注入SQL注入:通过改变事先预定好的参数的SQL语句,以达到执行代码对服务器进行攻击的方法select * from user_info where username or 1 1即时SQL是将参数进行拼接,而预编译SQL就已经确定这个位置应该放入哪些具体的数据排序功能${}还是有一些用处的,此处的${order}是不需要加引号的,因为排序不需要加 Select(select * from user_info order by id ${order}) ListUserInfo selectUserByOrder(String order);有 $ 的地方就会有SQL注入的问题,解决办法:规定只能有asc或者desc,定义为枚举就可以了Like查询使用#{}查不出来自动会加引号,但使用${}可以查出来但是依旧会有SQL注入的问题,那么我们就使用mysql的内置函数来解决Select(select * from user_info where username like concat(%,#{key},%)) ListUserInfo queryAllUserByLike(String key);数据库连接池用户多次创建和销毁连接会消耗比较多的资源,那么数据库连接池会提前创建多个连接,也就是多个连接对象,用户请求连接时,将连接对象connection获取给用户,SQL语句执行完之后再还给数据库连接池如果要切换连接池,只需要引入相关依赖即可