MyBatis 学习入门·基本配置·项目实例

什么是MyBatis?

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可以重复

<resultMap type="com.daley.bean.Message" id="MessageResult">标签中对应的就是Message实体类

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;
	}
}



这个项目zip的下载地址:https://github.com/DaleyChao/MicroMessage/archive/master.zip
如果不能下载的话,请自行前往下载:https://github.com/DaleyChao/MicroMessage/tree/master
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西海幼鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值