Mybatis 快速入门之Hello World

来源:互联网 发布:淘宝佣金怎么用 编辑:程序博客网 时间:2024/06/05 19:59

Mybatis 快速入门之Hello World

这篇文章不讲如何实现,目的只有一个,那就是让开发者迅速掌握mybatis如何使用

1. mybatis 基本配置文件

这里以mysql为例,将mybatis.xml放置到src目录下

<?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="mysql_development">    <environment id="mysql_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/mybatis"/>            <property name="username" value="root"/>            <property name="password" value="gaoxiang"/>        </dataSource>    </environment></environments> <!--映射文件--><mappers>    <mapper resource="domain/StudentMapper.xml"/></mappers></configuration>

2. 写测试实体Student

省略getter and setter methods

public class Student {private int id;private String name;private  double salary;}

3. 写Student的映射文件

类似于hibernate,mybatis也要指定实体类与数据库的关系
在实体类相同目录下建立StudentMapper.xml(名称不限)

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="student"><insert id="add1">    insert into student(id,name,salary) values(1,'哈哈',7000)</insert><insert id="add2" parameterType="domain.Student">    INSERT INTO  student VALUES (#{id},#{name},#{salary});</insert></mapper>

说明:我们后续的操作也是在操作这个xml、

4. 写mybatis的工具类

同其他操作数据库一样,我们也需要获取session等,所以工具类不可或缺

public class MyBatisUtils {private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();private static SqlSessionFactory sqlSessionFactory;static{    try {        Reader reader = Resources.getResourceAsReader("mybatis.xml");        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);    } catch (IOException e) {        e.printStackTrace();        throw new RuntimeException(e);    }}private MyBatisUtils(){}public static SqlSession getSqlSession(){    SqlSession sqlSession = threadLocal.get();    if(sqlSession == null){        sqlSession = sqlSessionFactory.openSession();        threadLocal.set(sqlSession);    }    return sqlSession;}public static void closeSqlSession(){    SqlSession sqlSession = threadLocal.get();    if(sqlSession != null){        sqlSession.close();        threadLocal.remove();    }}}

5. 大功告成

public class MyBatisTest {@Testpublic  void testInsert(){    Student student = new Student(2,"hehe",10000);    SqlSession session = MyBatisUtils.getSqlSession();    session.insert("student.add2",student);    session.commit();}}

6. mybatis 工作流

1)通过Reader对象读取src目录下的mybatis.xml配置文件(该文本的位置和名字可任意)
2)通过SqlSessionFactoryBuilder对象创建SqlSessionFactory对象
3)从当前线程中获取SqlSession对象
4)事务开始,在mybatis中默认
5)通过SqlSession对象读取StudentMapper.xml映射文件中的操作编号,从而读取sql语句
6)事务提交,必写
7)关闭SqlSession对象,并且分开当前线程与SqlSession对象,让GC尽早回收

7. 小结一下hello World

  1. mybatis 工具类是把session和线程绑定了,这一个工具类很有用,可以拷贝下来
  2. 主配置文件中容易将映射文件漏掉,这一点初学者很容易犯这个错
  3. 下一篇我们将深入讲解映射文件
1 0
原创粉丝点击