Mybatis一对一的关系
前言:本项目是使用idea进行开发,数据库使用的是Mysql。
1.搭建maven项目,引入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_02</groupId>
<artifactId>Mybatis_02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Mybatis_02 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.12</version>
<scope>test</scope>
</dependency>
<!--引入Mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!--引入MySql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
</dependencies>
<build>
<!--设置java目录下的com.zs.entity的映射文件作为资源文件编译-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<finalName>Mybatis_02</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.0.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.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</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>
2.项目模块图:
3.在resources目录下新建mybatis的核心配置文件mybatis-config.xml
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>
<!--配置Author.xml里面的返回类型 -->
<typeAliases>
<package name="com.zs.entity"/>
</typeAliases>
<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/zs?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="1234" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zs/entity/Person.xml"></mapper>
</mappers>
</configuration>
4.建立与数据库对应的实体包,包下建立类与对应的xml文件(这里没有使用接口调用方法,是使用xml文件调用方法)
Card.java
package com.zs.entity;
/**
* @author 小思
* @PackageName:com.zs.entity
* @ClassName: Card
* @Description:
* @date 2018/10/28 18:45
*/
public class Card {
private Integer cid;//身份证的Id
private String cnumber;//身份证的号码
private Person Person;//
public Card(Integer cid, String cnumber, com.zs.entity.Person person) {
this.cid = cid;
this.cnumber = cnumber;
Person = person;
}
public Card(Integer cid, String cnumber) {
this.cid = cid;
this.cnumber = cnumber;
}
public Card() {
}
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 com.zs.entity.Person getPerson() {
return Person;
}
public void setPerson(com.zs.entity.Person person) {
Person = person;
}
}
Person.java
package com.zs.entity;
/**
* @author 小思
* @PackageName:com.zs.entity
* @ClassName: Person
* @Description:Person的实体类
* @date 2018/10/28 18:28
*/
public class Person {
private Integer pid;//人的id
private String pname;//人的姓名
private Card card;//人的身份证
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public Person(Integer pid, String pname, Card card) {
this.pid = pid;
this.pname = pname;
this.card = card;
}
public Person(Integer pid, String pname) {
this.pid = pid;
this.pname = pname;
}
public Person() {
}
}
Card.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="card">
<!--通过卡的信息找人的信息和人的信息和Person.xml和TesrOneToOne里面的testPerson()测试方法雷同-->
</mapper>
Person.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="person">
<!--查询所有人的信息(不包括卡)-->
<select id="getAllPerson" resultType="Person">
select * from person
</select>
<!--查询单个人和其拥有的卡-->
<select id="getPersonByPid" resultMap="person_card" parameterType="int" >
select * from person p,card c where p.cid=c.cid and p.pid=#{pid}
</select>
<!--自定义返回结果集-->
<!--id是这个结果集的唯一标识符,方便根据Id调用,type是主体返回的实体类-->
<resultMap id="person_card" type="Person" >
<!--主键-->
<id column="pid" property="pid"></id>
<!--普通属性-->
<result column="pname" property="pname"></result>
<!--一对一的映射:人对应他的卡 javaType代表对应的类的类型,column代表是通过哪一列段进行关联-->
<association property="card" javaType="Card" column="cid" >
<id property="cid" column="cid"></id>
<result property="cnumber" column="cnumber"></result>
</association>
</resultMap>
<!--添加单个的人 增加几个字段,就写上几个字段,如果要添加卡的信息,获取卡的信息即可-->
<insert id="addPerson" parameterType="Person">
insert into person(pname) values(#{pname})
</insert>
<!--修改人的信息(不包括卡的信息)-->
<update id="updatePerson" parameterType="Person">
update person set pname=#{pname} where pid=#{pid}
</update>
<!--根据人的id修改人的信息和卡的信息 Map可以传入多个参数-->
<update id="updatePerson_card" parameterType="Map">
update person set pname=#{pname},cid=#{cid} where pid=#{pid}
</update>
<!--删除人的信息(不包括卡的信息)-->
<delete id="deletePerson" parameterType="int">
delete from person where pid=#{pid}
</delete>
<!--通过人的pid删除人的信息与卡的信息-->
<delete id="deleteCard" parameterType="int">
delete from card where pid=#{pid}
</delete>
</mapper>
5.在test文件夹下新建com.zs.test文件夹,并且新建单元测试类TestOneToOne.java
TestOneToOne.java
package com.zs.test;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author 小思
* @PackageName:com.zs.test
* @ClassName: TestOneToOne
* @Description:测试一对一关系是否映射成功
* @date 2018/10/28 19:02
*/
public class TestOneToOne {
private SqlSessionFactory sessionFactory;
private SqlSession session;
@Before
public void before() {
sessionFactory = new SqlSessionFactoryBuilder().build(getClass().getClassLoader().getResourceAsStream("mybatis-config.xml"));
session = sessionFactory.openSession();
}
@Test
public void testPerson() {
// //通过配置文件Person.xml获取方法
//
// //查询所有人的信息(不包括卡)
// List<Person> ls=session.selectList("person.getAllPerson");
// for (Person l : ls) {
// System.out.println(l.getPname());
// }
//
// //查询单个人和其拥有的卡
// Person p = session.selectOne("person.getPersonByPid", 1);
// System.out.println(p.getPname() + "___________" + p.getCard().getCnumber());
// //添加单个的人
// Person per=new Person();
// per.setPname("小小");
// int a=session.insert("person.addPerson",per);
// System.out.println(a);
// Person pp=new Person();
// pp.setPid(1);
// pp.setPname("大大");
// Card c=new Card();
// c.setCid(1);
// pp.setCard(c);
// pp.getCard().setCid(2);
// //修改人的信息(不包括卡的信息)
// Person s=new Person();
// s.setPid(1);
// s.setPname("类类");
// int c=session.update("person.updatePerson",s);
// System.out.println(c);
// //根据人的id修改人的信息和卡的信息
// Map<String,String> map=new HashMap<>();
// map.put("pname","大大");
// map.put("cid","1");
// map.put("pid","1");
// int b=session.update("person.updatePerson_card",map);
// System.out.println(b);
// //删除人的信息(不包括卡的信息)
// int d=session.delete("person.deletePerson",1);
// System.out.println(d);
// //通过人的pid删除人的信息与卡的信息(先删除人后删除卡)
// int e=session.delete("person.deletePerson",3);
// int f=session.delete("person.deleteCard",3);
// System.out.println(e+"___"+f);
}
@Test
public void testCard(){
// 通过卡的信息找人的信息和人的信息和Person.xml和TesrOneToOne里面的testPerson()测试方法雷同
}
@After
public void after() {
session.commit();
session.close();
}
}
6.测试时,取消注释,运行测试类即可。
如果想节省繁琐的搭建过程,完整项目已上传,能直接运行,请前往https://download.****.net/download/zeal9s/10755856下载
说在最后的话:编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~