数据库表

数据库叫blog_gp1701

author表

数据

blog表

数据

comment表

数据

post表

数据

vechcle

目录结构

jar包导入

先给对应的jar包导入

建立一个junit单元测试

配置文件

log4j.properties

### \u914D\u7F6E\u6839 ###
log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE### \u8BBE\u7F6E\u8F93\u51FAsql\u7684\u7EA7\u522B\uFF0C\u5176\u4E2Dlogger\u540E\u9762\u7684\u5185\u5BB9\u5168\u90E8\u4E3Ajar\u5305\u4E2D\u6240\u5305\u542B\u7684\u5305\u540D ###
log4j.logger.org.apache=dubug
log4j.logger.java.sql.Connection=dubug
log4j.logger.java.sql.Statement=dubug
log4j.logger.java.sql.PreparedStatement=dubug
log4j.logger.java.sql.ResultSet=dubug### \u914D\u7F6E\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{1}:%L - %m%n

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>
<settings><setting name="useGeneratedKeys" value="true"/>
</settings>
<typeAliases><!--  <typeAlias type="com.geyao.mybatis.pojo.Blog" alias="Blog"/><typeAlias type="com.geyao.mybatis.pojo.Author" alias="Author"/>--><package name="com.geyao.mybatis.pojo"/><package name="com.geyao.mybatis.mvo"/>
</typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC" /><!-- 配置数据库连接信息 --><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/blog_gp1701?serverTimezone=GMT%2B8" /><property name="username" value="root" /><property name="password" value="123" /></dataSource></environment></environments><mappers><!-- 注册userMapper.xml文件, userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml--><mapper resource="com/geyao/mybatis/mapper/BlogMapper.xml"/><mapper resource="com/geyao/mybatis/mapper/AuthorMapper.xml"/><mapper resource="com/geyao/mybatis/mapper/BlogMapperCustom.xml"/><mapper resource="com/geyao/mybatis/mapper/VehicleMapper.xml"/></mappers></configuration>

com.geyao.mybatis.mapper

AuthorMapper

package com.geyao.mybatis.mapper;import com.geyao.mybatis.pojo.Author;
public interface AuthorMapper {Author selectAuthorById(Integer id);
}

BlogMapper

package com.geyao.mybatis.mapper;import java.util.List;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlogById(Integer id);List<Blog> selectBlogList();List<Blog> selectBlogListNested();Blog selectBlogByIdConstructor(Integer id);
}

Comment.Maoper

package com.geyao.mybatis.mapper;import java.util.List;import com.geyao.mybatis.pojo.Comment;public interface CommentMapper {
List<Comment> selectCommentByPostId(Integer id);
}

PostMapper

package com.geyao.mybatis.mapper;import com.geyao.mybatis.pojo.Post;public interface PostMapper {Post selectPostById(Integer id);
}

AuthorMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)-->
<mapper namespace="com.geyao.mybatis.mapper.AuthorMapper"><resultMap type="Author" id="authorResultMap"><id column="id" property="id" jdbcType="INTEGER"></id></resultMap><select id="selectAuthorById" parameterType="int" resultMap="authorResultMap">select * from author where id=#{id}</select>
</mapper>

BlogMapper.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="com.geyao.mybatis.mapper.BlogMapper"><resultMap type="Blog" id="blogResultMap"><id column="id" property="id" jdbcType="INTEGER"></id>                                     <association property="author" column="author" javaType="Author"select="com.geyao.mybatis.mapper.AuthorMapper.selectAuthorById"></association></resultMap><select id="selectBlogById" parameterType="int" resultMap="blogResultMap">select * from Blog where id=#{id}</select><select id="selectBlogList"  resultMap="blogResultMap">select * from Blog </select><resultMap type="Blog" id="blogResultMapNested"><id column="blog_id" property="id"></id><result column="blog_title" property="title"></result><result column="blog_state" property="state"></result><result column="blog_featured" property="featured"></result><result column="blog_style" property="style"></result><association column="blog_atuthor" property="author" javaType="Author"><id column="author_id" property="id"></id><result column="author_username" property="username"></result><result column="author_password" property="password"></result><result column="author_email" property="email"></result><result column="author_bio" property="bio"></result><result column="blog_favouriteSection" property="favouriteSection"></result><result column="blog_nickname" property="nickname"></result><result column="blog_realname" property="realname"></result></association></resultMap><select id="selectBlogListNested" resultMap="blogResultMapNested">SELECTb.id AS blog_id,b.title AS blog_title,b.author AS blog_author,b.state AS blog_state,b.featured AS blog_featured,b.style AS blog_style,a.id AS author_id,a.username AS author_username,a.password AS author_password,a.email AS author_email,a.bio AS author_bio,a.favouriteSection AS author_favouriteSection,a.nickname AS author_nickname,a.realname AS author_realnameFROM blog bLEFT JOIN author aON b.author= a.id</select><resultMap type="Blog" id="blogResultMapConstructor"><constructor><idArg column="id" javaType="int"></idArg><arg column="title" javaType="string"></arg></constructor></resultMap><select id="selectBlogByIdConstructor" parameterType="int" resultMap="blogResultMapConstructor">select * from blog where id= #{id}</select>
</mapper>

CommentMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)-->
<mapper namespace="com.geyao.mybatis.mapper.CommentMapper"><resultMap type="Comment" id="commentResultMap"><id column="id" property="id" jdbcType="INTEGER"></id></resultMap><select id="selectCommentByPostId" parameterType="int" resultMap="commentResultMap">select * from comment where post_id=#{postId}</select><select id="selectCommentById" parameterType="int" resultMap="commentResultMap">select * from comment where id=#{id}</select>
</mapper>

PostMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)-->
<mapper namespace="com.geyao.mybatis.mapper.PostMapper"><resultMap type="Post" id="postResultMap"><id column="id" property="id" jdbcType="INTEGER"></id><collection property="commentList" column="id" javaType="ArrayList" ofType="Comment"select="com.geyao.mybatis.mapper.CommentMapper.selectCommentByPostId"></collection></resultMap><select id="selectPostById" parameterType="int" resultMap="postResultMap">select * from post where id=#{id}</select>
</mapper>

VehicleMapper.

package com.geyao.mybatis.mapper;import com.geyao.mybatis.pojo.Vehicle;public interface VehicleMapper {Vehicle selectVehicleById(Integer id);
}
package com.geyao.mybatis.mapper;import com.geyao.mybatis.pojo.Vehicle;public interface VehicleMapper {Vehicle selectVehicleById(Integer id);
}

VehicleMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)-->
<mapper namespace="com.geyao.mybatis.mapper.VehicleMapper"><resultMap type="Vehicle" id="vehicleResultMap"><id column="id" property="id" jdbcType="INTEGER"></id><discriminator javaType="int" column="vehicle_type"><case value="1" resultType="Car"><result column="door_count" property="doorCount"></result></case><case value="2" resultType="Suv"><result column="all_wheel_drive" property="allWheelDrive"></result></case></discriminator></resultMap><select id="selectVehicleById" parameterType="int" resultMap="vehicleResultMap">select * from vehicle where id=#{id}</select>
</mapper>

com.geyao.mybatis.pojo

vehicle类

package com.geyao.mybatis.pojo;import java.util.Date;public abstract class Vehicle {private Integer id;private String vin;private Date year;private String make;private String model;private String color;public Vehicle() {}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getVin() {return vin;}public void setVin(String vin) {this.vin = vin;}public Date getYear() {return year;}public void setYear(Date year) {this.year = year;}public String getMake() {return make;}public void setMake(String make) {this.make = make;}public String getModel() {return model;}public void setModel(String model) {this.model = model;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}@Overridepublic String toString() {return "Vehicle [id=" + id + ", vin=" + vin + ", year=" + year + ", make=" + make + ", model=" + model+ ", color=" + color + "]";}}

Car类

package com.geyao.mybatis.pojo;public class Car extends Vehicle {private Integer doorCount;public Car() {}public Integer getDoorCount() {return doorCount;}public void setDoorCount(Integer doorCount) {this.doorCount = doorCount;}}

Suv

package com.geyao.mybatis.pojo;public class Suv extends Vehicle {private Boolean allWheelDrive;public Suv() {}public Boolean getAllWheelDrive() {return allWheelDrive;}public void setAllWheelDrive(Boolean allWheelDrive) {this.allWheelDrive = allWheelDrive;}@Overridepublic String toString() {return "Suv [allWheelDrive=" + allWheelDrive + "]";}}

Author

package com.geyao.mybatis.pojo;public class Author {private Integer id;private String username;private String password;private String email;private String bio;private String favouriteSection;private String nickname;private String realname;public Author() {super();}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 String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getBio() {return bio;}public void setBio(String bio) {this.bio = bio;}public String getFavouriteSection() {return favouriteSection;}public void setFavouriteSection(String favouriteSection) {this.favouriteSection = favouriteSection;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}public String getRealname() {return realname;}public void setRealname(String realname) {this.realname = realname;}public Author(Integer id, String username, String password, String email, String bio, String favouriteSection,String nickname, String realname) {super();this.id = id;this.username = username;this.password = password;this.email = email;this.bio = bio;this.favouriteSection = favouriteSection;this.nickname = nickname;this.realname = realname;}@Overridepublic String toString() {return "Author [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", bio="+ bio + ", favouriteSection=" + favouriteSection + ", nickname=" + nickname + ", realname=" + realname+ "]";}}

Blog

package com.geyao.mybatis.pojo;import java.io.Serializable;public class Blog implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String title;private Author author;private String state;private boolean featured;private String style;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public Author getAuthor() {return author;}public void setAuthor(Author author) {this.author = author;}public String getState() {return state;}public void setState(String state) {this.state = state;}public boolean isFeatured() {return featured;}public void setFeatured(boolean featured) {this.featured = featured;}public String getStyle() {return style;}public void setStyle(String style) {this.style = style;}public static long getSerialversionuid() {return serialVersionUID;}@Overridepublic String toString() {return "Blog [id=" + id + ", title=" + title + ", author=" + author + ", state=" + state + ", featured="+ featured + ", style=" + style + "]\n";}public Blog(Integer id, String title) {super();this.id = id;this.title = title;System.out.println("构造方法执行中。。");}}

Comment

package com.geyao.mybatis.pojo;import java.util.Date;public class Comment {private Integer id;private String name;private String comment;private Date createOn;private Post post;private Author author;public Comment() {}public Comment(Integer id, String name, String comment, Date createOn, Post post, Author author) {super();this.id = id;this.name = name;this.comment = comment;this.createOn = createOn;this.post = post;this.author = author;}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 String getComment() {return comment;}public void setComment(String comment) {this.comment = comment;}public Date getCreateOn() {return createOn;}public void setCreateOn(Date createOn) {this.createOn = createOn;}public Post getPost() {return post;}public void setPost(Post post) {this.post = post;}public Author getAuthor() {return author;}public void setAuthor(Author author) {this.author = author;}@Overridepublic String toString() {return "Comment [id=" + id + ", name=" + name + ", comment=" + comment + ", createOn=" + createOn + ", post="+ post + ", author=" + author + "]";}}

Post

package com.geyao.mybatis.pojo;import java.util.Date;
import java.util.List;public class Post {private Integer id;private Author author;private Blog blog;private Date createOn;private String section;private String subject;private String draft;private String body;private Integer visit;private List<Comment> commentList;public Post(Integer id, Author author, Blog blog, Date createOn, String section, String subject, String draft,String body, Integer visit, List<Comment> commentList) {super();this.id = id;this.author = author;this.blog = blog;this.createOn = createOn;this.section = section;this.subject = subject;this.draft = draft;this.body = body;this.visit = visit;this.commentList = commentList;}public Post() {super();}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public Author getAuthor() {return author;}public void setAuthor(Author author) {this.author = author;}public Blog getBlog() {return blog;}public void setBlog(Blog blog) {this.blog = blog;}public Date getCreateOn() {return createOn;}public void setCreateOn(Date createOn) {this.createOn = createOn;}public String getSection() {return section;}public void setSection(String section) {this.section = section;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getDraft() {return draft;}public void setDraft(String draft) {this.draft = draft;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}public Integer getVisit() {return visit;}public void setVisit(Integer visit) {this.visit = visit;}public List<Comment> getCommentList() {return commentList;}public void setCommentList(List<Comment> commentList) {this.commentList = commentList;}@Overridepublic String toString() {return "Post [id=" + id + ", author=" + author + ", blog=" + blog + ", createOn=" + createOn + ", section="+ section + ", subject=" + subject + ", draft=" + draft + ", body=" + body + ", visit=" + visit + "]";}}

com.geyao.mybatis.util

MybatisUtil

package com.geyao.mybatis.util;import java.io.InputStream;
import java.io.Reader;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisUtil {private static SqlSessionFactory sqlSessionFactory =null;static {try {InputStream in = Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}private MyBatisUtil() {}public static SqlSession getSqlSession() {return sqlSessionFactory.openSession();}
}

单元测试

com.geyao.mybatis.mapper

PostMannerTest

package com.geyao.mybatis.mapper;import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import com.geyao.mybatis.pojo.Blog;
import com.geyao.mybatis.pojo.Post;
import com.geyao.mybatis.util.MyBatisUtil;public class PostMapperTest {@Testpublic void testSelectPost() {SqlSession session =MyBatisUtil.getSqlSession();PostMapper postMapper =session.getMapper(PostMapper.class);Post post = postMapper.selectPostById(1);session.close();System.out.println(post);}
}

BlogMapperTest类

package com.geyao.mybatis.mapper;import java.util.List;import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import com.geyao.mybatis.pojo.Author;
import com.geyao.mybatis.pojo.Blog;
import com.geyao.mybatis.pojo.Post;
import com.geyao.mybatis.util.MyBatisUtil;public class BlogMapperTest {@Testpublic void testselectBlogListNested() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> biogList = blogMapper.selectBlogListNested();session.close();System.out.println(biogList);}@Testpublic void testselectBlogByIdConstructor() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog biog = blogMapper.selectBlogByIdConstructor(1);session.close();System.out.println(biog);}}

VehicleMapperTest类

package com.geyao.mybatis.mapper;import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import com.geyao.mybatis.mvo.BlogCustom;
import com.geyao.mybatis.pojo.Car;
import com.geyao.mybatis.pojo.Suv;
import com.geyao.mybatis.util.MyBatisUtil;public class VehicleMapperTest {@Testpublic void testselectVehicleById() {SqlSession session =MyBatisUtil.getSqlSession();VehicleMapper vehicleMapper =session.getMapper(VehicleMapper.class);Car vehicle1=(Car)vehicleMapper.selectVehicleById(1);Suv vehicle3=(Suv)vehicleMapper.selectVehicleById(3);session.close();System.out.println(vehicle1);System.out.println(vehicle3);}
}

运行结果

mybatis学习(54):鉴定器相关推荐

  1. Mybatis resultMap discriminator(鉴定器) 实操

    文章目录 实体对象 Mapper 文件配置 测试用例 接口调用响应 数据库数据 数据库表结构 实体对象 @Data public class SysUser {private Long id;priv ...

  2. MyBatis学习之映射器Mapper(接口映射器+xml映射文件)

    Table of Contents 01 MyBatis映射器: 1.1 接口映射器+xml映射器 1.2 接口映射器+注解 02 接口映射器+xml映射器  方式 2.1 mybatis配置文件 引 ...

  3. 【转】MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突

    [转]MyBatis学习总结(四)--解决字段名与实体类属性名不相同的冲突 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况下的如何解决字段名与实体 ...

  4. 事务中mybatis通过id查不到但是通过其他条件可以查到_40打卡 MyBatis 学习

    第57次(mybatis) 学习主题:mybatis 学习目标: 1 掌握框架的概念 2 掌握mybatis环境搭建 对应视频: http://www.itbaizhan.cn/course/id/8 ...

  5. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    上一篇博文MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...

  6. MyBatis学习--简单的增删改查

    jdbc程序 在学习MyBatis的时候先简单了解下JDBC编程的方式,我们以一个简单的查询为例,使用JDBC编程,如下: 1 Public static void main(String[] arg ...

  7. MyBatis多参数传递之Map方式示例——MyBatis学习笔记之十三

    前面的文章介绍了MyBatis多参数传递的注解.参数默认命名等方式,今天介绍Map的方式.仍然以前面的分页查询教师信息的方法findTeacherByPage为例(示例源代码下载地址:http://d ...

  8. MyBatis学习随记

    1       Mybatis入门 1.1     单独使用jdbc编程问题总结 1.1.1  jdbc程序 Public static void main(String[] args) { Conn ...

  9. MyBatis多参数传递之混合方式——MyBatis学习笔记之十五

    在本系列文章的<MyBatis多参数传递之Map方式示例>一文中,网友mashiguang提问如下的方法如何传递参数:public List findStudents(Map condit ...

最新文章

  1. 【C#串口编程计划】C#通讯类库构建
  2. 独家|OpenCV 1.3 矩阵的掩膜操作(附链接)
  3. 企业网远程协助的小能手
  4. (转)电脑程序员才能看懂的笑话
  5. 什么是 Spring?
  6. python中表示单一数据的类型被称为_各种Python数据类型的完整列表
  7. 如何修改elementUI里面Dialog组件标题的样式
  8. [js] axios拦截器原理是什么?
  9. [ACM]HDU Problem 2000 + Java
  10. 传输层 传输层协议和应用层协议之间的关系
  11. dell服务器全系列手册,DELL服务器 RAID配置中文手册.pdf
  12. CMD命令查看WiFi密码
  13. 安全牛3·15期间遭黑客攻击 阿里云成功抵御
  14. 计算机二级考试python+C语言通过记录
  15. 0基础学3dmax建模难吗?
  16. 2021年数维杯数学建模分析和思路——C题
  17. 问卷分析之SPSS相关分析、相关系数矩阵(Pearson)
  18. 机器学习推荐系统记录
  19. 高等数学——多元函数的极值的条件
  20. 于博士cadence教程讲解内容

热门文章

  1. 夺命雷公狗jquery---22-bind为jquery对象绑定多个相关事件
  2. 75个移动App开发教程
  3. Abraca:XMMS2 的客户端
  4. SQL Server查询结果中添加自动编号
  5. js :check 檢查
  6. ajax登录验证的原理,ajax用户登录验证-get和post提交方式,与工作原理—2018-8-15...
  7. 罗技键盘linux,logiops,在 Linux下设置罗技鼠标的按键和手势
  8. jsx怎么往js里传参数_在vue中使用jsx语法的使用方法
  9. 一维有限元法matlab,有限元matlab研究.ppt
  10. 可变悬挂调节软硬_【5040地推合作品牌】荷兰高性能减震器及悬挂系统品牌—KONI...