MyBatis 实用篇(一)入门

MyBatis(http://www.mybatis.org/mybatis-3/zh/index.html) 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。

MyBatis 消除了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。

MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java 对象)映射成数据库中的记录。

1. 引入 MyBatis

<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
</dependencies>

2. 配置文件

<?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="dev">
<environment id="dev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="com/github/binarylei/mybatis/helloworld/UserMapper.xml"/>
</mappers>
</configuration>

3. mapper 文件

<?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.github.binarylei.mybatis.helloworld.UserMapper">
<select id="getUser" resultType="com.github.binarylei.mybatis.helloworld.User">
select * from user where id=#{id};
</select>
</mapper>

4. 使用

4.1 xml 调用

public void test() {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
User user = sqlSession.selectOne("getUser", 1);
System.out.println(user);
} finally {
sqlSession.close();
}
}

4.2 接口调用

public interface UserMapper {
void getUser(User user);
} public void test() {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = (User) userMapper.getUser(1);
System.out.println(user);
} finally {
sqlSession.close();
}
}

每天用心记录一点点。内容也许不重要,但习惯很重要!

MyBatis 实用篇(一)入门的更多相关文章

  1. Mybatis 实用篇(四)返回值类型

    Mybatis 实用篇(四)返回值类型 一.返回 List.Map List<User> getUsers(); <select id="getUsers" re ...

  2. Mybatis 实用篇(三)参数处理

    Mybatis 实用篇(三)参数处理 sql 语句中的参数 parameterType 可以省略不写. 一.参数封装 1.1 单个参数处理 public interface UserMapper { ...

  3. MyBatis 实用篇(二)配置文件

    MyBatis 实用篇(二)配置文件 一.全局配置 全局配置:http://www.mybatis.org/mybatis-3/zh/configuration.html <?xml versi ...

  4. 持久层之 MyBatis: 第一篇:快速入门

    MyBatis入门到精通 JDBC回顾 1.1.认识MyBatis 1.1.使用IDEA创建maven工程 1.2.引入mysql依赖包 1.3.准备数据 1.4 使用JDBC手写MyBatis框架 ...

  5. MyBatis高级篇之整合ehcache缓存框架

    MyBatis高级篇之整合ehcache缓存框架  2017-09-01  0 Comments  1,671 Views  0 Times 一.前言 MyBatis为我们提供了Cache接口,也提供 ...

  6. net core体系-web应用程序-4asp.net core2.0 项目实战(CMS)-第二章 入门篇-快速入门ASP.NET Core看这篇就够了

    .NET Core实战项目之CMS 第二章 入门篇-快速入门ASP.NET Core看这篇就够了   原文链接:https://www.cnblogs.com/yilezhu/p/9985451.ht ...

  7. Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门

    文章目录 1. 声明式缓存 2. Spring Boot默认集成CacheManager 3. 默认的 ConcurrenMapCacheManager 4. 实战演练5. 扩展阅读 4.1. Mav ...

  8. SSM框架之Mybatis(1)入门

    Mybatis(1)入门 1.mybatis的概述 mybatis是一个持久层框架,用java编写的. 它封装了jdbc操作的很多细节,使开发者只需要关注sql语句本身,而无需关注注册驱动,创建连接等 ...

  9. MyBatis运行流程及入门第一个程序

    1. mybatis是什么? MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并 ...

随机推荐

  1. yarn import 使用package-lock.json

    yarn 1.7(目前最新的版本)支持npm 的package-lock.json 了 环境准备 安装更新yarn sudo npm install -g yarn 查看版本 yarn version ...

  2. hdu1066

    求N!的非零末尾位(吉大ACM模板) #include <stdio.h> #include <string.h> #define MAXN 10000 int lastdig ...

  3. 浅谈ASP.NET ---- 系列文章

    [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作篇)(下) [04]浅谈ASP. ...

  4. 为什么JSP会比Beetl慢

    转自:http://my.oschina.net/xiandafu/blog/475740 JSP是预编译成class的,然后模板渲染里比Beetl慢很多,文章从JSP静态文本处理不足,以及JSTL实 ...

  5. emacs之配置3,键盘和鼠标设置

    emacsConfig/kbd-mouse-setting.el ;;强制TAB键使用空格 (setq-default indent-tabs-mode nil) ;M-i执行tab-to-tab-s ...

  6. eclipse 的安装和汉化

    第一步:直接百度搜索eclipse,第一个就是官方网站 第二步:点击DOWNLOAD 64BIT进入下载页面 第三步:点击DOWNLOAD进行下载 If the download doesn't st ...

  7. 花瓶使用笔记 (抓数据时,记得添加host,不然抓不了包的)

    情况一: 有时候抓不了app的数据,那么把app的host 添加一下就可以了 proxy > SSL Proxying Settings 情况二: 开了 翻 墙 是抓不了包的! (掉了一次坑)

  8. java中的DAO设计模式

    创建数据库和表 sql语句: DROP TABLE IF EXISTS product; CREATE TABLE product( product_id varchar(20) NOT NULL, ...

  9. WebStorm live edit 浏览器实现同步实现步骤

    1.打开WebStore的设置对话框,找到live edit选项,选中Enable live editing. 2.打开Chrome浏览器,进入Chrome网上商店,搜索JetBrains IDE S ...

  10. secureCRT下linux rz命令上传文件失败或变小(破损)的问题解决方法

    在使用secureCRT的linux服务器时候,很多时候需要安装软件,而服务器本身是没有连接外网的 ,这时候就需要用到rz命令了. 在使用rz命令时候,有时候上传文件会失败,是因为上传的文件流中包含某 ...