主要是自己总是记不住,写下来以备复习
以博客表逻辑为例
create table blog(
blog_id int,
blog_title varchar(30),
blog_author_id int
);
create table post(
post_id int,
post_subject varchar(30),
post_body varchar(30),
blog_id int
);
create table author(
author_id int,
author_username varchar(30),
author_password varchar(30)
);
insert into blog values(1, 'title1', '1');
insert into blog values(2, 'title2', '1');
insert into blog values(3, 'title3', '1');
insert into post values(1, 'subject1', 'body1', '1');
insert into post values(2, 'subject1', 'body1', '1');
insert into post values(3, 'subject1', 'body1', '1');
insert into post values(4, 'subject1', 'body1', '2');
insert into post values(5, 'subject1', 'body1', '2');
insert into post values(6, 'subject1', 'body1', '3');
insert into author values(1, 'bili', 'bili');
insert into author values(2, 'xiaozhi', 'xiaozhi');
insert into author values(3, 'xiaofeng', 'xiaofeng');
1.新建项目(不详细说)
2.下载mybatis
mybatis下载链接
进去后如图
下载前两个(可能版本会有不同,大家下载最新版本即可)
3.导入jar包:项目里先在WEB-INF下新建lib文件夹,将如下jar包放入lib下
找Mybatis下载目录
还有一个mysql-connector-java.jar
导入依赖
src下建立如下4个包
4.导入配置文件:config下放configuration.xml配置文件,文件位置如图
5.src下新建两个文件
名字不要改变
log4j.properties文件下粘贴以下日志
log4j.rootLogger=DEBUG,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.org.apache=INFO
property.properties为jdbc配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jdbc_20210201
jdbc.username=root
jdbc.password=123456
6.主配置文件:configuration.xml配置
第一个属性是properties,resource为上面的property.properties路径
<properties resource="property.properties"></properties>
第二个属性为settings,常用属性如下
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
意思是数据库字段名下划线自动转驼峰,默认为false
下一个属性为typeAliases(类型别名),类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写
<typeAliases>
<package name="bean"/>
</typeAliases>
这个代表bean包下面的类被xml文件引用时均可用小写类名代替
第四个为environment属性,
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value=""/>
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
property属性值为property.properties里面的值
最后一个为mapper属性,注册映射器,具体在后面
<mappers>
</mappers>
以上是比较常用的属性,还有其他属性参照以下网址
Mybatis 3配置里的xml配置文件部分
7.util包:在util下新建DBUtil类,代码如下
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class DBUtil
{
public static SqlSession getSqlSession() throws IOException
{
InputStream inputStream = Resources.getResourceAsStream("config/Configuration.xml");//括号里为Configuration.xml的所在路径
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory.openSession(true);//设置自动提交
}
}
8.在bean下建立实体类
博客类
import java.util.List;
public class Blog
{
private int blogId;
private String blogTitle;
private Author author;
private List<Post> posts;
public Blog()
{
}
public Blog(int blogId, String blogTitle, Author author, List<Post> posts)
{
this.blogId = blogId;
this.blogTitle = blogTitle;
this.author = author;
this.posts = posts;
}
public int getBlogId()
{
return blogId;
}
public void setBlogId(int blogId)
{
this.blogId = blogId;
}
public String getBlogTitle()
{
return blogTitle;
}
public void setBlogTitle(String blogTitle)
{
this.blogTitle = blogTitle;
}
public Author getAuthor()
{
return author;
}
public void setAuthor(Author author)
{
this.author = author;
}
public List<Post> getPosts()
{
return posts;
}
public void setPosts(List<Post> posts)
{
this.posts = posts;
}
@Override
public String toString()
{
return "Blog{" +
"blogId=" + blogId +
", blogTitle='" + blogTitle + '\'' +
", author=" + author +
", posts=" + posts +
'}';
}
}
博文类
public class Post
{
private int postId;
private String postSubject;
private String postBody;
public Post(int postId, String postSubject, String postBody)
{
this.postId = postId;
this.postSubject = postSubject;
this.postBody = postBody;
}
public Post()
{
}
public int getPostId()
{
return postId;
}
public void setPostId(int postId)
{
this.postId = postId;
}
public String getPostSubject()
{
return postSubject;
}
public void setPostSubject(String postSubject)
{
this.postSubject = postSubject;
}
public String getPostBody()
{
return postBody;
}
public void setPostBody(String postBody)
{
this.postBody = postBody;
}
@Override
public String toString()
{
return "Post{" +
"postId=" + postId +
", postSubject='" + postSubject + '\'' +
", postBody='" + postBody + '\'' +
'}';
}
}
作者类
public class Author
{
private int authorId;
private String authorUsername;
private String authorPassword;
public Author(int authorId, String authorUsername, String authorPassword)
{
this.authorId = authorId;
this.authorUsername = authorUsername;
this.authorPassword = authorPassword;
}
public Author()
{
}
public int getAuthorId()
{
return authorId;
}
public void setAuthorId(int authorId)
{
this.authorId = authorId;
}
public String getAuthorUsername()
{
return authorUsername;
}
public void setAuthorUsername(String authorUsername)
{
this.authorUsername = authorUsername;
}
public String getAuthorPassword()
{
return authorPassword;
}
public void setAuthorPassword(String authorPassword)
{
this.authorPassword = authorPassword;
}
@Override
public String toString()
{
return "Author{" +
"authorId=" + authorId +
", authorUsername='" + authorUsername + '\'' +
", authorPassword='" + authorPassword + '\'' +
'}';
}
}
9.建立mybatis模板
点击Files下面的加号
如图,name为mybatis,extension为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}" >
</mapper>
点击确定即可
10.建立接口文件并和映射器文件相关联
在mapper下新建一个Interface,名字为BlogMapper
在mapper下新建mybatis文件(我这里模板名字为mybatis1,大家新建mybatis即可)
回到主配置文件,将包内的映射器接口实现全部注册为映射器
<mappers>
<package name="mapper"/>
</mappers>
在BlogMapper.xml粘贴以下代码
<!--关联的嵌套查询方法一 -->
<resultMap id="blogResult" type="blog">
<id property="blogId" column="blog_id"></id>
<result property="blogTitle" column="blog_title"></result>
<association property="author" javaType="author">
<id property="authorId" column="author_id"></id>
<result property="authorUsername" column="author_username"></result>
<result property="authorPassword" column="author_password"></result>
</association>
</resultMap>
<!--关联的嵌套查询结果二 -->
<resultMap id="blogResult1" type="blog">
<id property="blogId" column="blog_id"></id>
<result property="blogTitle" column="blog_title"></result>
<association property="author" javaType="author" resultMap="authorResult1"></association>
</resultMap>
<resultMap id="authorResult1" type="author">
<id property="authorId" column="author_id"></id>
<result property="authorUsername" column="author_username"></result>
<result property="authorPassword" column="author_password"></result>
</resultMap>
<select id="selectBlog" resultMap="blogResult1">
select blog_id,blog_title,blog_author_id,author_id,author_username,author_password
from blog,author
where blog_author_id=author_id;
</select>
BlogMapper.java
import bean.Blog;
import org.apache.ibatis.session.SqlSession;
import util.DBUtil;
import java.io.IOException;
import java.util.List;
public class BlogMapperImpl
{
public static void main(String[] args)
{
try(SqlSession sqlSession = DBUtil.getSqlSession())
{
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
List<Blog> blogs = blogMapper.selectBlog();
System.out.println(blogs);
} catch (IOException e)
{
e.printStackTrace();
}
}
}
UserMapper.java
import bean.User;
import org.apache.ibatis.session.SqlSession;
import util.DBUtil;
import java.io.IOException;
import java.util.List;
public class UserMapperTest
{
public static void main(String[] args)
{
try(SqlSession sqlSession = DBUtil.getSqlSession())
{
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.selectAll();
System.out.println(users);
sqlSession.commit();
} catch (IOException e)
{
e.printStackTrace();
}
}
}