前言:本项目是使用idea进行开发,数据库使用的是Mysql。
其中,一对一的关系是member--------card
1.搭建一个maven项目
2.引入mybatis需要的依赖
习惯性建立java、resources、test等文件夹,建立文件夹的时候注意标记文件夹的用途(不然编译器无法识别,无法编译代码)
pom.xml(注意要将实体包下面的xml文件设置可编译)
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>MyBatis_Joy_03</groupId>
<artifactId>MyBatis_Joy_03</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>MyBatis_Joy_03 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!-- ********************** 日志配置 ********************** -->
<!--记得修改mybatis.cfg.xml添加如下内容-->
<!--<setting name="logImpl" value="LOG4J2"/>-->
<!--核心log4j2jar包-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.9.1</version>
</dependency>
<!--web工程需要包含log4j-web,非web工程不需要
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.9.1</version>
</dependency>-->
</dependencies>
<build>
<finalName>MyBatis_Joy_03</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3.项目模块图
3.在resources目录下新建mybatis的核心配置文件mybatis.xml
mybatis.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="logImpl" value="LOG4J2"/>
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</dataSource>
</environment>
</environments>
<!--配置实体映射关系-->
<mappers>
<mapper class="com.joy.mapper.MemberMapper"></mapper>
<mapper class="com.joy.mapper.CardMapper"></mapper>
</mappers>
</configuration>
4.建立与数据库对应的实体包
Member.java
package com.joy.model;
public class Member {
private Integer mid;
private String mname;
private String mpwd;
private Card card;
public Member() {
}
public Member(Integer mid, String mname, String mpwd, Card card) {
this.mid = mid;
this.mname = mname;
this.mpwd = mpwd;
this.card = card;
}
public Integer getMid() {
return mid;
}
public void setMid(Integer mid) {
this.mid = mid;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getMpwd() {
return mpwd;
}
public void setMpwd(String mpwd) {
this.mpwd = mpwd;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
@Override
public String toString() {
return "Member{" +
"mid=" + mid +
", mname='" + mname + '\'' +
", mpwd='" + mpwd + '\'' +
", card=" + card +
'}';
}
}
Card.java
package com.joy.model;
public class Card {
private Integer cid;
private String cnumber;
private Member member;
public Card() {
}
public Card(Integer cid, String cnumber, Member member) {
this.cid = cid;
this.cnumber = cnumber;
this.member = member;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCnumber() {
return cnumber;
}
public void setCnumber(String cnumber) {
this.cnumber = cnumber;
}
public Member getMember() {
return member;
}
public void setMember(Member member) {
this.member = member;
}
@Override
public String toString() {
return "Card{" +
"cid=" + cid +
", cnumber='" + cnumber + '\'' +
'}';
}
}
5.建立Mapper包,在包里建的接口
MemberMapper.java
package com.joy.mapper;
import com.joy.model.Member;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface MemberMapper {
//查询全部用户
@Select("select * from member")
public List<Member> findAllMember();
//根据id查询Member
@Select("select * from member where mid=#{mid}")
public Member findMemberByMid(int mid);
//一对一查询(通过mid查询其所对应的卡)
@Select("select * from member where mid=#{mid}")
@Results({
@Result(property = "mid",column = "mid",id = true),
@Result(property = "card",column = "mid",one = @One(select="com.joy.mapper.CardMapper.findCardById"))
})
public Member findMemberAndCardByMid(int mid);
}
CardMapper.java
package com.joy.mapper;
import com.joy.model.Card;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
public interface CardMapper {
//通过mid查询卡
@Select("select * from card where mid=#{mid}")
public Card findCardById(int mid);
//通过cid查询卡及用户
@Select("select * from card where cid=#{cid}")
@Results({
@Result(property = "cid",column = "cid",id = true),
@Result(property = "member",column = "cid",one = @One(select = "com.joy.mapper.MemberMapper.findMemberByMid"))
})
public Card findCardAndMemberByCid(int cid);
}
6.在test文件夹下新建com.joy.test文件夹,并且新建单元测试类TestMember.java
package com.joy.test;
import com.joy.mapper.CardMapper;
import com.joy.mapper.MemberMapper;
import com.joy.model.Card;
import com.joy.model.Member;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
public class TestMember {
private SqlSession sqlSession;
@Before
public void before(){
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(this.getClass().getClassLoader().getResourceAsStream("mybatis.xml"));
sqlSession = sqlSessionFactory.openSession();
}
@Test
public void test(){
MemberMapper mapper=sqlSession.getMapper(MemberMapper.class);
//查询全部
// List<Member> list= mapper.findAllMember();
// for (Member m : list) {
// System.out.println(m);
// }
//一对一查询(通过mid查询其所对应的卡)
// Member member= mapper.findMemberAndCardByMid(1);
// System.out.println(member);
//通过cid查询卡及用户
CardMapper cardMapper=sqlSession.getMapper(CardMapper.class);
Card card=cardMapper.findCardAndMemberByCid(1);
System.out.println(card);
}
}
7.取消注释,运行测试类即可
最后的最后:写文章不容易啊,亲若是喜欢或者对你有帮助记得点赞、+关注或者收藏呦~就酱,啾咪~