MyBatis1:MyBatis入门
MyBatis是什么
MyBatis是什么,MyBatis的jar包中有它的官方文档,文档是这么描述MyBatis的:
MyBatis is a first class persistence framework with support for custom SQL, stored procedures
and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of
parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration
and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.
翻译过来就是:MyBatis是一款支持普通SQL查询、存储过程和高级映射的持久层框架。MyBatis消除了几乎所有的JDBC代码、参数的设置和结果集的检索。MyBatis可以使用简单的XML或注解用于参数配置和原始映射,将接口和Java POJO(普通Java对象)映射成数据库中的记录。
本文先入门地搭建表、建立实体类、写基础的配置文件、写简单的Java类,从数据库中查出数据,深入的内容后面的文章再逐一研究。
建表、建立实体类
从简单表开始研究MyBatis,所以我们先建立一张简单的表:
create table student
(
studentId int primary key auto_increment not null,
studentName varchar(20) not null,
studentAge int not null,
studentPhone varchar(20) not null
)charset=utf8 insert into student values(null, 'Jack', 20, '');
insert into student values(null, 'Mark', 21, '');
insert into student values(null, 'Lily', 22, '');
insert into student values(null, 'Lucy', 23, '');
commit;
一个名为student的表格,里面存放了student相关字段,当然此时我们需要有一个Java实体类与之对应:
public class Student
{
private int studentId;
private String studentName;
private int studentAge;
private String studentPhone; public Student()
{
super();
} public Student(int studentId, String studentName, int studentAge,
String studentPhone)
{
this.studentId = studentId;
this.studentName = studentName;
this.studentAge = studentAge;
this.studentPhone = studentPhone;
} public int getStudentId()
{
return studentId;
} public void setStudentId(int studentId)
{
this.studentId = studentId;
} public String getStudentName()
{
return studentName;
} public void setStudentName(String studentName)
{
this.studentName = studentName;
} public int getStudentAge()
{
return studentAge;
} public void setStudentAge(int studentAge)
{
this.studentAge = studentAge;
} public String getStudentPhone()
{
return studentPhone;
} public void setStudentPhone(String studentPhone)
{
this.studentPhone = studentPhone;
} public String toString()
{
return "StudentId:" + studentId + "\tStudentName:" + studentName +
"\tStudentAge:" + studentAge + "\tStudentPhone:" + studentAge;
}
}
注意,这里空构造方法必须要有,SqlSession的selectOne方法查询一条信息的时候会调用空构造方法去实例化一个domain出来。OK,这样student表及其对应的实体类Student.java就创建好了。
写config.xml文件
在写SQL语句之前,首先得有SQL运行环境,因此第一步是配置SQL运行的环境。创建一个Java工程,右键src文件夹,创建一个普通文件,取个名字叫做config.xml,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>
<typeAliases>
<typeAlias alias="Student" type="com.xrq.domain.Student"/>
</typeAliases> <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/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="student.xml"/>
</mappers>
</configuration>
这就是一个最简单的config.xml的写法。接着右键src,创建一个普通文件,命名为student.xml,和mapper里面的一致(resource没有任何的路径则表示student.xml和config.xml同路径),student.xml这么写:
<?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="com.xrq.StudentMapper">
<select id="selectStudentById" parameterType="int" resultType="Student">
<![CDATA[
select * from student where studentId = #{id}
]]>
</select>
</mapper>
专门有一个student.xml表示student表的sql语句,当然也可以几张表共用一个.xml文件,这个看自己的项目和个人喜好。至此,MyBatis需要的两个.xml文件都已经建立好,接下来需要做的就是写Java代码了。
Java代码实现
首先要进入MyBatis的jar包:

第二个MySql-JDBC的jar包注意一下要引入,这个很容易忘记。
接着创建一个类,我起名字叫做StudentOperator,由于这种操作数据库的类被调用频繁但是调用者之间又不存在数据共享的问题,因此使用单例会比较节省资源。为了好看,写一个父类BaseOperator,SqlSessionFactory和Reader都定义在父类里面,子类去继承这个父类:
public class BaseOperator
{
protected static SqlSessionFactory ssf;
protected static Reader reader; static
{
try
{
reader = Resources.getResourceAsReader("config.xml");
ssf = new SqlSessionFactoryBuilder().build(reader);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
然后创建子类StudentOperator:
public class StudentOperator extends BaseOperator
{
private static StudentOperator instance = new StudentOperator(); private StudentOperator()
{ } public static StudentOperator getInstance()
{
return instance;
} public Student selectStudentById(int studentId)
{
SqlSession ss = ssf.openSession();
Student student = null;
try
{
student = ss.selectOne("com.xrq.StudentMapper.selectStudentById", 1);
}
finally
{
ss.close();
}
return student;
}
}
这个类里面做了两件事情:
1、构造一个StudentOperator的单实例
2、写一个方法根据studentId查询出一个指定的Student
验证
至此,所有步骤全部就绪,接着写一个类来验证一下:
public class MyBatisTest
{
public static void main(String[] args)
{
System.out.println(StudentOperator.getInstance().selectStudentById(1));
}
}
运行结果为:
StudentId:1 StudentName:Jack StudentAge:20 StudentPhone:20
看一下数据库中的数据:

数据一致,说明以上的步骤都OK,MyBatis入门完成,后面的章节,将会针对MyBatis的使用细节分别进行研究。
MyBatis1:MyBatis入门的更多相关文章
- mybatis入门基础(二)----原始dao的开发和mapper代理开发
承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...
- MyBatis入门基础(一)
一:对原生态JDBC问题的总结 新项目要使用mybatis作为持久层框架,由于本人之前一直使用的Hibernate,对mybatis的用法实在欠缺,最近几天计划把mybatis学习一哈,特将学习笔记记 ...
- MyBatis入门案例、增删改查
一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...
- mybatis入门_mybatis基本原理以及入门程序
一.传统jdbc存在的问题 1.创建数据库的连接存在大量的硬编码, 2.执行statement时存在硬编码. 3.频繁的开启和关闭数据库连接,会严重影响数据库的性能,浪费数据库的资源. 4.存在大量的 ...
- MyBatis入门学习教程-使用MyBatis对表执行CRUD操作
上一篇MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对use ...
- MyBatis入门学习(二)
在MyBatis入门学习(一)中我们完成了对MyBatis简要的介绍以及简单的入门小项目测试,主要完成对一个用户信息的查询.这一节我们主要来简要的介绍MyBatis框架的增删改查操作,加深对该框架的了 ...
- MyBatis入门学习(一)
一.MyBatis入门简要介绍(百科) MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyB ...
- MyBatis入门案例 增删改查
一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...
- MyBatis入门(五)---延时加载、缓存
一.创建数据库 1.1.建立数据库 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.7.9-log : Database - mybatis ****** ...
随机推荐
- Oracle分析函数入门
一.Oracle分析函数入门 分析函数是什么?分析函数是Oracle专门用于解决复杂报表统计需求的功能强大的函数,它可以在数据中进行分组然后计算基于组的某种统计值,并且每一组的每一行都可以返回一个统计 ...
- MIP 官方发布 v1稳定版本
近期,MIP官方发布了MIP系列文件的全新v1版本,我们建议大家尽快完成升级. 一. 我是开发者,如何升级版本? 对于MIP页面开发者来说,只需替换线上引用的MIP文件为v1版本,就可以完成升级.所有 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(75)-微信公众平台开发-用户管理
系列目录 前言 本节主要是关注者(即用户)和用户组的管理,微信公众号提供了用户和用户组的管理,我们可以在微信公众号官方里面进行操作,添加备注和标签,以及移动用户组别,同时,微信公众号也提供了相应的接口 ...
- ASP.NET Core 中文文档 第四章 MVC(3.9)视图组件
作者: Rick Anderson 翻译: 娄宇(Lyrics) 校对: 高嵩 章节: 介绍视图组件 创建视图组件 调用视图组件 演练:创建一个简单的视图组件 附加的资源 查看或下载示例代码 介绍视图 ...
- WebApi基于Token和签名的验证
最近一段时间在学习WebApi,涉及到验证部分的一些知识觉得自己并不是太懂,所以来博客园看了几篇博文,发现一篇讲的特别好的,读了几遍茅塞顿开(都闪开,我要装逼了),刚开始读有些地方不理解,所以想了很久 ...
- CSS中强悍的相对单位之em(em-and-elastic-layouts)学习小记
使用相对单位em注意点 1.浏览器默认字体是16px,即1em = 16px,根元素设置如下 html{ font-size: 100%; /* WinIE text resize correctio ...
- 网站里加入QQ在线客服
1.开启"QQ在线状态"服务 http://jingyan.baidu.com/article/b24f6c823425a586bfe5da1f.html http://www. ...
- 【从零开始学BPM,Day4】业务集成
[课程主题] 主题:5天,一起从零开始学习BPM [课程形式] 1.为期5天的短任务学习 2.每天观看一个视频,视频学习时间自由安排. [第四天课程] 1.课程概要 Step 1 软件下载:H3 BP ...
- 为什么你SQL Server的数据库文件的Date modified没有变化呢?
在SQL Server数据库中,数据文件与事务日志文件的修改日期(Date Modified)是会变化的,但是有时候你会发现你的数据文件或日志文件的修改日期(Date Modified)几个月甚至是半 ...
- springMVC初始化绑定器
单日期 在处理器类中配置绑定方法 使用@InitBinder注解 在这里首先注册一个用户编辑器 参数一为目标类型 propertyEditor为属性编辑器,此处我们选用 CustomDateEd ...