什么是MyBatis?![](https://img-blog.csdn.net/20161129191402850)
MyBatis是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以对配置和原生地图使用简单的XML或注解,将接口和java的POJO(Plain Old java对象,普通的java对象)映射成数据库中的记录。
MyBatis下载地址:https://github.com/mybatis/mybatis-3/releases (下载最新版本为3.4.1)
下载的时候最好下载两个包:一个是mybatis-3.4.1.zip,这是就是正式发布文件,另一个是Source code.zip,这个文件中包含了MyBatis的一些例子还有一些配置文件,很有用,大家最好下载。
安装
要使用 MyBatis, 只需将 mybatis-x.x.x.jar 文件置于 classpath 中即可,或者放入lib中,例如:
配置时使用的XML示例,都在Sourcecode中,路径为:mybatis-3-mybatis-3.4.1\src\test\java\org\apache\ibatis\submitted\complex_property
从 XML 中构建 SqlSessionFactory
每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先定制的 Configuration 的实例构建出 SqlSessionFactory 的实例。
获得SqlSession:
Reader reader= Resources.getResourceAsReader("com/daley/config/Configuration.xml");
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession=sqlSessionFactory.openSession();
com/daley/config/Configuration.xml就是Configuration.xml的所在位置
SqlSession的作用:1、向SQL语句传入参数 2、执行SQL语句 3、获取结果 4、事务的控制 如何得到SqlSession:1、通过配置文件获取连接 2、通过SqlSessionFactoryBuilder() 来得到sql 会话工厂 3、最后打开会话得到SqlSession
Configuration.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">
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/daley/config/sqlxml/Message.xml" />
</mappers>
</configuration>
<mappers> <mapper resource="com/daley/config/sqlxml/Message.xml" /> //这就是SQLxml映射配置文件</mappers>
映射SQL语句XML文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2016 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Message">
<resultMap type="com.daley.bean.Message" id="MessageResult">
<id column="ID" jdbcType="INTEGER" property="id"/>
<result column="COMMAND" jdbcType="VARCHAR" property="command"/>
<result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/>
<result column="CONTENT" jdbcType="VARCHAR" property="content"/>
</resultMap>
<select id="queryMessageList" resultMap="MessageResult">
select ID,COMMAND,DESCRIPTION,CONTENT from message where 1=1
</select>
</mapper>
<mapper namespace="Message"> 是命名空间,里面的id和其他空间的id可以重复
type="com.daley.bean.Message"是Message类的位置(见上图)
id="MessageResult"是resultMap的标签 唯一不重复
<result column="COMMAND" jdbcType="VARCHAR" property="command"/> column:表中列名 jdbcType:数据库种类型 property:bean中变量名
实体类和数据库中的表如下:
public class Message {
private String id;
private String command;
private String description;
private String content;
public Message(){
}
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 getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}