首页 > 代码库 > mybatis快速入门,mybatis简单实例, 如何使用mybatis

mybatis快速入门,mybatis简单实例, 如何使用mybatis

目录结构:

技术分享

 

 

1. 导入所需要的包

技术分享

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. 建立相应的 javabean

package com.loger.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;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    public User(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public User() {
        // TODO Auto-generated constructor stub
    }
}

4. 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="apple" />
            </dataSource>
        </environment>
    </environments>
    <!-- 注册 mapper -->
    <mappers>
        <mapper resource="mapper.xml"/>
    </mappers>
</configuration>

5. 创建对数据库具体操作的 mapper.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">
<!-- namespace 是这个 mapper 的一个名称 -->
<mapper namespace="userMapper">
    <!-- 这是一个简单的查询操作, 操作的 id 是 getUser, parameterType 是查询所根据的参数 -->
    <select id="getUser" parameterType="int"
        resultType="com.loger.bean.User">
        select * from users where id=#{id}
    </select>
</mapper>

6. Main.java

package com.loger.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 com.loger.bean.User;

public class Main {
    public static void main(String[] args) throws IOException {
        
        //加载配置文件
        Reader reader = Resources.getResourceAsReader("mybatis-conf.xml");
        
        //构建 sqlSession 的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        
        //使用该工厂
        SqlSession session = sessionFactory.openSession();
        
        //查询
        String statement = "userMapper.getUser";
        int id = 1;
        User user = session.selectOne(statement, id);
        System.out.println(user.toString());
    }
}

 

运行结果:

技术分享

 

mybatis快速入门,mybatis简单实例, 如何使用mybatis