zoukankan      html  css  js  c++  java
  • mybatis

    Mybatis 官方网站:http://www.mybatis.org/mybatis-3

    官方中文教程:http://www.mybatis.org/mybatis-3/zh/index.html

    Mybatis 源码:https://github.com/mybatis/mybatis-3

    Mybatis API:http://www.mybatis.org/mybatis-3/apidocs/index.html

    推荐的blog

    https://blog.csdn.net/isea533/article/category/2092001

    https://www.cnblogs.com/hellokitty1/p/5216025.html

    https://www.cnblogs.com/hellokitty1/p/5218892.html

    https://blog.csdn.net/hellozpc/article/details/80878563

    Mybatis 视频培训教程

    http://edu.51cto.com/course/1354.html

    在项目中引入 Mybatis jar 包的两种方式

    1. 直接下载 mybatis-x.x.x.jar 文件,然后放到 classpath 路径中

    2. 使用 Maven 管理依赖

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>x.x.x</version>
    </dependency>

    配置 Mybatis 的几种方式

    1. 使用 xml 配置文件,通常命名为 mybatis-config.xml (推荐)

    2. 使用注解

    3. 使用 Spring bean 方式进行配置

    <?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>
        <settings>
    <!-- logImpl 属性配置指定使用 LOG4J 输出日志 -->
    <setting name="logImpl" value="LOG4J" /> </settings> <typeAliases>
    <!-- 配置了一个包的别名,通常使用一个类需要使用全限定名 -->
    <!-- 配置了 package 标签后,就可以只用类名,不需要写出包名 --> <package name="tk.mybatis.simple.model" /> </typeAliases>
    <!-- 配置数据库的链接 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> <property name="" value="" /> </transactionManager> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.100.142:3306/mybatis" /> <property name="username" value="username" /> <property name="password" value="password" /> </dataSource> </environment> </environments>
    <!-- 配置完整的 Mapper.xml 路径 --> <mappers> <mapper resource="tk/mybatis/simple/mapper/CountryMapper.xml" /> </mappers> </configuration>

    创建实体类和 Mapper.xml 文件

    Mybatis 是一个结果映射框架,这里创建的实体类实际上是一个数据值对象(Data Value Object),是一个 JavaBean。

    在实际应用中,一个表一般会对应一个实体类,一个字段对应一个实体类的属性,用于 INSERT、UPDATE、DELETE 和简单的 SELECT 操作。

    Mybatis  默认遵循 “下划线转驼峰”的命名方式,所以在创建实体类的时候一般都按照这种方式进行。

    一般用 Mapper 作为 XML 和接口名的后缀,这里的 Mapper 和常用的 DAO 后缀类似。

    SQL 语句定义在 Mapper.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" >
    <!-- 根元素,namespace属性定义了当前xml的命名空间 -->
    <mapper namespace="tk.mybatis.simple.mapper.CountryMapper">
    <!-- 定义一个select查询 id属性为当前查询定义唯一的id resultType属性定义了返回值类型 -->
    <select id="selectAll" resultType="Country" > select id, countryname, countrycode from country </select> </mapper>

    编写测试类使用 Mybatis

    package tk.mybatis.simple.mapper;
    
    import java.io.IOException;
    import java.io.Reader;
    import java.util.List;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import tk.mybatis.simple.model.Country;
    
    public class CountryMapperTest {
        private static SqlSessionFactory sqlSessionFactory;
        
        //@BeforeClass
        public static void init() {
            try { 
    // 该工具类用于将配置文件读入 Reader Reader reader
    = Resources.getResourceAsReader("mybatis-config.xml");
    // 创建 sqlSessionFactory 工厂对象,在创建过程中,首先解析 mybatis-config.xml
    // 读取配置文件中的 mappers 配置后会读取全部的 Mapper.xml 进行具体方法的解析
    // 在这些解析完成后,SqlSessionFactory 就包含了所有的属性配置和执行 SQL 的信息 sqlSessionFactory
    = new SqlSessionFactoryBuilder().build(reader); reader.close(); } catch (IOException ignore) { ignore.printStackTrace(); } } //@Test public void testSelectAll() {
    // 通过 SqlSessionFactory 工厂对象获取一个 SqlSession SqlSession sqlSession
    = sqlSessionFactory.openSession(); try { // 通过 selectList 方法查找 Mapper.xml 中的 id="selectAll" select 语句,执行 SQL 查询
    // Mybatis 底层使用 JDBC 执行 SQL,获得查询结果集 ResultSet 后,根据 resultType 的配置
    // 将结果映射为相应类型的集合,返回查询结果
    List
    <Country> countryList = sqlSession.selectList("selectAll"); printCountryList(countryList); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); // 关闭 sqlsession } } private void printCountryList(List<Country> countryList) { for (Country country : countryList) { System.out.printf("%-4d%4s%4s ", country.getId(), country.getCountryName(), country.getCountryCode()); } } public static void main(String [] args) { CountryMapperTest countryMapperTest = new CountryMapperTest(); CountryMapperTest.init(); countryMapperTest.testSelectAll(); } }
  • 相关阅读:
    Read-Copy Update Implementation For Non-Cache-Coherent Systems
    10 华电内部文档搜索系统 search04
    10 华电内部文档搜索系统 search05
    lucene4
    10 华电内部文档搜索系统 search01
    01 lucene基础 北风网项目培训 Lucene实践课程 索引
    01 lucene基础 北风网项目培训 Lucene实践课程 系统架构
    01 lucene基础 北风网项目培训 Lucene实践课程 Lucene概述
    第五章 大数据平台与技术 第13讲 NoSQL数据库
    第五章 大数据平台与技术 第12讲 大数据处理平台Spark
  • 原文地址:https://www.cnblogs.com/0820LL/p/10074508.html
Copyright © 2011-2022 走看看