mybatis笔记<一> Demo

mybatis作为一个orm互联网公司基本都在用,今天写个笔记。记录一下mybatis使用

参考官网:http://www.mybatis.org/mybatis-3/getting-started.html

这里使用的mysql, 建了一个数据库db_test,表person,字段id, name, age。插了一条数据,id为1。demo 主要功能从mysql中读取这条记录。

mybatis主要通过读取配置文件xml,初始化得到SqlSessionFactory, 通过facotry拿到session,然后操作数据库。这里具体还要看下源码,后面在做个笔记记录一下。

同时会写下集成spring后的用法

代码:https://github.com/luckygxf/MybatisDemo

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.gxf.mybatis</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
    </dependencies>

</project>

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/db_test"/>
                <property name="username" value="root"/>
                <property name="password" value="luckygxf"/>
            </dataSource>
        </environment>
    </environments>
    <!--<mappers>-->
        <!--<mapper resource="mapper/PersonMapper.xml"/>-->
    <!--</mappers>-->
</configuration>

Person.java

package com.gxf.mybatis.util;

public class Person {
    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 "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
PersonMapper.java
package com.gxf.mybatis.mapper;

import com.gxf.mybatis.util.Person;

public interface PersonMapper {

    Person selectPerson(int id);
}

PersonMapper1.java

package com.gxf.mybatis.mapper;

import com.gxf.mybatis.util.Person;
import org.apache.ibatis.annotations.Select;

public interface PersonMapper1 {
    @Select("SELECT * FROM person WHERE id = #{idss}")
    Person selectPerson(int idss);
}
SqlSessionFactoryHelper.java
package com.gxf.mybatis.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class SqlSessionFactoryHelper {

    public static SqlSessionFactory getSqlSessionFactory() {
        SqlSessionFactory sqlSessionFactory = null;
        try{
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
        return sqlSessionFactory;
    }
}

测试类mai方法

package com.gxf.mybatis.util;

import com.gxf.mybatis.mapper.PersonMapper;
import com.gxf.mybatis.mapper.PersonMapper1;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class PersonTableOps {
    private static SqlSessionFactory sqlSessionFactory = SqlSessionFactoryHelper.getSqlSessionFactory();

    public static void main(String[] args) {
//        selectOne1();
//        selectOne2();
        selectOne3();
    }

    /**
     * 第一种方式
     * */
    public static void selectOne1(){
        SqlSession session = sqlSessionFactory.openSession();
        try {
            Person person = session.selectOne(
                    "com.gxf.mybatis.mapper.PersonMapper.selectPerson", 1);
            System.out.println(person);
        } finally {
            session.close();
        }
    }

    /**
     * 第二种方式
     * */
    public static void selectOne2(){
        SqlSession session = sqlSessionFactory.openSession();
        try {
            PersonMapper mapper = session.getMapper(PersonMapper.class);
            Person person = mapper.selectPerson(1);
            System.out.println(person);
        } finally {
            session.close();
        }

    }

    /**
     * 第三种方式
     * */
    public static void selectOne3(){
        SqlSession session = sqlSessionFactory.openSession();
        try {
            sqlSessionFactory.getConfiguration().addMapper(PersonMapper1.class);
            PersonMapper1 mapper = session.getMapper(PersonMapper1.class);
            Person person = mapper.selectPerson(1);
            System.out.println(person);
        } finally {
            session.close();
        }
    }
}

 

posted on 2018-08-21 00:27  luckygxf  阅读(169)  评论(0编辑  收藏  举报

导航