mybatis一---入门

来源:互联网 发布:开通淘宝达人 编辑:程序博客网 时间:2024/06/05 17:10

1:简介

MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plan Old Java Objects,普通的 Java对象)映射成数据库中的记录。

2:安装

a:添加mybatis-x.x.x.jar

b:若是maven项目,则在pom.xml中加入如下代码

<dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis</artifactId>  <version>x.x.x</version></dependency>

3:从xml文件中构建sqlsessionfactory

String resource = "org/mybatis/example/mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

4:从java程序中构建sqlsessionfactory

DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();TransactionFactory transactionFactory = new JdbcTransactionFactory();Environment environment = new Environment("development", transactionFactory, dataSource);Configuration configuration = new Configuration(environment);configuration.addMapper(BlogMapper.class);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

所加的BlogMapper类,若存在xml配置文件的话,mybatis会自动加载与其名称对等的xml文件

5:xml文件包含对mybatis的核心设置、获取数据库连接、决定事务范围和控制的事务管理器。简单示例如下

<?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="${driver}"/>        <property name="url" value="${url}"/>        <property name="username" value="${username}"/>        <property name="password" value="${password}"/>      </dataSource>    </environment>  </environments>  <mappers>    <mapper resource="org/mybatis/example/BlogMapper.xml"/>  </mappers></configuration>

xml元素:验证xml文档的正确性

environment元素:事务管理器和连接池的配置

mappers元素:包含所有的mapper映射器

mapper对应的xml文件则是SQL语句及映射定义

6:从sqlsessionfactory获取sqlsession,sqlsession可以用来执行已经映射的SQL语句

a:

SqlSession session = sqlSessionFactory.openSession();try {  Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);} finally {  session.close();}

b:

SqlSession session = sqlSessionFactory.openSession();try {  BlogMapper mapper = session.getMapper(BlogMapper.class);  Blog blog = mapper.selectBlog(101);} finally {  session.close();}

7:SQL语句的执行

<mapper namespace="org.mybatis.example.BlogMapper">  <select id="selectBlog" parameterType="int" resultType="Blog">    select * from Blog where id = #{id}  </select></mapper>

在 命 名 空 间 “com.mybatis.example.BlogMapper”中,它定义了一个名为“selectBlog”的映射语句,这 样它允许你使用完全限定名 “org.mybatis.example.BlogMapper.selectBlog” 来调用映射语句

a:

Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
b:

BlogMapper mapper = session.getMapper(BlogMapper.class);Blog blog = mapper.selectBlog(101);

8:注解写SQL语句(不建议使用)

package org.mybatis.example;public interface BlogMapper {  @Select("SELECT * FROM blog WHERE id = #{id}")  Blog selectBlog(int id);}