概述
数据库
目录结构
映入jar包和junit单元测试
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 com.geyao.mybatis.pojo.Blog;
public interface BlogMapper {
Blog selectBlogById(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>
</mapper>
com.geyao.mybatis.pojo
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;
}
@Override
public 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;
}
@Override
public String toString() {
return "Blog [id=" + id + ", title=" + title + ", author=" + author + ", state=" + state + ", featured="
+ featured + ", style=" + style + "]";
}
}
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 block
e.printStackTrace();
}
}
private MyBatisUtil() {}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
com.geyao.mybatis.mapper测试类
BlogMapperTest
package com.geyao.mybatis.mapper;
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.util.MyBatisUtil;
public class BlogMapperTest {
@Test
public void testSelectBlog() {
SqlSession session =MyBatisUtil.getSqlSession();
BlogMapper blogMapper =session.getMapper(BlogMapper.class);
Blog blog = blogMapper.selectBlogById(1);
session.close();
System.out.println(blog);
}
}
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"/>
</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"/>
</mappers>
</configuration>
log4j.properties
### u914Du7F6Eu6839 ###
log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE
### u8BBEu7F6Eu8F93u51FAsqlu7684u7EA7u522BuFF0Cu5176u4E2Dloggeru540Eu9762u7684u5185u5BB9u5168u90E8u4E3Ajaru5305u4E2Du6240u5305u542Bu7684u5305u540D ###
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
### u914Du7F6Eu8F93u51FAu5230u63A7u5236u53F0 ###
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学习(47):嵌套查询--一对一的全部内容,希望文章能够帮你解决mybatis学习(47):嵌套查询--一对一所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复