第一个Mybatis程序示例 Mybatis简介(一)
- 要求通过xml或注解的方式将要执行的各种statement(statement、preparedStatemnt、CallableStatement)配置起来
- 并通过java对象和statement中的sql进行映射生成最终执行的sql语句
- mybatis框架执行sql并将结果映射成java对象并返回。
第一个Mybatis程序
1.新建项目

2.包获取与导入

3.数据库准备
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '默认姓名' COMMENT '姓名',
`age` int(11) DEFAULT '',
`sex` varchar(255) DEFAULT NULL,
`random` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

4.配置文件设置


5.SQL映射文件设置


6.调整配置文件


7.创建实体类Student

8.修改myMapper.xml文件

9.测试

初识Mybatis


- 要从哪个数据库进行操作?
- 要操作的SQL在哪里?
- 执行哪个SQL?通过层级的命名标识符定位
- 执行SQL的细节信息有哪些?SQL内容,参数内容,返回类型等

- 配置信息搭建了Mybatis应用框架
- 映射设置了一次执行的所需信息


<mappers>
<package name="org.mybatis.builder"/>
</mappers>
附录:完整代码
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '默认姓名' COMMENT '姓名',
`age` int(11) DEFAULT '',
`sex` varchar(255) DEFAULT NULL,
`random` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
项目结构

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://localhost:3306/sampledb?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/myMapper.xml"/>
</mappers>
</configuration>
myMapper.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="mapper.myMapper">
<select id="selectStudent" resultType="first.Student">
select * from student where id = #{id}
</select>
</mapper>
Student
package first;
public class Student {
private Long id;
private String name;
private Integer age;
private String sex;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Student{");
sb.append("id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append(", sex='").append(sex).append('\'');
sb.append('}');
return sb.toString();
}
}
测试代码
package first;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Test { public static void main(String[] args) throws Exception { /*
* 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的。
* SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。
* 而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先定制的 Configuration 的实例构建出 SqlSessionFactory 的实例。
* */
String resource = "config/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
/*
* 从 SqlSessionFactory 中获取 SqlSession
* */
SqlSession session = sqlSessionFactory.openSession();
try {
Student student = (Student) session.selectOne("mapper.myMapper.selectStudent", 2);
System.out.println(student);
} finally {
session.close();
}
}
}
接口应用

package first;
public interface MyMapper {
Student selectStudent(Integer id);
}
<?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="first.MyMapper">
<select id="selectStudent" resultType="first.Student">
select * from student where id = #{id}
</select>
</mapper>


package first; import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Test2 { public static void main(String[] args) throws Exception {
/*
* 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的。
* SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。
* 而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先定制的 Configuration 的实例构建出 SqlSessionFactory 的实例。
* */
String resource = "config/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"development"); /*
* 从 SqlSessionFactory 中获取 SqlSession
* */
SqlSession session = sqlSessionFactory.openSession();
try {
MyMapper mapper = session.getMapper(MyMapper.class);
Student student = mapper.selectStudent(2);
System.out.println(student);
} finally {
session.close();
}
} }
MyMapper mapper = session.getMapper(MyMapper.class);
Student student = mapper.selectStudent(2);
第一个Mybatis程序示例 Mybatis简介(一)的更多相关文章
- 第一个Java程序示例——Hello World!【转】
本文转载自: 跟随世界潮流,第一个Java程序输出“Hell World!”. 通过Eclipse运行程序 启动Eclipse,在菜单中选择“文件 --> 新建 --> Java项目”,弹 ...
- iOS 5 :一个UIPageViewController程序示例
原文:http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_UIPageViewController_Application 在Xco ...
- 我的第一个Java程序和Java简介
public calss HelloWorld{ public static void main(String[] args){ System.out.println("Hello Worl ...
- 我的第一个Mybatis程序
第一个Mybatis程序 在JDBC小结中(可以参阅本人JDBC系列文章),介绍到了ORM,其中Mybatis就是一个不错的ORM框架 MyBatis由iBatis演化而来 iBATIS一词来源于“i ...
- MyBatis(一):第一个MyBatis程序
本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出1便就懂!b站搜索狂神说即可 https://space.bilibili.com/95256449?spm_id_from=333.788 ...
- Mybatis入门及第一个Mybatis程序
Mybatis笔记整理 所需要的基础知识 JDBC Mysql Java基础 Maven Junit 框架:是有配置文件的.最好的方式:看官网文档 1.简介 1.1.什么是MyBatis 简介 什么是 ...
- 创建一个简单MyBatis程序
文章目录 MyBatis基础 MyBatis 简介 创建一个MyBatis程序 1. 创建Java项目 2. 加载MyBatis包 3. 编写POJO类和映射文件 4.创建mybatis-config ...
- 第一个MyBatis程序(博客初写者)
第一个Mybatis程序 一.环境: 1.JDK1.8 2.MYSQL5.7 3.IDEA 4.MAVEN 3.63 二.Mybatis认识: 1.查看官方文档 https://mybatis.org ...
- MyBatis-02-第一个Mybatis程序
2.第一个Mybatis程序 思路:搭建环境-->导入Mybatis-->编写代码-->测试! 2.1.搭建环境 搭建数据库 CREATE DATABASE `mybatis`; u ...
随机推荐
- 数据分析——matplotlib
基础 # coding=utf-8 import matplotlib.pyplot as pt import numpy as np from matplotlib import font_mana ...
- 201771010126 王燕《面向对象程序设计(Java)》第十四周学习总结(测试程序11)
实验十四 Swing图形界面组件 理论部分: 不使用布局管理器 有时候可能不想使用任何布局管理器,而只 是想把组件放在一个固定的位置上.下面是将一 个组件定位到某个绝对定位的步骤: 1)将布局管理器 ...
- (转)基因芯片数据GO和KEGG功能分析
随着人类基因组计划(Human Genome Project)即全部核苷酸测序的即将完成,人类基因组研究的重心逐渐进入后基因组时代(Postgenome Era),向基因的功能及基因的多样性倾斜.通过 ...
- ASP.NET MVC 网页应用 action 传递的Model
视图界面 @using {引用模型} @model {具体模型} <html> @Model.{具体模型的属性} </html> 注意区分Model的大小写 引入时,使用@mo ...
- eclipse中生成的html存在中文乱码问题的解决方法
最近在做测试报告生成时遇到了个中文乱码的问题,虽然在html创建过程中设置了编码格式htmlReporter.config().setEncoding("UTF-8");但是生成的 ...
- CentOS 5.9裸机编译安装搭建LAMP
Linux系统:CentOS 5.9,查看CentOS版本,命令如下: [root@localhost /]# cat /etc/redhat-release CentOS release 5.9 ( ...
- angular.js学习笔记(一)
1.angular单项数据绑定 2.不要使用控制器的时候: 任何形式的DOM操作:控制器只应该包含业务逻辑.DOM操作则属于应用程序的表现层逻辑操作,向来以测试难度之高闻名于业界.把任何表现层的逻辑放 ...
- 出现 HTTP Status 500 - Servlet.init() for servlet springmvc threw exception 异常的原因及解决方法
今天做项目的时候遇到了这个问题 其中有一句是Caused by: org.springframework.beans.factory.BeanCreationException: Error crea ...
- 中文转拼音without CJK
Xamarin写Android程序时,通常要使用按中文首字母分组显示(如通讯录) . 于是需要被迫包含CJK,不过包含后包肯定是会变大的,于是....自己写了一个硬枚举的中文转拼音的类. 原理是这样的 ...
- .net core 灵活读取配置文件
using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using Syst ...