mybatis一对一关联
来源:互联网 发布:淘宝老客户怎么维护 编辑:程序博客网 时间:2024/05/01 18:57
一.导入资源
<dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.3.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.20</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>二.创建数据库表
CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET latin1 */;USE `mybatis`;create table t_wife( id int primary key auto_increment, wife_name varchar(20), fk_husband_id int);create table t_husband(id int primary key auto_increment,husband_name varchar(20));insert into t_husband values (null,'hello');insert into t_wife values(null,'kitty',1)三.创建基本对象
public class Wife implements Serializable{ private static final long serialVersionUID = 1L; private Integer id; private String name; private Husband husband; public Wife() { super(); } public Wife(Integer id, String name, Husband husband) { super(); this.id = id; this.name = name; this.husband = husband; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Husband getHusband() { return husband; }public void setHusband(Husband husband) { this.husband = husband; } @Override public String toString() { return "Wife [id=" + id + ", name=" + name + ", husband=" + husband + "]"; }}public class Husband implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String name;private Wife wife;public Husband() {super();}public Husband(int id, String name, Wife wife) {super();this.id = id;this.name = name;this.wife = wife;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Wife getWife() {return wife;}public void setWife(Wife wife) {this.wife = wife;}@Overridepublic String toString() {return "Husband [id=" + id + ", name=" + name + ", wife=" + wife + "]";}}四.创建接口以及映射文件
public interface HusbandDao {public Husband selectHusbandById(int id) throws Exception;public Husband selectHusbandAndWife(int id) throws Exception;}public interface WifeDao {public Husband selectWifeByHusbandId(int id) throws Exception;}<mapper namespace="com.dao.HusbandDao"><resultMap type="Husband" id="husbandAndWife"><id property="id" column="id" javaType="java.lang.Integer" /><result property="name" column="husband_name" javaType="java.lang.String" /><association property="wife" column="id" javaType="Wife"select="com.dao.WifeDao.selectWifeByHusbandId"></association></resultMap><select id="selectHusbandById" resultType="Husband">select * from t_husband where id=#{id}</select><select id="selectHusbandAndWife" resultMap="husbandAndWife">select * from t_husband where id=#{id}</select></mapper><mapper namespace="com.dao.WifeDao"><select id="selectWifeByHusbandId" resultType="Wife">selectid,wife_name as name fromt_wife where fk_husband_id = #{id}</select></mapper>五.创建配置文件mybatis-config.xml以及数据库配置文件config.properties
<configuration><properties resource="config.properties"></properties><typeAliases><typeAlias alias="Wife" type="com.domain.Wife" /><typeAlias alias="Husband" type="com.domain.Husband" /></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /></dataSource></environment></environments><mappers><mapper resource="mapper/WifeMapper.xml" /><mapper resource="mapper/HusbandMapper.xml" /></mappers></configuration>driver=com.mysql.jdbc.Driverurl=jdbc:mysql:///mybatisusername=rootpassword=root六.编写单元测试
public class HusbandDaoTest {SqlSession session = null;HusbandDao husbandDao = null;@Beforepublic void setUp() throws Exception {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);session = sqlSessionFactory.openSession();husbandDao = (HusbandDao) session.getMapper(HusbandDao.class);}@Afterpublic void tearDown() throws Exception {session.commit();session.close();}@Testpublic void testSelectHusbandAndWife() throws Exception {Husband husband = husbandDao.selectHusbandAndWife(1);System.out.println(husband);}}
源码链接:https://github.com/wangjianyangchn/myBatis/tree/master/AssociationOnetoOne
参考文档:http://www.mybatis.org/mybatis-3/zh/index.html
0 0
- mybatis---一对一双向关联
- mybatis-(一对一关联)
- MyBatis关联查询(一对一)
- mybatis一对一关联查询
- Mybatis一对一关联查询
- Mybatis一对一关联查询
- mybatis一对一关联
- mybatis一对一关联关系
- Mybatis一对一关联查询
- Mybatis学习笔记-一对一关联
- Mybatis---一对一关联表查询
- Mybatis入门--关联查询一对一
- MyBatis中一对一关联查询
- MyBatis之一对一关联查询
- MyBatis 关联映射之一对一
- MyBatis一对一关联表查询
- mybatis一对一表关联问题
- Mybatis之一对一关联关系
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- 浅谈Json在java中的使用
- Java多态总结
- 单例设计模式
- mssql2012以后新增的offset分页,看起来爽死了!!!
- mybatis一对一关联
- Java多态总结
- 51nod 1433 0和5
- Linux(centOS)下中文输入的问题
- 关于eclipse中server中的Tomcat的环境配置问题
- C++高质量编程指南笔记(二)
- 【Mysql】积累四
- ISTA算法求解L1正则化问题
- HorizontalListView横着的Listview