发布于 2017-12-07 13:28:51 | 226 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的MyBatis教程,程序狗速度看过来!

MyBatis 基于Java的持久层框架

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。


这篇文章主要介绍了MyBatis如何使用(一)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下

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;
private String command;
private String description;
private String comment;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
@Override
public 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的环境搭建完成,下面可以进行测试了。

三、测试

下面是测试代码,


package com.cn.test;
import java.io.IOException;
import java.io.Reader;
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 com.cn.imooc.entity.Message;
public class MyTest {
public static void 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如何使用(一)的全部叙述,希望对大家有所帮助。后续还会介绍别的版本,更多内容敬请关注PHPERZ!



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务