码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis的简单搭建

时间:2018-01-17 23:33:10      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:input   oct   bat   pen   stat   factor   dtd   integer   return   

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>
    <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/xxxx" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
    <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.Entity.Book">
  <select id="selectBook" resultType="com.Entity.Book">
    select * from Book where id = #{id}
  </select>
</mapper>

其中namespace是实体的类路径,resultType是返回值类型(全限定类名)

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整合

 

Mybatis的简单搭建

标签:input   oct   bat   pen   stat   factor   dtd   integer   return   

原文地址:https://www.cnblogs.com/zhangjianbing/p/8306583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!