Mybatis的简单搭建

1.官方网址

http://www.mybatis.org/mybatis-3/zh/getting-started.html

2.导入jar包

3.根据官方文档,首先写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>

    <!-- 引入jdbc.properties -->
    <properties resource="conf/jdbc.properties" />

    <!-- 全局配置文件为映射xml文件的resultType起别名,name为报名以及所有子包 -->
    <typeAliases>
        <package name="com.Entity" />
    </typeAliases>

    <!-- 默认环境为开发环境 -->
    <environments default="development">
        <!-- 配置测试环境 -->
        <environment id="test">
            <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>

        <environment id="development">
            <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>
    
    <!-- mybatis支持多数据库厂商:mysql或者oracle等等 -->
    <databaseIdProvider type="DB_VENDOR">
        <property name="MySQL" value="mysql"/>
        <property name="Oracle" value="oracle"/>
        <property name="SQL Server" value="sqlserver"/>
    </databaseIdProvider>

    <mappers>
        <mapper resource="conf/BookMapper.xml" />
    </mappers>
</configuration>

用来设定数据源和映射实体的xml文件,包括为返回值类型起别名,数据源环境,数据库厂商(移植性)等等,.,

4.编写实体类

public class Book {
    
    private Integer id;
    
    private String title;
    
    private String price;
    
    private String publishDate;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(String publishDate) {
        this.publishDate = publishDate;
    }

    @Override
    public String toString() {
        return "Book [id=" + id + ", title=" + title + ", price=" + price + ", publishDate=" + publishDate + "]";
    }
    
}

5.编写BookMapper.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.dao.BookMapper">
  <select id="selectBook" resultType="Book" databaseId="mysql">
    select * from Book where id = #{id}
  </select>
</mapper>

其中namespace是实体的类路径,resultType是返回值类型(全限定类名),databaseId是数据库厂商id

6.编写测试类

public class Test {

    public static void main(String[] args) throws IOException {
        // 读取全局配置文件
        String resource = "conf/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 打开session
        SqlSession session = sqlSessionFactory.openSession();
        try {
            // 查询单个实体,其中参数1:BookMapper.xml的namespace加上查询语句的唯一标识,参数2:sql语句的参数
          Book book = session.selectOne("com.Entity.Book.selectBook", 1);
          System.out.println(book.toString());
        } finally {
          // 关闭session
          session.close();
        }
    }
}

7.运行结果:

Book [id=1, title=2, price=3, publishDate=2016-11-02 20:44:20.0]

下一篇:spring和Mybatis整合

 

posted @ 2018-01-17 22:43  在谷歌上百度  阅读(278)  评论(0编辑  收藏  举报