Mybatis入门到精通:helloworld
官方地址:https://github.com/mybatis/mybatis-3
官方中文文档地址:http://www.mybatis.org/mybatis-3/zh/getting-started.html
项目代码: https://gitee.com/ganganbobo/mybatis-parent 里面的"mybatis-helloword模块"
一:准备工作
1、导入MyBatis的jar包和mysql连接驱动(maven):
<properties><mybatis.version>3.4.1</mybatis.version><mysql-connector.version>5.1.38</mysql-connector.version><lombok.version>1.16.12</lombok.version></properties><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql-connector.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies>
2、新建数据库和表,并在表中插入几条测试数据:
CREATE TABLE `tb_employee` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`last_name` varchar(20) DEFAULT NULL,`email` varchar(20) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
3、创建实体类对象:
import lombok.Data;@Data
public class Employee {private Integer id;private String lastName;private String email;
}
二:以XML的方式构建SqlSessionFactory
public SqlSessionFactory getSqlSessionFactory() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);return new SqlSessionFactoryBuilder().build(inputStream);}
创建Employee实体类Sql映射文件:
EmployeeMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.ganbo.mapper.EmployeeMapper"><select id="getEmpById" resultType="com.ganbo.entity.Employee">select id,last_name lastName,email from tb_employee where id = #{id}</select></mapper>
创建全局配置文件mybatis-conf.xml,该配置文件包含了对 MyBatis 系统的核心设置,包含获取数据库连接实例的数据源(DataSource)和决定事务作用域和控制方式的事务管理器(TransactionManager)。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//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://localhost:3306/mybatis" /><property name="username" value="root" /><property name="password" value="root" /></dataSource></environment></environments><mappers><mapper resource="EmployeeMapper.xml" /></mappers>
</configuration>
测试方法:
/*** 1、根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 有数据源一些运行环境信息* 2、sql映射文件;配置了每一个sql,以及sql的封装规则等。* 3、将sql映射文件注册在全局配置文件中* 4、写代码:* 1)、根据全局配置文件得到SqlSessionFactory;* 2)、使用sqlSession工厂,获取到sqlSession对象使用他来执行增删改查* 一个sqlSession就是代表和数据库的一次会话,用完关闭* 3)、使用sql的唯一标志来告诉MyBatis执行哪个sql。sql都是保存在sql映射文件中的。** @throws IOException*/@Testpublic void test() throws IOException {// 2、获取sqlSession实例,能直接执行已经映射的sql语句// sql的唯一标识:statement Unique identifier matching the statement to use.// 执行sql要用的参数:parameter A parameter object to pass to the statement.SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession openSession = sqlSessionFactory.openSession();try {Employee employee = openSession.selectOne("com.ganbo.mapper.EmployeeMapper.getEmpById", 1);System.out.println(employee);} finally {openSession.close();}}
输出
Employee(id=1, lastName=ganbo, email=946211284@qq.com)
到此完毕,这是最原始的方式构建查询语句
现在比较流行的是以接口的方式使用Mybatis,创建接口Mapper:
import com.ganbo.entity.Employee;public interface EmployeeMapper {Employee getEmpById(Integer id);}
测试代码:
@Testpublic void test01() throws IOException {// 1、获取sqlSessionFactory对象SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();// 2、获取sqlSession对象SqlSession openSession = sqlSessionFactory.openSession();try {// 3、获取接口的实现类对象//会为接口自动的创建一个代理对象,代理对象去执行增删改查方法EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);Employee employee = mapper.getEmpById(1);System.out.println(mapper.getClass());System.out.println(employee);} finally {openSession.close();}}
会得到跟上面同样的运行结果.
如有疑问,请在底部留言,我会一一回复大家!
Mybatis入门到精通:helloworld相关推荐
- mybatis实战教程(mybatis in action),mybatis入门到精通
目录(?) [-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程 ...
- MyBatis入门到精通——Mybatis入门篇
目录 一.什么是 MyBatis? 二.入门案例 (1)创建maven项目 (2)引入依赖包 (3)创建数据库执行sql脚本 三.入门程序源码 (1)User.java (2)UserMapper.j ...
- mybatis实战教程(mybatis in action),mybatis入门到精通(转)
写在这个系列前面的话: 以前曾经用过ibatis,这是mybatis的前身,当时在做项目时,感觉很不错,比hibernate灵活.性能也比hibernate好.而且也比较轻量级,因为当时在项目中,没来 ...
- MyBatis 入门到精通(二) SQL语句映射XML文件
MyBatis 真正强大之处就在这些映射语句,也就是它的魔力所在.对于它的强大功能,SQL 映射文件的配置却非常简单. 如果您比较SQL 映射文件配置与JDBC 代码,您很快可以发现,使用SQL 映射 ...
- MyBatis 入门到精通(一) 了解MyBatis获取SqlSession
MyBatis是什么? MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis ...
- MyBatis入门HelloWorld,注解实现
入门数据准备 HelloWorld表准备 -- 创建hello表 CREATE TABLE hello(id INT,`name` VARCHAR(10) ); -- 添加数据 INSERT INTO ...
- Mybatis从入门到精通下篇
Mybatis从入门到精通下篇: 输入类型: 输出类型: ResultMap: 动态sql: if标签: where标签: sql片段: foreach标签: 关联查询: 以订单作为主体: 一对一查询 ...
- mybatis 存储过程 tmp_count_MyBatis从入门到精通
MyBatis是一款优秀的持久层框架,它支持定制化SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis可以使用简单的 XML或注解 ...
- Mybatis从入门到精通(全)
目录 前言 1. idea软件小技巧 2. mybatis优势 3. mybatis入门项目 3.1 项目具体步骤 3.2 创建数据表 3.3 创建maven项目 3.4 pom.xml配置文件 3. ...
最新文章
- syslog打印不带等级_printk的日志级别和控制台级别
- python DbUtils 使用教程
- 华为云服务器配置ipv6,华为云获“IPv6支持度优秀奖”,持续助推云网融合
- 观点:Linux 上的杀毒软件
- C和指针之strcat函数 strchr函数 strcmp函数 strcpy函数 strnchr函数 strstr函数实现
- python3学习笔记(2)_list-tuple
- java opencv bytearray转mat_如何用Python+OpenCV处理图像色彩?终于有人讲明白了
- jsp、servlet、tomcat中都设置了编码为UTF-8,依然报错:java.io.CharConversionException: Not an ISO 8859-1 character:
- Vue.js自定义指令的用法与实例
- 音乐播放小程序demo
- SHEL中调用uci
- 微信小程序开发工具整理代码快捷键
- 【T001】win10开机 自动打开 chrome word excel
- sqlloader 导入数据的一点经验教训(最后附我的导入过程)
- Webots中常用的函数(C版)
- PDF Search for Mac(PDF文件搜索工具)
- RAID数据恢复技术介绍
- C++string中的insert()插入函数
- building workspace js validation
- CANoe.DiVa操作指南-Pre-condition测试环境配置