mybatis配置关联映射
来源:互联网 发布:托马斯·哈里斯 知乎 编辑:程序博客网 时间:2024/06/05 18:13
在做一个Demo的时候,配置mybatis的关联映射,想分享一下。
数据库如下:
CREATE TABLE TB_CLAZZ(id INT PRIMARY KEY AUTO_INCREMENT,CODE VARCHAR (18));INSERT INTO TB_CLAZZ(CODE) VALUES('j1601');INSERT INTO TB_CLAZZ(CODE) VALUES('j1602');CREATE TABLE TB_STUDENT(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(18),sex CHAR(3),age INT,clazz_id INT,FOREIGN KEY(clazz_id) REFERENCES TB_CLAZZ(id));INSERT INTO TB_STUDENT(NAME,sex,age,clazz_id) VALUES('jack','男',22,1);INSERT INTO TB_STUDENT(NAME,sex,age,clazz_id) VALUES('rose','女',18,1);INSERT INTO TB_STUDENT(NAME,sex,age,clazz_id) VALUES('tom','男',25,1);INSERT INTO TB_STUDENT(NAME,sex,age,clazz_id) VALUES('mary','女',20,1);
就是这么个东西,先导包。如图:
然后配置mybatis的核心配置文件,sqlMapConfig.xml.
sqlMapConfig.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> <properties resource="jdbc.properties"/><typeAliases><!-- <typeAlias type="com.liujie.pojo.User" alias="User"/>--><package name="com.liujie.pojo"/></typeAliases> <!-- 对事务的管理和连接池的配置 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis2?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="123" /> </dataSource> </environment> </environments> <!-- mapping 文件路径配置 --> <mappers> <mapper resource="com/liujie/mapper/UserMapper.xml" /> </mappers> </configuration>
就这个文件有个小地方坑了我半个小时,因为始终报错could not find source ....../UserMapper.xml。是因为配置的路径是全名,把工程名还有src都带进去了,这里的时候可以按住control看能不能过去,不能就肯定路径有问题,在这里默认是classpath路径也就是src路径的。ps:我用的是eclipse,如果用的是IDEA,那么要注意,IDEA不会编译src目录的java的xml文件,所以要将IDEA maven项目中src源代码下的xml等资源文件编译进classes文件夹。
然后写pojo,写工具类。
SessionUtils.java:
package com.liujie.junit;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;public class SessionUtils {private static SqlSessionFactory sqlSessionFactory=null;static{try {InputStream in =Resources.getResourceAsStream("sqlMapConfig.xml");sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);} catch (IOException e) {e.printStackTrace();}}public static SqlSession getSqlSession(){return sqlSessionFactory.openSession();}public static SqlSessionFactory getSqlSessionFactory(){return sqlSessionFactory;}}Clazz.java:
package com.liujie.pojo;public class Clazz {private Integer id;private String code;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}}
Student.java:
package com.liujie.pojo;public class Student {private Integer id;private String name;private String sex;private String age;private Clazz clazz;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 String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public Clazz getClazz() {return clazz;}public void setClazz(Clazz clazz) {this.clazz = clazz;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", clazz=" + clazz + "]";}}然后写UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="test"><resultMap type="com.liujie.pojo.Student" id="studentResultMap"><id property="name" column="name"/><result property="sex" column="sex"/><result property="age" column="age"/><association property="clazz" column="clazz_id" javaType="com.liujie.pojo.Clazz" select="selectClazzWithId"/></resultMap><select id="selectClazzWithId" resultType="Clazz">select * from TB_CLAZZ where id=#{id}</select><select id="selectStudent" resultMap="studentResultMap">select * from TB_STUDENT</select></mapper>
这里的association就是关联映射,还有一个是collection。里面的select属性表示执行一条查询语句,将查询到的数据封装property所代表的类型对象当中。上面的selectClazzWithId就是执行一台哦SQL语句,将学生的clazz_id作为参数查询对应的班级信息。
阅读全文
0 0
- mybatis配置关联映射
- MyBatis之关联映射
- MyBatis关联映射
- MyBatis关联映射
- mybatis关联查询映射
- Mybatis 关联映射
- MyBatis关系关联映射
- mybatis关联映射
- Mybatis之关联映射
- Mybatis关联映射
- Mybatis多表关联映射
- MyBatis 关联映射之一对一
- Mybatis关联映射(关联单个对象)
- MyBatis关联关系配置
- Hibernate关联映射及配置
- hibernate 关联关系映射配置
- MyBatis学习_2_关联映射_一对一
- Mybatis系列(七)关联映射
- Duan2baka的线段树模板!
- 第三周总结
- Robot Framework自动化测试框架(二)————待续
- 计算机组成与体系结构-第一章导论
- 星际争霸2人工智能初探——SC2LE研究环境搭建
- mybatis配置关联映射
- C# 代码实现 顺序队列和链队列
- VMware的安装过程—实验一
- 2017 JUST Programming Contest 2.0【solved:11 / 11】
- Rust: codewars的prize draw算法
- Java Persistence with MyBatis 3(中文版) 第五章 与Spring集成
- 残
- JEECG环境搭建
- jQuery中queue和dequeue的用法