一.什么是MyBatis?

  MyBatis源自Apache的iBatis开源项目, 从iBatis3.x开始正式更名为MyBatis。它是一个优秀的持久层框架。

  二.为什么使用MyBatis?

  为了和数据库进行交互,通常的做法是将SQL语句写在Java代码中,SQL语句和Java代码耦合在一起不利于后期维护修改,而MyBatis能够帮助我们将SQL语句和Java代码分离,方便了后期因需求变动而对SQL语句进行修改。(联系properties文件的作用)

  三. 如何使用MyBatis

  a、下载MyBatis相应jar包:在github可以找到

  b、创建Java工程,导入MyBatis jar包(mybatis-3.4.4.jar)和数据库驱动包

  c、创建user_info表并添加数据(此处使用MySQL)

  create table user_info(

  id char(36) primary key,

  user_name varchar(15) unique,

  password varchar(15) not null

  )

  insert into user_info (id, user_name, password)

  values ('3ddcf637-15a8-49d9-a378-b3fa2f2f9c65', 'Aimee', '1983_2560x');

  insert into user_info (id, user_name, password)

  values ('7eb9deed-ec87-416e-abb3-1a5a2ce819de', 'Cassie', 'xm@560_lq');

  java代码部分

  方法一:

  在src根目录下创建名为config-mybatis.xml的XML File文件,代码如下:

  在src根目录下创建名为student.xml的XML File文件,代码如下:

  select name from student where id=1

  在com.jd.test包下创建Test类。代码如下

  public class Test {

  public static void main(String[] args) {

  try {

  InputStream inputStream = Resources.getResourceAsStream("config-mybatis.xml");

  //获取SqlSessionFactory对象

  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

  //获取SqlSession对象,代表与数据库的一次会话,用完需要关闭。注意:由于SqlSession为非线程安全的,所以该变量应定义为局部变量,不要定义成全局变量

  SqlSession sqlSession = sqlSessionFactory.openSession();

  String name = openSession.selectOne("com.jd.student.getNameById");

  System.out.println(name);

  sqlSession.close();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  说明:这种配置方式在为SQL语句传数据时由于参数类型为Object,所以隐藏类型安全问题。

  补充说明的是

  新手学习mybatis编写实体映射文件时,eclipse或者STS或者其他会显示红叉叉,将鼠标放在红叉叉上会得到这样的一串提示."The content of element type "mapper" must match "EMPTY". Children of type "comment" are not allowed."

  这是因为定义实体映射xml文件的文档类型里面的属性不对的缘故。

  如果是这样写的:没有网络时,mybatis-3-mapper.dtd加载不到,所以下面定义的元素标签就会报红叉叉。

  改成这样就好了

  方法二:

  创建接口文件IStudentDao

  public interface IStudentDao {

  String getNameById();

  }

  在方法一的基础上, 无锡人流医院 http://xmobile.wxbhnk120.com/

  获取了SqlSession对象后,try-catch中的代码变更为

  IStudentDao studentDao=sqlSession.getMapper(IStudentDao.class);

  System.out.println(studentDao.getClass().getName());

  String name=studentDao.getNameById();

  System.out.println(name);

  说明:这种方法不仅实现了SQL语句的分离,而且弥补了第一种方法的不足,所以推荐使用这种使用方式。

  方法三:,

  在方法二的的基础上,

  在接口文件中,在接口方法之前添加注解

  @Select("select name from student where id=1")

  config-mybatis.xml的XML File文件中,变更的内容,更改mapper的属性,把之前的resource指向变为接口的路径

  说明:这种方式不仅使得SQL语句与Java代码出现了耦合,而且不便于写复杂的SQL语句,比如当依据多个字段查询时,where关键字后面的条件是动态的,这时就不便于使用注解的方式写该SQL语句。

最新文章

  1. 界面使用webview,并且webview里面有图片进行自动切换导致界面上滚动条卡顿。
  2. Noi2011 阿狸的打字机
  3. 优化Select 语句的原则
  4. 【原创】Eclipse中为SVN设置快捷键
  5. 如何正确选择MySQL数据列类型
  6. 用python3破解wingIDE
  7. 自定义NavigationView's item 的高度
  8. Cookie和Session (转)
  9. HDU 1079 Calendar Game 博弈
  10. Quartz_理解3
  11. MyBatis框架及原理分析
  12. tp5命名空间
  13. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习6
  14. VS2008/2005 MFC程序调试经验
  15. Maven------pom.xml自动加载各种类库代码
  16. jQuery的过滤器总结
  17. S3C2440启动程序运行过程
  18. vue(4)hello world
  19. linux正则表达式(一)
  20. 洛谷 P1330 封锁阳光大学

热门文章

  1. 一次解决idea maven settings.xml文件不生效
  2. TTA 方法
  3. 搭建阿里云服务 FTP 折中方案
  4. 【转】史上最强Tomcat8性能优化
  5. 安装Nvidia显卡驱动、CUDA和cuDNN的方法(jsxyhelu整编)
  6. 将已经存在的项目提交到gitlab的新分支中
  7. linux环境中,openssl升级及openresty中nginx基于新版本openssl重新编译
  8. time命令_Linux time命令:测量命令的执行时间或者系统资源的使用情况(转)
  9. php连接mysql8报错如何解决
  10. MAVEN安装代码到本地库,安装jar, source, javadoc的方式