一、基础

  1.jar包下载https://github.com/mybatis/mybatis-3/releases

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

  2.找到配置文件:E:\2019.2\MyBatis\mybatis-3-mybatis-3.5.1\src\test\java\org\apache\ibatis\submitted\complex_property,获取

MyBatis入门 随笔 第1张

 

二、sqlSession

  sqlSession的作用:

  1.向sql语句传入参数

  2.执行sql语句

  3.获取执行sql语句的结果

  4.事务的控制

  如何获得sqlSession

  1.通过配置文件获取数据库连接相关信息

  2.通过配置信息构建sqlSessionFactory

  3.通过sqlSessionFactory打开数据库会话,即sqlSesseion(一次与数据库的会话)

MyBatis入门 随笔 第2张

获取sql配置文件

MyBatis入门 随笔 第3张

在该xml文件中写入sql语句

MyBatis入门 随笔 第4张

在dao层,用获取到的sqlSession来执行方法,方法中的参数传入对应sql语句的id

调用时,可以加上命名空间

MyBatis入门 随笔 第5张

MyBatis入门 随笔 第6张

MyBatis入门 随笔 第7张

MyBatis入门 随笔 第8张

在核心配置文件中导入sql配置文件

MyBatis入门 随笔 第9张

三、sql配置文件

  1.向sql语句中传参,但是只能传递一个参数,所以需要对参数封装

将参数加在后面:

MyBatis入门 随笔 第10张

 

  2.配置文件如何接收参数:

  MyBatis入门 随笔 第11张

  Mybatis中使用的是OGNL表达式获取参数

  MyBatis入门 随笔 第12张

MyBatis入门 随笔 第13张

 

 MyBatis入门 随笔 第14张

MyBatis入门 随笔 第15张

MyBatis入门 随笔 第16张

and后 第一个是数据库写法,等号后面#{(这里面依据上面进行写)}。

MyBatis入门 随笔 第17张

四、应用LOg4j调试动态sql

在log4j配置文件中配置

   MyBatis入门 随笔 第18张

log4j.rootLogger=DEBUG,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d[%t]%-5p[%c]-%m%n
log4j.logger.org.apache=INFO

 

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄