我爱编程

MyBatis概述和入门案例

2018-04-15  本文已影响0人  嗷老板

一、MyBatis的概述

MyBatis最初是Apache基金协会下面的一个开源框架叫做ibatis,之后代码托管到googleCode尚明正是更名为mybatis,最后代码托管到github上面。

jdbc编程的步骤
1、通过反射加载数据库驱动
2、创建数据库连接
3、定义我们的sql语句
4、接收我们的prepareStatement
5、为sql语句设置参数
6、执行sql语句
7、遍历结果集
8、关闭连接

jdbc编程存在的问题:
1、硬编码问题,代码修改困难,sql语句变更不方便,设置参数也不方便。
2、频繁的打开和关闭数据库连接,可以通过数据库连接池来解决
3、结果的获取很不方便,每次都需要遍历结果集

MyBatis的架构


MyBatis的架构

二、MyBatis的入门程序

1、创建数据库表

/*
SQLyog Ultimate v8.32 
MySQL - 5.6.22-log : Database - mybatis
*********************************************************************
*/


/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `mybatis`;

/*Table structure for table `orders` */

DROP TABLE IF EXISTS `orders`;

CREATE TABLE `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL COMMENT '下单用户id',
  `number` varchar(32) NOT NULL COMMENT '订单号',
  `createtime` datetime NOT NULL COMMENT '创建订单时间',
  `note` varchar(100) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  KEY `FK_orders_1` (`user_id`),
  CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

/*Data for the table `orders` */

insert  into `orders`(`id`,`user_id`,`number`,`createtime`,`note`) values (3,1,'1000010','2015-02-04 13:22:35',NULL),(4,1,'1000011','2015-02-03 13:22:41',NULL),(5,10,'1000012','2015-02-12 16:13:23',NULL);

/*Table structure for table `user` */

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) NOT NULL COMMENT '用户名称',
  `birthday` date DEFAULT NULL COMMENT '生日',
  `sex` char(1) DEFAULT NULL COMMENT '性别',
  `address` varchar(256) DEFAULT NULL COMMENT '地址',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;

/*Data for the table `user` */

insert  into `user`(`id`,`username`,`birthday`,`sex`,`address`) values (1,'王五',NULL,'2',NULL),(10,'张三','2014-07-10','1','北京市'),(16,'张小明',NULL,'1','河南郑州'),(22,'陈小明',NULL,'1','河南郑州'),(24,'张三丰',NULL,'1','河南郑州'),(25,'陈小明',NULL,'1','河南郑州'),(26,'王五',NULL,NULL,NULL);

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

2、导入需要使用的jar包:mysql、mybatis、slf4j、log4j

<dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <!-- mybatis 包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.13</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
    </dependencies>

3、修改配置文件:SqlMapConfig.xml、mapper.xml

  从帮助文档中找到配置文件内容进行修改。

SqlMapConfig.xml

帮助文档
Sql
mapper.xml
帮助文档

4、代码的实现

(1)创建User类

package demo01;

import java.util.Date;

public class User {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="
                + address + "]";
    }

}

(2)通过id查询数据

首先在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">
<mapper namespace="org.mybatis.example.BlogMapper">
    <select id="selectById" resultType="demo01.User">
        select * from user where id = #{id}
    </select>
</mapper>

然后在测试类中进行查询

package demo01;

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

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.Before;
import org.junit.Test;

public class MyBatisDemo01 {
    private SqlSession sqlSession;
    
    //before的意思是在所有test执行前执行的一段代码
    //我们把执行sql语句前的步骤放到before中去,减少代码量
    @Before
    public void init() throws IOException{
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = builder.build(Resources.getResourceAsStream("SqlMapConfig.xml"));
        sqlSession = build.openSession();
    }
    
    @Test
    public void getUserById() throws Exception{
        User user = sqlSession.selectOne("selectById", 16);
        System.out.println(user.toString());
        sqlSession.close();
    }
    
}
通过id查询数据

(3)通过用户名模糊查询数据

在mapper.xml文件中添加查询语句

<select id="selectByAddress" resultType="demo01.User">
    select * from user where address like '%${value}%'
</select>

在测试类中执行查询

@Test
    public void getUserByAddress() throws Exception{
        List<User> userList = sqlSession.selectList("selectByAddress", "河南");
        for (User user : userList) {
            System.out.println(user.toString());
        }
        sqlSession.close(); 
    }
通过用户名模糊查询数据

注意:

  我们通过观察可以看出进行模糊查询和具体查询的时候,上面两种查询有一些区别,下面来解释一下:

(4)插入数据

mappe.xml配置文件

    <insert id="insertUser" parameterType="demo01.User">
        insert into user (username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
    </insert>

测试代码

@Test
    public void insertUser() throws Exception{
        User user = new User();
        user.setUsername("赵六");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("河南郑州");
        sqlSession.insert("insertUser", user);
        //注意:更新、插入、删除记录都要提交数据到数据库
        sqlSession.commit();
        sqlSession.close(); 
    }

(5)更新数据

mapper.xml文件

    <update id="updateUser" parameterType="demo01.User">
        update user set address = #{address}
    </update>

测试代码

@Test
    public void updateUser() throws Exception {
        User user = sqlSession.selectOne("selectById", 16);
        user.setAddress("上海");
        sqlSession.update("updateUser", user);
        sqlSession.commit();
        sqlSession.close();
    }

(6)删除数据

mapper.xml配置文件

    <delete id="deleteUser" parameterType="int">
        delete from user where id = #{id}
    </delete>

测试代码

    @Test
    public void deleteUser() throws Exception {
        sqlSession.delete("deleteUser", 28);
        sqlSession.commit();
        sqlSession.close();
    }

(7)实现mysql自增主键返回

mapper.xml配置文件

    <!-- 
            插入数时候的时候实现主键返回,获取我们插入数据的主键
            keyProperty:指定我们返回的主键,封装到哪个字段当中
            resultType:表示指定我们返回的id是什么类型的
            order:表示什么时候获取我们的主键
            使用select  last_insert_id()来获取主键,适用于主键是自增长类型的。
         -->
    
    <insert id="idReturn">
        <selectKey keyProperty="id" resultType="int" order="AFTER">
            select last_insert_id();
        </selectKey>
        insert into user (username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
    </insert>

测试代码

@Test
    public void idResult() throws Exception {   
        User user = new User();
        //获取未设置前的id
        System.out.println("原始未设置的id为:"+user.getId());
        user.setUsername("田七");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("上海");
        sqlSession.insert("insertUser", user);
        sqlSession.commit();
        //插入数据后的id
        System.out.println("获取的id为:"+user.getId());
        sqlSession.close(); 
    }
自增主键
上一篇下一篇

猜你喜欢

热点阅读