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. 老爷机iphone4s 9.2.1降级6.1.3

    原帖见威锋网 sunnyskyline 2017年1月10日发的贴. 本文中加了一些我自己的情况,也是一知半解,抛砖引玉吧. 首先进行备份.进行备份.进行备份. 感谢大神@极端阴险 感谢@shuaig ...

  2. TortoiseSVN 结合使用哪个问题跟踪系统比较好?TRAC?REDMINE?都有什么优缺点?

    TortoiseSVN 结合使用哪个问题跟踪系统比较好?TRAC?REDMINE?都有什么优缺点? -------------------------------------------------- ...

  3. 关于oracle的profiles

    一.关于oracle的profiles profiles文件是口令和资源限制的配置集合,包括CPU的时间.I/O的使用.空闲时间.连接时间.并发会话数量.密码策略等对于资源的使用profile可以做到 ...

  4. 基数排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def RadixSort(a): i = 0 #初始为个位排序 n = 1 #最小的位数置为1(包含0) max ...

  5. redis与lua

    内容大纲 redis里使用eval和evalsha redis管理Lua脚本  php里使用redis的lua脚本 在redis里使用lua脚本的好处 1.Lua脚本在Redis中是原子执行的,执行过 ...

  6. 使用JdbcTemplate访问数据库

    参考源端:https://blog.csdn.net/liaodehong/article/details/76974827 今天用Spring Boot访问一下数据库,并且把数据返回到页面中,进行增 ...

  7. PHP报错open_basedir restriction in effect

    问题是出现在了PHP.INI上面了 原因是php.ini里设置了 open_basedir=/var/web/w0895/:/tmp:/usr/lib/php 这里加上相关的目录就可以了 解答:其实o ...

  8. jQuery load() 判断 iframe 是否加载完毕

    判断 iframe 是否加载完毕  方法.jQuery load() var frm = document.getElementById('myiframe'); $(frm).load(functi ...

  9. 转化RGB颜色为CMYK颜色

    procedure RGBTOCMYK(R : byte; G : byte; B : byte; var C : byte; var M : byte; var Y : byte; var K : ...

  10. MyBatis 注解式开发