# MyBatis作为一个ORM框架,其重要程度不用过多介绍。
下面开始一起学习吧:
本博客的编程方法与MyBatis官方文档基本一致:
## 1.创建一个数据库mybatis_learn以及对应的表tbl_employee:
`CREATE DATABASE mybatis_learn;`
```
CREATE TABLE `tbl_employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`last_name` varchar(255) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
```
## 2.创建一个JavaBean(使用了lombok):
```
package com.mybatis.learn.bean;
import lombok.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Employee {
private Integer id;
private String lastName;
private String gender;
private String email;
}
```
## 3.正式开始玩Mybatis:
### 3.1 引入依赖或者jar包
```
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
```
### 3.2创建SqlSessionFactory(不/使用xml方式)
使用xml方式:
```
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
```
使用这种方式时,需要你写一个配置文件mybatis-config.xml:
```
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.1.61:3306/mybatis_learn" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 -->
<mappers>
<mapper resource="EmployeeMapper.xml" />
</mappers>
</configuration>
```

当然, 你也可以告别繁琐的xml文件配置:
```
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
```

###3.3从 SqlSessionFactory 中获取 SqlSession
```
SqlSession session = sqlSessionFactory.openSession();
try {
Employee employee = (Employee) session.selectOne("org.mybatis.example.BlogMapper.selectEmployee", 1);
System.out.println(employee);
} finally {
session.close();
}
```
同时别忘了写具体的sql映射文件:
```
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectEmployee" resultType="com.mybatis.learn.bean.Employee">
select * from tbl_employee where id = #{id}
</select>
</mapper>
```
这样子就可以正常查询了(lastName 需要在配置文件中使用别名)

MyBatis3系列__01HelloWorld的更多相关文章

  1. MyBatis3系列__03几个常用的属性配置

    本文主要讲几个xml配置属性: 其都写在mybatis配置文件中 1.properties属性:其作用主要是可以动态引进外部的配置文件中的相关配置 resource:引入类路径下的资源 url:引入网 ...

  2. MyBatis3系列__02接口式编程

    hello world MyBatis3支持面向接口编程: 具体做法如下,将helloWorld中的EmployeeMapper.xml文件进行更改: <?xml version="1 ...

  3. Spring系列__01HelloWorld

    Spring作为一款成熟的Java框架,其优点和意义不用我多说,可以参考:https://m.w3cschool.cn/wkspring/pesy1icl.html 今天开始写一下Spring家族的总 ...

  4. MyBatis3系列__06查询的几点补充

    关于查询的一点补充: 当查询部门信息时,希望查询该部门下的所有员工,下面会采取两种方式实现: 1.联合查询 public Department getDeptWithEmpById(Integer i ...

  5. MyBatis3系列__Demo地址

    一直光写博客了,并且感觉贴代码有点麻烦,但是以后的博客也尽量说的清楚,此外,觉得贴一下demo会好一些: 当然了,需要能够FQ哈,如果不能FQ的话建议百度或者参考这个:https://secure.s ...

  6. MyBatis3系列__05查询补充&resultMap与resultType区别

    1.查询补充 当你查询一条记录并且是简单查询时,情况相对简单,可以参考以下的例子: public Employee getEmpById(Integer id); 对应的xml文件中: <sel ...

  7. MyBatis3系列__04CRUD以及参数处理

    本文将会简单介绍一下MyBatis的CRUD以及结合源码讲解一下MyBatis对参数的处理. 作为一个ORM框架,最基本的使用也就是CRUD了,MyBatis提供了两种方法:xml配置文件和动态注解. ...

  8. SpringBoot系列__01HelloWorld

    接触SpringBoot很久了,但是一直没有很深入的研究一下源码,最近重启了博客,顺便开始深入研究一下技术. 1.简介 参照官方文档的说法,SpringBoot的设计理念就是为了简化Java程序员搭建 ...

  9. mybatis3.2.8 与 hibernate4.3.6 混用

    mybatis.hibernate这二个框架各有特色,对于复杂的查询,利用mybatis直接手写sql控制起来更灵活,而一般的insert/update,hibernate比较方便.同一个项目中,这二 ...

随机推荐

  1. MySQL学习笔记(一)Ubuntu16.04中MySQL安装配置(5.6优化、错误日志、DNS解决)

    目录 第一部分.5.6安装.配置.自动备份 第二部分.5.7源码安装.配置.自动备份 第一部分.5.6安装 1.安装mysql sudo apt-get install mysql-server su ...

  2. Node.js实战项目学习系列(4) node 对象(global、process进程、debug调试)

    前言 在之前的课程我们学习了Node的模块化规则,接下来我们将学习下 Node的几个新特性:global ,process进程,debug调试 global 跟在浏览器中的window一样都是全局变量 ...

  3. SHELL希尔排序

    /****************************************************************************** * Compilation: javac ...

  4. sublime 浏览器快捷键设置

    之前每次忘掉在哪打开,这次认真记一下 菜单栏Preferences-->Package Settings-->Side Bar-->Key Binding-Users [ // ch ...

  5. opensuse 使用xx-net

    提示安装 [launcher][WARNING] import pynotify fail, please install python-notify if possible. gae_proxy][ ...

  6. sql server 2008 中的 server profiler 的简单使用

    server profiler 是一个SQL server的 数据库执行语句的监控工具. 登录你需要监控的数据库. 2 .设置要监控进程的PID. 3.设置监控的数据库. 4 . 最后点击运行 就可以 ...

  7. spring的纯注解的IOC配置

    package config; import com.mchange.v2.c3p0.ComboPooledDataSource;import org.apache.commons.dbutils.Q ...

  8. CDQ分治求不知道多少维偏序 (持续更新 ]

    求三维偏序的模板 : //Author : 15owzLy1 //luogu3810.cpp //2018 12 25 16:31:58 #include <cstdio> #includ ...

  9. 在visual studio 2013中编译Lua5.3.1

    注:以下是基于 别人的教程或笔记来操作并按照自己的操作记录的纯文字版编译和hello lua过程. 原图文版链接: 原文链接 1.创建空的解决方案: 文件->新建->项目->其他项目 ...

  10. 【easy】404. Sum of Left Leaves

    求所有左节点的和. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...