这里介绍的mybatis比较简单, 我做为一个初学者, 记录下个人在学习中方法, 如果那里出错, 希望读者朋友们见谅.

首先这里介绍一下我们下面用的表结构:

Mybatis介绍(一)

author表是保存了作者的个人信息, 因为我们在这里做测试, 所以就简单的定义几个字段.

blog表是保存谁写了博客的内容, 这里也是几个简单的字段.

comment表是保存对哪篇博客评论, 也是几个简单的字段.

注意: 这三张表的id都是自增型, 你也可以做其他的改变, 这里是为了方便.

下面给出了几张表格创建的sql语句:

CREATE TABLE `author` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `name` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
   `age` int(5) NULL DEFAULT NULL,
   `email` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
   `country` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
   PRIMARY KEY (`id`) 
);

CREATE TABLE `blog` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `authorid` int(11) NULL DEFAULT NULL,
   `title` varchar(35) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
   `mainbody` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
   `creattime` varchar(70) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
   PRIMARY KEY (`id`) 
);

CREATE TABLE `comment` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `blogid` int(11) NULL DEFAULT NULL,
   `content` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
   `creattime` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
   PRIMARY KEY (`id`) 
);

 使用maven+springmvc+mabatis+spring搭建web环境, 可以参考博客: https://www.cnblogs.com/jay36/p/7762448.html, 这里就不详细的介绍了.

 这里主要介绍mybatis的用法, 首先使用Mybatis Generator生成pojo/dao/mapping三个文件, 即实体类、DAO接口和Mapping映射文件.

 可以参考博客:  https://blog.csdn.net/zhshulin/article/details/23912615, 下面就开始简单介绍Mybatis的使用.

 1.  看一下三个表pojo(Plain Ordinary Java Object), 即实体类

package com.springdemo.pojo;
public class Author {
    private Integer id;
    private String name;
    private Integer age;
    private String email;
    private String country; 
    /* getting and setting function */
}
package com.springdemo.pojo;
public class Blog {
    private Integer id;
    private Integer authorid;
    private String title;
    private String creattime;
    private String mainbody;
    /* getting and setting function */
}
package com.springdemo.pojo;
public class Comment {
    private Integer id;
    private Integer blogid;
    private String creattime;
    private String content;
    /* getting and setting function */
}
View Code

相关文章: