MyBatis的一对一
1. 建立好工程后,在pom.xml中配置myBatis的依赖。
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.whl</groupId> <artifactId>MyBatis02</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>MyBatis02 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> </dependencies> <build> <finalName>MyBatis02</finalName> </build> </project>
主要就是加一个MyBatis的依赖,以及修改一下Junit的版本,以适应自己的环境。
2 新建一个Card类和一个Person类,每一个Person只有一个Card。故在Person类中
会有一个Card类的属性,在建立数据库时,cid作为外键,存在于Person表中。代码如下:
数据库在dos框中自己建立(cid int unique , foreign key (cid) references card (cid))(外键设置)
Card类代码如下
package com.whl.one2one; public class Card { private int cid; private String cnumber; public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getCnumber() { return cnumber; } public void setCnumber(String cnumber) { this.cnumber = cnumber; } public Card(int cid, String cnumber) { super(); this.cid = cid; this.cnumber = cnumber; } public Card() { super(); } @Override public String toString() { return "Card [cid=" + cid + ", cnumber=" + cnumber + "]"; } }
Person类代码如下:
package com.whl.one2one; public class Person { private int pid; private String pname; private String page; private String psex; private Card card; public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } public String getPsex() { return psex; } public void setPsex(String psex) { this.psex = psex; } public Card getCard() { return card; } public void setCard(Card card) { this.card = card; } @Override public String toString() { return "Person [pid=" + pid + ", pname=" + pname + ", page=" + page + ", psex=" + psex + ", card=" + card + "]"; } public Person(int pid, String pname, String page, String psex, Card card) { super(); this.pid = pid; this.pname = pname; this.page = page; this.psex = psex; this.card = card; } public Person() { super(); } }
3.在建立好上述的Jopo类后,在同样的包下新建CardMapper.xml和PersonMapper.xml来映射SQL语句。
CardMapper.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.whl.one2one"> <select id="selectCardByID" parameterType="int" resultType="com.whl.one2one.Card"> select * from card where cid = #{id} </select> </mapper>
PersonMapper.xml代码如下:在Person类中,存在主外键关系,所以select中不在是单一表的查询,将原本的resultType改为
resultMap,其中的值,就是后续Resultmap的ID。且在resultMap中会存在一个对CardMapper.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.whl.one2one"> <select id="selectPersonById" parameterType="int" resultMap="resultPersonMap"> select * from tb_person where pid=#{id} </select> <resultMap id="resultPersonMap" type="com.whl.one2one.Person"> <id property="pid" column="pid" /> <result property="pname" column="name" /> <result property="psex" column="psex" /> <result property="page" column="page" /> <association property="card" column="cid" select="com.whl.one2one.selectCardByID" javaType="com.whl.one2one.Card"/> </resultMap > </mapper>
4. 在myBatis-config.xml 中实现与数据库的连接。当然在连接之前需要数据库的Jar包,网速好时可在pom中添加依赖自动下载
网速不好使要手动加载。在代码中可以看到连接数据库的driver、url、user、pass等都是通过<properties resource="jdbc.properties"/>来加载资源从而获得的。jdbc.properties文件后面会展示。同时在此XML文件中还需要完成对CardMapper.xml以及PersonMapper.xml的映射。如<Mapper>节点所示。
<?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> <!--加载资源 --> <properties resource="jdbc.properties"/> <environments default="mybatis"> <environment id="mybatis"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.pass}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/whl/one2one/CardMapper.xml"></mapper> <mapper resource="com/whl/one2one/PersonMapper.xml"></mapper> </mappers> </configuration>
5 测试:测试代码如下所示:利用Junit的before和After来实现前期session的准备和后期session的关闭。
package com.whl.testone2one; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; 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; import com.whl.one2one.Card; import com.whl.one2one.Person; public class TestOne2One { private SqlSession s; @Before public void beforeTest(){ String res = "mybatis-config.xml"; try { InputStream is = Resources.getResourceAsStream(res); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is); s = ssf.openSession(); } catch (IOException e) { e.printStackTrace(); } } @Test public void testPersonById(){ Person p = s.selectOne("com.whl.one2one.selectPersonById",1); System.out.println(p); } @Test public void testCardById(){ Card c = s.selectOne("com.whl.one2one.selectCardByID",1); System.out.println(c); } @After public void afterTest(){ s.commit(); if(s != null){ s.close(); s = null; } } }
posted on 2017-07-11 17:32 注定要成为攻城狮的男人 阅读(190) 评论(0) 编辑 收藏 举报