加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何使用mybatis《一》

发布时间:2020-12-14 18:03:34 所属栏目:大数据 来源:网络整理
导读:mybatis作为ORM轻量级框架一出现就吸引了无数人的眼球,比hibernate要简单且入门较容易,下面开始我的第一个mybatis程序。 一、下载mybatis的包 我们知道任何一个框架都会有其包,我们从其官方网站下载其包,官网网址为:http://www.mybatis.org/mybatis-3/

mybatis作为ORM轻量级框架一出现就吸引了无数人的眼球,比hibernate要简单且入门较容易,下面开始我的第一个mybatis程序。

一、下载mybatis的包

我们知道任何一个框架都会有其包,我们从其官方网站下载其包,官网网址为:http://www.mybatis.org/mybatis-3/,我这里使用的版本为3.3.0。下载完成之后解压可看到如下的目录结构:

?

mybatis-3.3.0.jar是其包,lib目录下是其依赖包,我们把这些包放到我们的项目中。我这里创建的是javaweb项目,方便以后做web测试,编写的程序是普通的java程序。

二、配置环境

把mybatis的包放到项目的lib目录下之后,接下来时配置mybatis的环境,我们知道mybatis做为ORM框架,属于开发中的DAO层,是和数据库打交道的,所以我们必须有数据,这里以mysql数据为例,具体的建库和建表这里不阐述。

在src目录下创建mybatis的配置文件,文件名称为:configuratin.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="Message" type="com.cn.imooc.entity.Message"/> </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://127.0.0.1:3306/weixin?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!--映射文件-->
<mappers> <mapper resource="com/cn/mappers/message.xml"/> </mappers> </configuration>

在mybatis配置文件中还有很多的配置项这里仅仅使用了这几个,

<typeAliases>? 别名配置,即把实体类做个别名,目的在于在映射文件中使用实体类时不使用全限类名,而是使用别名,起到简单的作用

<environments>? 配置了一些环境 比如数据配置,这里我们配置了数据源

<mappers> 配置映射文件,这里配置了com.cn.mappers包下的message.xml映射文件

下面对Message实体类进行说明,此实体类里是一些属性,如下,

package com.cn.imooc.entity;

public class Message {
    private String id;
     String command;
     String description;
     String comment;
    public String getId() {
        return id;
    }
    void setId(String id) {
        this.id = String getCommand() {
         command;
    }
     setCommand(String command) {
        this.command = String getDescription() {
         description;
    }
     setDescription(String description) {
        this.description = String getComment() {
         comment;
    }
     setComment(String comment) {
        this.comment = comment;
    }
    @Override
     String toString() {
        return "Message [id=" + id + ",command=" + command + ",description="
                + description + ",comment=" + comment + "]";
    }
    

}

提供了getXXX和setXXX方法,其中setXXX方法很关键,我这里的属性和数据库的字段名是一致,可以方便使用mybatis查询出结果之后反射到实体类中,当然也可以和数据库表字段名不一致,后续会进行说明。

message.xml映射文件如下,

<mapper namespace="com.cn.inter.IMessageOperation">
     <select id="selectUserByID" parameterType="int" resultType="com.cn.imooc.entity.Message">
         select * from `message` where id = #{id}
     </select>
     
     <select id="selectMessages"  resultType="Message">
         select id,command,description,comment
                from message;
         
     </select>
</mapper>

这是我的mapper映射文件,里边有两个方法,一个是:selectUserById 根据id查询,另一个是selectMessages 查询所有

好了,到此为止,我们的mybatis的环境搭建完成,下面可以进行测试了。

三、测试

下面是测试代码,

 com.cn.test;

import java.io.IOException;
 java.io.Reader;

 org.apache.ibatis.io.Resources;
 org.apache.ibatis.session.SqlSession;
 org.apache.ibatis.session.SqlSessionFactory;
 org.apache.ibatis.session.SqlSessionFactoryBuilder;

 com.cn.imooc.entity.Message;

 MyTest {

    static  main(String[] args) {
        // TODO Auto-generated method stub
 
        Reader reader;
        SqlSession sqlSession=null;
        try {
            1、获得sqlSessionFactory
            reader = Resources.getResourceAsReader("Configuration.xml");
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
            2、获得sqlSession
             sqlSession=sqlSessionFactory.openSession();
            3、查询
            Message message=sqlSession.selectOne("com.cn.inter.IMessageOperation.selectUserByID",1);
            System.out.println(message);
        } catch (IOException e) {
             TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            sqlSession.close();
        }
        
    }

}

从上面可以看出,首先需要一个SqlSessionFactory,然后由sqlSessionFactory获得sqlSession,由sqlSession执行查询,使用了selectOne方法,第一个参数是映射文件中的nameSpace+“.”方法名,第二个参数是查询参数。

至此,一个mybatis程序就写完了,后续还会介绍别的版本。

有不当之处欢迎指正

谢谢

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读