mybatis

Posted 大数据开发程序猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis相关的知识,希望对你有一定的参考价值。

SSM

-------------------------------

  java web 框架

  spring    //业务层框架

  spring MVC  //开发web程序应用的模块model+ view +controller模块化编程。展现部分放到一个模块中去,控制的部分放到一个文件中去,数据部分放到一个模块中去。

  Mybatis    //数据持久化。jcbc,mysql

 

一、mybatis

-------------------------------------------

  前身ibatis.

  

1.体验ibatis

----------------------------------

    1.创建项目和模块

    2.添加pom文件    

<?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.it18zhang</groupId>
    <artifactId>idea-bigdata-project4</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

    3.XML 配置文件(configuration XML)中包含了对 MyBatis 系统的核心设置,包含获取数据库连接实例的数据源(DataSource)和决定事务作用域和控制方式的事务管理器(TransactionManager)。XML 配置文件的详细内容后面再探讨,这里先给出一个简单的示例:

    打开mysql:创建数据库:create database mybatis ;  //
            create table users(id int primary_key,name varchar(20),age int)//创建表

    [resources/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="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

  4.创建库和表

  5.API编程测试连接

package com.it18zhang.mybatisdemo;

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 java.io.InputStream;

/**
 * Created by stone on 2018/9/19.
 */
public class App {
    public static void main(String[] args){
        try{
            //指定配置文件的路径(类路径)
            String resource = "mybatis-config.xml";
            //加载文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //创建回话工厂Builder,相当于连接池
            SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
            //通过sf开启会话,相当于打开连接
            SqlSession s=sf.openSession();
            System.out.println(s);

        } catch(Exception e ){
            e.printStackTrace();
        }
    }
}

  6.编写映射文件

    a.创建User类,和User表对应

package com.it18zhang.mybatisdemo.domain;

/**
 * Created by stone on 2018/9/19.
 */
public class User {
    private Integer id ;
    private String name ;
    private int age ;

    public Integer getId() {
        return id;
    }

    public void setId(Integer 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;
    }
}

    b.创建UserMapper.xml文件

    [com/it18zhang/mybatisdemo/domain/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="users ">
    <insert id="insert" >
        insert into users(name,age) values(#{NAME},#{age})
    </insert>
</mapper>

    c.通过映射来进行数据库的CRUD

package com.it18zhang.mybatisDemo;

import com.it18zhang.mybatisdemo.domain.User;
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 org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by stone on 2018/9/19.
 */
public class testCRUD {
    /*
    *
    * insert
    * */
    @Test
    public void insert(){
        try {
            //添加配置文件(加载类路径)
            String resources = "mybatis-config.xml";
            //加载文件
            InputStream inputStream = Resources.getResourceAsStream(resources);
            //创建会话工厂Builder,相当于连接池
            SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
            //通过sf开启会话,相当于打开连接
            SqlSession s = sf.openSession();
            User u = new User();
            u.setId(2);
            u.setName("jerry");
            u.setAge(13);
            s.insert("users.insert",u);
            s.commit();
            s.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    /*
    * uodate
    * */
    @Test
    public void update(){
        try {
            //添加配置文件(加载类路径)
            String resources = "mybatis-config.xml";
            //加载文件
            InputStream inputStream = Resources.getResourceAsStream(resources);
            //创建会话工厂Builder,相当于连接池
            SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
            //通过sf开启会话,相当于打开连接
            SqlSession s = sf.openSession();
            User u = new User();
            u.setId(2);
            u.setName("stone");
            u.setAge(23);
            s.update("users.update",u);
            s.commit();
            s.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*
    * SelectOne
    * */
    @Test
    public void selectOne(){
        try {
            //添加配置文件(加载类路径)
            String resources = "mybatis-config.xml";
            //加载文件
            InputStream inputStream = Resources.getResourceAsStream(resources);
            //创建会话工厂Builder,相当于连接池
            SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
            //通过sf开启会话,相当于打开连接
            SqlSession s = sf.openSession();
            User u = s.selectOne("users.selectOne",1);
            System.out.println(u.getName());
            s.commit();
            s.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    @Test
    public void selectAll(){
        try {
            //添加配置文件(加载类路径)
            String resources = "mybatis-config.xml";
            //加载文件
            InputStream inputStream = Resources.getResourceAsStream(resources);
            //创建会话工厂Builder,相当于连接池
            SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
            //通过sf开启会话,相当于打开连接
            SqlSession s = sf.openSession();
            List<User> users = s.selectList("users.selectAll");
            for(User u : users){
                System.out.println(u.getName()+":"+u.getAge());
            }
            s.commit();
            s.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

 

      

              

  

以上是关于mybatis的主要内容,如果未能解决你的问题,请参考以下文章

SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper

mybatis动态sql片段与分页,排序,传参的使用

MyBatis动态SQL标签用法

MYBATIS05_ifwherechoosewhentrimsetforEach标签sql片段

mybatis动态sql之利用sql标签抽取可重用的sql片段

[mybatis]动态sql_sql_抽取可重用的sql片段