1.MyBatis基础环境的搭建
1.1 核心配置文件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>
    <typeAliases>
        <typeAlias alias="User" type="com.mipo.entity.User"/>
    </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?characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/mipo/mapping/UserMapper.xml"/>
    </mappers>
</configuration>                    

1.2 配置映射文件UserMapper.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.mipo.mapping.UserMapper">
    <select id="selectUser" parameterType="Integer" resultType="User">
        SELECT * FROM T_USER WHERE ID = #{ID}
    </select>
</mapper>
    

1.3 编写junit单元测试

@Test
public void myBatisTest(){
    try {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new qlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = (User)sqlSession.selectOne("com.mipo.mapping.UserMapper.selectUser", Integer.valueOf(1));
        System.out.println(user);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

MyBatis 学习总结(二)的更多相关文章

  1. MyBatis学习系列二——增删改查

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...

  2. MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存

    目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...

  3. MyBatis学习 之 二、SQL语句映射文件(1)resultMap

    目录(?)[-] 二SQL语句映射文件1resultMap resultMap idresult constructor association联合 使用select实现联合 使用resultMap实 ...

  4. 【转】MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    [转]MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据, ...

  5. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作(转载)

    本文转载自:http://www.cnblogs.com/jpf-java/p/6013540.html 上一篇博文MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybati ...

  6. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...

  7. Mybatis学习笔记二

    本篇内容,紧接上一篇内容Mybatis学习笔记一 输入映射和输出映射 传递简单类型和pojo类型上篇已介绍过,下面介绍一下包装类型. 传递pojo包装对象 开发中通过可以使用pojo传递查询条件.查询 ...

  8. MyBatis学习笔记(二)——使用MyBatis对表执行CRUD操作

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4262895.html 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用My ...

  9. 二:MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...

  10. mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现

    项目结构  基础入门可参考:mybatis学习笔记(一)-- 简单入门(附测试Demo详细过程) 开始体验 1.新建项目,新建类MybatisUtil.java,路径:src/util/Mybatis ...

随机推荐

  1. windows server 2012 iis8.0部署mvc报错

    一开始以为需要在服务器装mvc在很多论坛找过也问了朋友都说需要装mvc,经过两天研究是不需要装mvc的只需要在项目的bin文件夹下放入下面三个dll. 未能加载文件或程序集“System.Web.Ht ...

  2. C#,Java,C++中的finally关键字

    博客原文:http://hankjin.blog.163.com/blog/static/33731937201031511305338/ 先说C++,标准C++不支持finally, 如果要实现fi ...

  3. js弹出新窗口居中

    方式1: <script language="javascript"> var newUrl = <%=newUrl % > //window.locati ...

  4. 总结iOS 8和Xcode 6的各种坑

    模拟器的路径从之前的~/Library/Application Support/iPhone Simulator移动到了~/Library/Developer/CoreSimulator/Device ...

  5. Oracle开始从Java运行时中移除JAR包

    早在2012年8月,在Java平台首席架构师Mark Reinhold宣布模块化项目Jigsaw之后不久,JEP 162这一题为“准备模块化”的提案就指出,在Jigsaw项目中为模块化系统开发的代码不 ...

  6. CreateProcessWithLogonW(好像可以指定进程的上下文环境)

    Creates a new process and its primary thread. Then the new process runs the specified executable fil ...

  7. Samples DataBind FastJson循环引用问题

    Fastjson full support databind, it's simple to use. Encode import com.alibaba.fastjson.JSON; Group g ...

  8. mysql explain 解释

     就是10W记录比如全表返回1行  Mysql显示可能是10W oracle是1行   mysql 是预估需要扫描的记录 Oracle 是预估返回的记录

  9. Single Number III——LeetCode

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  10. Sqrt(x)——LeetCode

    Implement int sqrt(int x). Compute and return the square root of x. 题目大意:实现求一个int的根. 解题思路:二分. public ...