首页 > 代码库 > 1. MyBatis 简介

1. MyBatis 简介

1. 传统 JDBC 编程
 
2. ORM 模型
Object Relational Model,对象映射模型:数据库的表和 POJO (Plain Ordinary Java Object) 的映射关系模型。
 
3. Hibernate
全表映射;不能组装 SQL;对多表关联和复杂 SQL 查询支持较差;性能较差。
 
4. MyBatis
半自动映射框架:手工匹配提供 POJO、SQL 和映射关系。
 
5. 什么时候用 MyBatis
 
mybais-helloworld
技术分享
 
mybatis-config.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"/>            <dataSource type="POOLED">                <property name="driver" value="com.mysql.jdbc.Driver"/>                <property name="url" value="jdbc:mysql://localhost:3306/test"/>                <property name="username" value="root"/>                <property name="password" value="root"/>            </dataSource>        </environment>    </environments>    <mappers>        <mapper resource="com\sunyard\dao\xml\UserMapper.xml"/>    </mappers></configuration>

 

User.java
package com.sunyard.model;import java.util.Date;/** * Created by gaoeb on 2017/4/24. */public class User {    private int id;    private String username;    private String password;    private int credits;    private String lastIp;    private Date lastVisit;    private Date createTime;    private Date updateTime;    private int status;    // getter and setter}

 

UserMapper.java
package com.sunyard.dao;import com.sunyard.model.User;/** * Created by gaoeb on 2017/4/24. */public interface UserMapper {    User getUserById(int id);}

 

UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.sunyard.dao.UserMapper">    <select id="getUserById" parameterType="java.lang.Integer" resultType="com.sunyard.model.User">        SELECT            id          AS id,            username    AS username,            password    AS password,            credits     AS credits,            last_ip     AS lastIp,            last_visit  AS lastVisit,            create_time AS createTime,            update_time AS updateTime,            status      AS status        FROM t_user        WHERE id = #{id}    </select></mapper>

 

MyBatisUtil.java
package com.sunyard.util;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;/** * Created by gaoeb on 2017/4/24. */public class MyBatisUtil {    private static SqlSessionFactory sqlSessionFactory = null;    public static SqlSessionFactory getSessionFactory() {        if (sqlSessionFactory == null) {            try {                sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));                return sqlSessionFactory;            } catch (IOException e) {                e.printStackTrace();            }        }        return sqlSessionFactory;    }}

 

Main.java
import com.sunyard.dao.UserMapper;import com.sunyard.model.User;import com.sunyard.util.MyBatisUtil;import org.apache.ibatis.session.SqlSession;public class Main {    public static void main(String[] args) {        System.out.println("Hello World!");        SqlSession session = MyBatisUtil.getSessionFactory().openSession();        UserMapper userMapper = session.getMapper(UserMapper.class);        User user = userMapper.getUserById(1);        System.out.println(user.toString());        session.close();    }}

 

 
 
--eof--

1. MyBatis 简介