MyBatis3系列__01HelloWorld
# 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的更多相关文章
- MyBatis3系列__03几个常用的属性配置
本文主要讲几个xml配置属性: 其都写在mybatis配置文件中 1.properties属性:其作用主要是可以动态引进外部的配置文件中的相关配置 resource:引入类路径下的资源 url:引入网 ...
- MyBatis3系列__02接口式编程
hello world MyBatis3支持面向接口编程: 具体做法如下,将helloWorld中的EmployeeMapper.xml文件进行更改: <?xml version="1 ...
- Spring系列__01HelloWorld
Spring作为一款成熟的Java框架,其优点和意义不用我多说,可以参考:https://m.w3cschool.cn/wkspring/pesy1icl.html 今天开始写一下Spring家族的总 ...
- MyBatis3系列__06查询的几点补充
关于查询的一点补充: 当查询部门信息时,希望查询该部门下的所有员工,下面会采取两种方式实现: 1.联合查询 public Department getDeptWithEmpById(Integer i ...
- MyBatis3系列__Demo地址
一直光写博客了,并且感觉贴代码有点麻烦,但是以后的博客也尽量说的清楚,此外,觉得贴一下demo会好一些: 当然了,需要能够FQ哈,如果不能FQ的话建议百度或者参考这个:https://secure.s ...
- MyBatis3系列__05查询补充&resultMap与resultType区别
1.查询补充 当你查询一条记录并且是简单查询时,情况相对简单,可以参考以下的例子: public Employee getEmpById(Integer id); 对应的xml文件中: <sel ...
- MyBatis3系列__04CRUD以及参数处理
本文将会简单介绍一下MyBatis的CRUD以及结合源码讲解一下MyBatis对参数的处理. 作为一个ORM框架,最基本的使用也就是CRUD了,MyBatis提供了两种方法:xml配置文件和动态注解. ...
- SpringBoot系列__01HelloWorld
接触SpringBoot很久了,但是一直没有很深入的研究一下源码,最近重启了博客,顺便开始深入研究一下技术. 1.简介 参照官方文档的说法,SpringBoot的设计理念就是为了简化Java程序员搭建 ...
- mybatis3.2.8 与 hibernate4.3.6 混用
mybatis.hibernate这二个框架各有特色,对于复杂的查询,利用mybatis直接手写sql控制起来更灵活,而一般的insert/update,hibernate比较方便.同一个项目中,这二 ...
随机推荐
- linux18.04+jdk11.0.2+hadoop3.1.2部署伪分布式
1. 下载 安装hadoop3.1.2http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-3.1.2/hadoop-3.1.2.tar.gz 注意 ...
- pta编译总结1
打印沙漏 (20 分) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号: ...
- 如何重写Java中的equals方法
Java中,只有8种基本类型不是对象,例如:4种整形类型(byte, short, int,long),2种浮点类型(flout, double),boolean, char不是对象,其他的所有类型, ...
- jq动画实现左右滑动
<!DOCTYPE html> <html> <head> <title>jquery动画滑动</title> <style type ...
- windows linux 文件编码转换
查看文件编码在Linux中查看文件编码可以通过以下几种方式:1.在Vim中可以直接查看文件编码:set fileencoding即可显示文件编码格式.如果你只是想查看其它编码格式的文件或者想解决用Vi ...
- MapReduce-FileInputFormat
在运行 MapReduce 程序时,输入的文件格式包括:基于行的日志文件.二进制格式文件.数据库表等.那么,针对不同的数据类型,MapReduce 是如何读取这些数据? FileInputFormat ...
- jQuery使用(十三):工具方法
proxy() onConflict() each() map() parseJson() makeArray() proxy() $.proxy()的实现机制与原生javaScript中的bind( ...
- RMQ(ST表)
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int N, M, ...
- Windows系统盘符错乱导致桌面无法加载。
问题如下 : 同事有台笔记本更换SSD硬盘,IT职员帮他将新硬盘分好区后再将系统完整Ghost过来,然后装到笔记本上.理论上直接就可以使用了!但结果开机后登陆用户桌面无法显示,屏幕黑屏什么都没有. 问 ...
- 【Nuxt】配置路由
export default ({store, redirect} => { if (!store.state.username) { redirect('/') } }) vuex 代码处理请 ...