mybatis

作者: dsying | 来源:发表于2018-02-06 10:17 被阅读0次

    1 要想使用mybatis 需要在maven的pom.xml中导入jar包和mysql的驱动包

        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.2.8</version>
        </dependency>
    
        <dependency>
          <groupId>Mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.38</version>
        </dependency>
    

    2 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的
    SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得
    SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先定制的 Configuration 的实例构建出
    SqlSessionFactory 的实例

        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    

    XML 配置文件(configuration XML)中包含了对 MyBatis 系统的核心设置,包含获取数据库连接实例的数据源(DataSource)和决定事务作用域和控制方式的事务管理器(TransactionManager)

    3 配置文件

    jdbc.properties

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://127.0.0.1:3306/数据库名?useUnicode=true&characterEncoding=utf-8
    username=*****
    password=*****
    

    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>
    
        <properties resource="jdbc.properties" />
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${driver}"/>
                    <property name="url" value="${url}"/>
                    <property name="username" value="${username}"/>
                    <property name="password" value="${password}"/>
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <mapper resource="BlogMapper.xml"/>
        </mappers>
    </configuration>
    

    BlogMapper.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.dsying.dao.BlogMapper">
        <select id="selectBlog" resultType="com.dsying.bean.Blog">
            select * from blog where id = #{id}
        </select>
    </mapper>
    
    #namespace:当前mapper.xml所对应的dao层接口类
    #id:对应接口类中的方法(唯一)
    #resultType:接口类中方法的返回值  
    

    4 接口和实体类以及数据库表结构

    dao层接口

    package com.dsying.dao;
    
    import com.dsying.bean.Blog;
    
    public interface BlogMapper {
        Blog selectBlog(int id);
    
    }
    

    实体类

    package com.dsying.bean;
    
    public class Blog {
        private  int id;
        private String name;
        private int age;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
    
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Blog{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    表结构

    CREATE TABLE `blog` (
      `id` int(11) NOT NULL,
      `name` varchar(255) DEFAULT NULL,
      `age` int(11) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    

    5 测试

     String resource = "mybatis-config.xml";
            InputStream inputStream =null;
            SqlSession sqlSession =null;
            try {
                inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
             
              //SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。
              //你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
                sqlSession = sqlSessionFactory.openSession();
    
              //两个版本比较下 你会选择哪个版本(后者可以避免强转)
              //Blog blog = (Blog) session.selectOne("com.dsying.dao.BlogMapper.selectBlog", 1);
              /*
               BlogMapper.class = com.dsying.dao.BlogMapper = dao层接口类路径 = mapper.xml中的namespace
                selectBlog = select.id = 接口中要被实现的方法
              */
                BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
                Blog blog = blogMapper.selectBlog(1);
    
                System.out.println(blog.toString());
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                sqlSession.close();
            }
    

    6 结果


    屏幕快照 2018-02-06 10.49.14.png

    相关文章

      网友评论

          本文标题:mybatis

          本文链接:https://www.haomeiwen.com/subject/slawzxtx.html