首页 > 代码库 > Mybatis入门第一节

Mybatis入门第一节

Mybatis入门第一节

MyBatis是支持普通的sql查询的,存储过程和高级映射的优秀的持久层框架,mybatis消除了几乎所有的jdbc代码和参数的手工的设置以及对结果集的检索封装,它可以使用简单的xml或者注解来配置和原始的映射,将接口和Java的pojo映射成数据库中的记录。

jdbc->dbutils(特点自动封装结果集)->MyBatis->Hibernate(基本不用写sql语句)

Hibernate可以直接跟数据库连接,MyBaties需要一个中间层(配置文件xml)来跟数据库有关系。

1:首先导入mybatis的架包mybatis-3.1.1.jar.

2:创建数据库和表

CREATE DATABASE mybatis;USE mybatis;CREATE TABLE users(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20),age INT);INSERT INTO users(NAME,age) VALUES (tom,12);INSERT INTO users(NAME,age) VALUES (jack,11);

3:添加mybatis的配置文件conf.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/mybatis" />                <property name="username" value="root" />                <property name="password" value="root" />            </dataSource>        </environment>    </environments></configuration>

4:定义表所对应的实体类

package bean;public class User {    private int id;    private String name;    private int age;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    }

5:定义操作users表的sql映射文件userMapper.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="bean.userMapper">    <!--         根据id查询得到一个user对象     -->     <select id="getUser" parameterType="int" resultType="bean.User">         select * from users where id=#{id}     </select></mapper>

6:在config.xml文件中注册userMapper.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>    <properties resource="db.properties"/>        <!--     development : 开发模式    work : 工作模式 -->    <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="${name}" />                <property name="password" value="${password}" />            </dataSource>        </environment>    </environments>        <mappers>        <mapper resource="bean/userMapper.xml"/>            </mappers></configuration>

7:编写测试类

package Test;import java.io.IOException;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import bean.User;public class Test {    public static void main(String[] args) throws IOException {        String resource = "conf.xml";         //加载mybatis的配置文件(它也加载关联的映射文件)        Reader reader = Resources.getResourceAsReader(resource);         //构建sqlSession的工厂        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);        //创建能执行映射文件中sql的sqlSession        SqlSession session = sessionFactory.openSession();        //映射sql的标识字符串                String statement = "bean.userMapper"+".getUser";                //执行查询返回一个唯一user对象的sql                User user = (User)session.selectOne(statement,1);                //User user = session.selectOne(statement, 1);                System.out.println(user.getName());    }}

 

 

Mybatis入门第一节