环境准备
- JDK1.8
- mybatis的版本3.4.4
- 编辑器idea(或者eclipse)
- maven
数据库脚本
/*
Navicat MySQL Data Transfer
Source Server : mysql
Source Server Version : 50045
Source Host : 127.0.0.1:3306
Source Database : mybatis
Target Server Type : MYSQL
Target Server Version : 50045
File Encoding : 65001
Date: 2017-06-16 23:32:50
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for author
-- ----------------------------
DROP TABLE IF EXISTS `author`;
CREATE TABLE `author` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
`email` varchar(20) NOT NULL,
`bio` varchar(20) NOT NULL,
`favourite_section` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1002 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of author
-- ----------------------------
INSERT INTO `author` VALUES ('1001', '林枫', '123456', '824156593@qq.com', '合肥的小男孩', '旅游');
-- ----------------------------
-- Table structure for blog
-- ----------------------------
DROP TABLE IF EXISTS `blog`;
CREATE TABLE `blog` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(20) NOT NULL,
`author_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of blog
-- ----------------------------
INSERT INTO `blog` VALUES ('1', '小说博客', '1001');
-- ----------------------------
-- Table structure for post
-- ----------------------------
DROP TABLE IF EXISTS `post`;
CREATE TABLE `post` (
`id` int(11) NOT NULL auto_increment,
`blog_id` int(11) NOT NULL,
`author_id` int(11) NOT NULL,
`created_on` date NOT NULL,
`section` varchar(20) NOT NULL,
`subject` varchar(20) NOT NULL,
`draft` varchar(20) NOT NULL,
`body` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of post
-- ----------------------------
INSERT INTO `post` VALUES ('1', '1', '1001', '2017-06-16', '旅游', '玄幻', '草稿', '绝世武神');
INSERT INTO `post` VALUES ('2', '1', '1001', '2017-06-16', '旅游', '玄幻', '草稿', '大主宰');
INSERT INTO `post` VALUES ('3', '1', '1001', '2017-06-16', '旅游', '玄幻', '草稿', '灵域');
工程环境
- 下载mybatis的源码
为了便于研究和写注释,我们需要下载mybatis的源代码文件,地址为
https://github.com/mybatis/mybatis-3 下载完成之后,解压。 - 新建一个maven工程
使用编辑器新建一个maven工程,导入刚才的mybatis源码,修改源码的pom文件 ,修改其中的<parent> </parent>
节点里面的内容为自己工程的父节点。
去除依赖
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.21.0-GA</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
修改依赖的范围
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.14</version>
<!--<scope>compile</scope>-->
<!--<optional>true</optional>-->
</dependency>
- 在新建一个model
maven的pom文件
<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">
<parent>
<artifactId>mybatis3-Learning</artifactId>
<groupId>com.lf</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis3</artifactId>
<packaging>jar</packaging>
<name>mybatis3</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</project>
此处的mybatis版本引用刚才下载的源码
4. 新建实体类
package com.lf.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@NoArgsConstructor
public class Author {
private Integer id;
private String username;
private String password;
private String email;
private String bio;
private String favouriteSection;
}
package com.lf.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@NoArgsConstructor
public class Blog {
private Integer id;
private String title;
private Integer authorId;
}
package com.lf.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.Date;
@Data
@ToString
@NoArgsConstructor
public class Post {
private Integer id;
private Integer blogId;
private Integer authorId;
private Date createdOn;
private String section;
private String subject;
private String draft;
private String body;
}
- mybatis的全局配置文件
<?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>
<!-- 方法一: 从外部指定properties配置文件, 除了使用resource属性指定外,还可通过url属性指定url-->
<!--<properties resource="dbConfig.properties" ></properties>-->
<!-- 方法二: 直接配置为xml -->
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="adminter"/>
</properties>
<typeAliases>
<!--
通过package, 可以直接指定package的名字, mybatis会自动扫描你指定包下面的javabean,
并且默认设置一个别名,默认的名字为: javabean 的首字母小写的非限定类名来作为它的别名。
也可在javabean 加上注解@Alias 来自定义别名, 例如: @Alias(user)
-->
<package name="com.lf.entity"/>
</typeAliases>
<typeHandlers>
<!--当配置package的时候,mybatis会去配置的package扫描TypeHandler-->
<package name="com.lf.typehandle"/>
<!--handler属性直接配置我们要指定的TypeHandler-->
<!--<typeHandler handler="com.lf.typehandle.BaseEnumTypeHandle" javaType="com.lf.dict.Gender"/>-->
</typeHandlers>
<plugins>
<plugin interceptor="com.lf.plugin.MyBatisPlugin">
<property name="name" value="你好"></property>
</plugin>
</plugins>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--
如果上面没有指定数据库配置的properties文件,那么此处可以这样直接配置
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test1"/>
<property name="username" value="root"/>
<property name="password" value="adminter"/>
-->
<!-- 上面指定了数据库配置文件, 配置文件里面也是对应的这四个属性 -->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<!-- 我再指定一个environment -->
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!-- 与上面的url不一样 -->
<property name="url" value="jdbc:mysql://localhost:3306/demo"/>
<property name="username" value="root"/>
<property name="password" value="adminter"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.lf.dao"></package>
</mappers>
</configuration>
- 对应的mapper文件
<?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="com.lf.dao.AuthorMapper">
<resultMap id="BaseResultMap" type="com.lf.entity.Author">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="bio" jdbcType="VARCHAR" property="bio" />
<result column="favourite_section" jdbcType="VARCHAR" property="favouriteSection" />
</resultMap>
<sql id="Base_Column_List">
id, username, password, email, bio, favourite_section
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from author
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from author
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.lf.entity.Author">
insert into author (id, username, password,
email, bio, favourite_section
)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{bio,jdbcType=VARCHAR}, #{favouriteSection,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.lf.entity.Author">
insert into author
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="username != null">
username,
</if>
<if test="password != null">
password,
</if>
<if test="email != null">
email,
</if>
<if test="bio != null">
bio,
</if>
<if test="favouriteSection != null">
favourite_section,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
<if test="email != null">
#{email,jdbcType=VARCHAR},
</if>
<if test="bio != null">
#{bio,jdbcType=VARCHAR},
</if>
<if test="favouriteSection != null">
#{favouriteSection,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.lf.entity.Author">
update author
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="email != null">
email = #{email,jdbcType=VARCHAR},
</if>
<if test="bio != null">
bio = #{bio,jdbcType=VARCHAR},
</if>
<if test="favouriteSection != null">
favourite_section = #{favouriteSection,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.lf.entity.Author">
update author
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
bio = #{bio,jdbcType=VARCHAR},
favourite_section = #{favouriteSection,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
<?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="com.lf.dao.BlogMapper">
<resultMap id="BaseResultMap" type="com.lf.entity.Blog">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="title" jdbcType="VARCHAR" property="title" />
<result column="author_id" jdbcType="INTEGER" property="authorId" />
</resultMap>
<sql id="Base_Column_List">
id, title, author_id
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from blog
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from blog
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.lf.entity.Blog">
insert into blog (id, title, author_id
)
values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{authorId,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.lf.entity.Blog">
insert into blog
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="title != null">
title,
</if>
<if test="authorId != null">
author_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="title != null">
#{title,jdbcType=VARCHAR},
</if>
<if test="authorId != null">
#{authorId,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.lf.entity.Blog">
update blog
<set>
<if test="title != null">
title = #{title,jdbcType=VARCHAR},
</if>
<if test="authorId != null">
author_id = #{authorId,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.lf.entity.Blog">
update blog
set title = #{title,jdbcType=VARCHAR},
author_id = #{authorId,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
<?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="com.lf.dao.PostMapper">
<resultMap id="BaseResultMap" type="com.lf.entity.Post">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="blog_id" jdbcType="INTEGER" property="blogId" />
<result column="author_id" jdbcType="INTEGER" property="authorId" />
<result column="created_on" jdbcType="DATE" property="createdOn" />
<result column="section" jdbcType="VARCHAR" property="section" />
<result column="subject" jdbcType="VARCHAR" property="subject" />
<result column="draft" jdbcType="VARCHAR" property="draft" />
<result column="body" jdbcType="VARCHAR" property="body" />
</resultMap>
<sql id="Base_Column_List">
id, blog_id, author_id, created_on, section, subject, draft, body
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap" statementType="STATEMENT">
select
<include refid="Base_Column_List" />
from post
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from post
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.lf.entity.Post">
insert into post (id, blog_id, author_id,
created_on, section, subject,
draft, body)
values (#{id,jdbcType=INTEGER}, #{blogId,jdbcType=INTEGER}, #{authorId,jdbcType=INTEGER},
#{createdOn,jdbcType=DATE}, #{section,jdbcType=VARCHAR}, #{subject,jdbcType=VARCHAR},
#{draft,jdbcType=VARCHAR}, #{body,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.lf.entity.Post">
insert into post
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="blogId != null">
blog_id,
</if>
<if test="authorId != null">
author_id,
</if>
<if test="createdOn != null">
created_on,
</if>
<if test="section != null">
section,
</if>
<if test="subject != null">
subject,
</if>
<if test="draft != null">
draft,
</if>
<if test="body != null">
body,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="blogId != null">
#{blogId,jdbcType=INTEGER},
</if>
<if test="authorId != null">
#{authorId,jdbcType=INTEGER},
</if>
<if test="createdOn != null">
#{createdOn,jdbcType=DATE},
</if>
<if test="section != null">
#{section,jdbcType=VARCHAR},
</if>
<if test="subject != null">
#{subject,jdbcType=VARCHAR},
</if>
<if test="draft != null">
#{draft,jdbcType=VARCHAR},
</if>
<if test="body != null">
#{body,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.lf.entity.Post">
update post
<set>
<if test="blogId != null">
blog_id = #{blogId,jdbcType=INTEGER},
</if>
<if test="authorId != null">
author_id = #{authorId,jdbcType=INTEGER},
</if>
<if test="createdOn != null">
created_on = #{createdOn,jdbcType=DATE},
</if>
<if test="section != null">
section = #{section,jdbcType=VARCHAR},
</if>
<if test="subject != null">
subject = #{subject,jdbcType=VARCHAR},
</if>
<if test="draft != null">
draft = #{draft,jdbcType=VARCHAR},
</if>
<if test="body != null">
body = #{body,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.lf.entity.Post">
update post
set blog_id = #{blogId,jdbcType=INTEGER},
author_id = #{authorId,jdbcType=INTEGER},
created_on = #{createdOn,jdbcType=DATE},
section = #{section,jdbcType=VARCHAR},
subject = #{subject,jdbcType=VARCHAR},
draft = #{draft,jdbcType=VARCHAR},
body = #{body,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>