一、mybatis简介:

1. Mybatis 开源免费框架.原名叫iBatis,2010 在google code,2013 年迁移到github
2. 作用: 数据访问层框架.
2.1 底层是对JDBC 的封装.
3. mybatis 优点之一:
3.1 使用mybatis 时不需要编写实现类,只需要写需要执行的sql 命令

二、mybatis的框架搭建:

1、建表,我使用的是mysql,建表语句如下:

CREATE TABLE `flower` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `price` double(10,0) NOT NULL,
  `production` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

INSERT INTO `flower` VALUES (1, '玫瑰花', 7, NULL);
INSERT INTO `flower` VALUES (3, '菊花', 5, NULL);

1、导入jar包

2、在src 下新建全局配置文件(编写JDBC 四个变量)

mybatis.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>
        <!-- default引用environment的id,当前所使用的环境 -->
    <environments default="default">
        <!-- 声明可以使用的环境 -->
        <environment id="default">
            <!-- 使用原生JDBC事务 -->
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/asiainfo/mapper/FlowerMapper.xml"/>
    </mappers>
</configuration>

3、编写pojo类

package com.asiainfo.pojo;

public class Flower {
    private int id;
    private String name;
    private double price;
    private String production;
    public Flower(int id, String name, double price, String production) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
        this.production = production;
    }
    public Flower() {
        super();
    }
    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 double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getProduction() {
        return production;
    }
    public void setProduction(String production) {
        this.production = production;
    }
    @Override
    public String toString() {
        return "Flower [id=" + id + ", name=" + name + ", price=" + price + ", production=" + production + "]";
    }
}

4、新建mapper接口为包,在包下新建实体类名+Mapper.xml文件

4.1文件的作用:编写执行的sql语句;把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">
<!-- namesapce:理解成实现类的全路径(包名+类名) -->
<mapper namespace="a.b" >
    <!-- id:方法名 
        parameterType:定义参数类型
        resultType:返回值类型.
        
        如果方法返回值是list,在resultType中写List的泛型,因为mybatis
        对jdbc封装,一行一行读取数据
    -->
    <select id="selAll" resultType="com.asiainfo.pojo.Flower">
        select * from flower
    </select>
    <select id="selById" resultType="int">
        select count(1) from flower where id=#{id}
    </select>
    <select id="selMap" resultType="com.asiainfo.pojo.Flower">
        select name,price,production from flower
    </select>
    <insert id="insert" parameterType="com.asiainfo.pojo.Flower">
        insert into flower(name, price,production) values(#{name}, #{price}, #{production})
    </insert>
</mapper>

5、写测试类:

package com.asiainfo.test;

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

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.asiainfo.pojo.Flower;

public class Test {
    public static void main(String[] args) throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession();
        // 第一种查询方式,返回一个列表
        List<Flower> list = session.selectList("a.b.selAll");
        for (Flower flower : list) {
            System.out.println(flower.toString());
        }
        // 第二种查询返回一条数据
        int num = session.selectOne("a.b.selById", 3);
        System.out.println(num);
        
        // 第三种查询返回一个以返回数据中某列为键的map集
        Map<String, Flower> selectMap = session.selectMap("a.b.selMap", "name");
        System.out.println(selectMap);
//        Flower flower = new Flower();
//        flower.setName("菊花");
//        flower.setPrice(4.6);
//        session.insert("a.b.insert", flower);
        session.close();
    }
}

测试结果

image