mybatis多表查询,自动生成id
主要是在配置文件中,配置好所要包含的字段。
类关系:account----role,1对1
account包含role类
java类:
public class Account{
private Integer accountId;
private String username;
private String password;
private Role role;
}
public class Role {
private Integer id;
private Integer leave;
private String name;
}
数据库表:
--
-- 表的结构 `account`
-- CREATE TABLE IF NOT EXISTS `account` (
`accountId` int(32) NOT NULL AUTO_INCREMENT,
`userName` varchar(32) COLLATE utf8_bin NOT NULL,
`password` varchar(32) COLLATE utf8_bin NOT NULL,
`roleId` int(32) NOT NULL,PRIMARY KEY (`accountId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1005 ;
--
-- 表的结构 `role`
-- CREATE TABLE IF NOT EXISTS `role` (
`id` int(21) NOT NULL AUTO_INCREMENT,
`r_leave` int(11) NOT NULL,
`name` varchar(32) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=12 ;
因为我是分开的,所有有两个配置文件。可以写在一个配置文件中
account-resultmap.xml设置,在配置文件中,也包含了role类的所有对应:
<mapper namespace="accountMap">
<!-- id 用于*-mapper.xml 中进行自动填值 ,-->
<resultMap type="com.hoo.entity.Account" id="accountResultMap">
<id property="accountId" column="accountId"/>
<result property="username" column="userName"/>
<result property="password" column="password"/>
<association property="role" javaType="com.hoo.entity.Role">
<id property="id" column="id"/>
<result property="leave" column="r_leave"/>
<result property="name" column="name"/>
</association>
</resultMap>
</mapper>
mapper.xml设置,查的时候,直接查两表,mybatis自动对应数据:
<select id="getAllAccount" resultType="list" resultMap="accountMap.accountResultMap">
<![CDATA[
select * from account a,role r where a.roleId=r.id
]]>
</select> <!-- accountResultMap是account-resultmap.xml中定义的resultmap -->
<select id="getAccountById" parameterType="string" resultType="com.hoo.entity.Account" resultMap="accountMap.accountResultMap">
<![CDATA[
select * from account a,role r where a.accountId = #{id} and a.roleId=r.id
]]>
</select>
<!-- 自动生成id策略 -->
<insert id="addAccount" useGeneratedKeys="true" keyProperty="accountId" parameterType="account">
insert into account(accountId, status, username, password,phone,email,roleId)
values(#{accountId}, #{status}, #{username}, #{password},#{phone},#{email},#{role.id})
</insert>
mybatis多表查询,自动生成id的更多相关文章
- Mybatis最入门---代码自动生成(generatorConfig.xml配置)
[一步是咫尺,一步即天涯] 经过前文的叙述,各位看官是不是已经被Mybatis的强大功能给折服了呢?本文我们将介绍一个能够极大提升我们开发效率的插件:即代码自动生成.这里的代码自动生成包括,与数据库一 ...
- Mybatis多表查询出现null字段
写在前面 今天使用mybatis实现多表查询,记录一下其中遇到的坑 mybatis多表查询简介 mybatis多表查询主要有两个方式,通俗易懂的来说就是一个是查询少量属性(association),一 ...
- Solr自动生成ID
在Solr中,每一个索引,都要有一个唯一的ID,类似于关系型数据库表中的主键.为了方便创建索引,需要配置自动生成的ID,即UUID. 一.配置schema.xml文件 添加uuid字段类型,修改字段i ...
- Solr4.0 如何配置使用UUID自动生成id值
原文链接http://blog.csdn.net/keepthinking_/article/details/8501058#comments 最近学习了Lucene,随便也学习了Solr,Solr规 ...
- ASP.NET 4.0 ListView等容器控件中获取ClientID值与HTML中自动生成ID字符串不一样问题。
ASP.NET 4.0 中 ClientIDMode的属性 可以设置获取不同ID格式的值. 项目中遇到的问题: 1.ListView1 ItemDataBound事件中,获取ClientID结果与自动 ...
- Elasticsearch 索引文档如何使用自动生成 Id?
一个文档的 _index . _type 和 _id 唯一标识一个文档. 我们可以提供自定义的 _id 值,或者让 index API 自动生成. 如果你的数据没有自然的 ID, Elasticsea ...
- Mybatis总结之如何自动生成数据库表结构
一般情况下,用Mybatis的时候是先设计表结构再进行实体类以及映射文件编写的,特别是用代码生成器的时候. 但有时候不想用代码生成器,也不想定义表结构,那怎么办? 这个时候就会想到Hibernate, ...
- mybatis根据表逆向自动化生成代码(自动生成实体类、mapper文件、mapper.xml文件)
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- 使用mybatis generator插件,自动生成dao、dto、mapper等文件
mybatis generator 介绍 mybatis generator中文文档http://mbg.cndocs.tk/ MyBatis Generator (MBG) 是一个Mybatis的代 ...
随机推荐
- BZO4197 & 洛谷2150 & UOJ129:[NOI2015]寿司晚宴——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4197 https://www.luogu.org/problemnew/show/P2150 ht ...
- BZOJ1087:[SCOI2005]互不侵犯——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1087 Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王 ...
- CF25E:Test——题解
https://vjudge.net/problem/CodeForces-25E 题目大意:给三个字符串,求最小串,使得前三个串都是它的子串. ———————————————— 这题虽然是看哈希的时 ...
- bzoj4870: [Shoi2017]组合数问题(DP+矩阵乘法优化)
为了1A我居然写了个暴力对拍... 那个式子本质上是求nk个数里选j个数,且j%k==r的方案数. 所以把组合数的递推式写出来f[i][j]=f[i-1][j]+f[i-1][(j-1+k)%k].. ...
- JS深度合并对象
/** * 如果target(也就是FirstOBJ[key])存在, * 且是对象的话再去调用deepObjectMerge, * 否则就是FirstOBJ[key]里面没这个对象,需要与Secon ...
- BAT大数据面试题
1.kafka的message包括哪些信息 一个Kafka的Message由一个固定长度的header和一个变长的消息体body组成 header部分由一个字节的magic(文件格式)和四个字节的CR ...
- Codeforces Round #331 (Div. 2) B. Wilbur and Array
B. Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- UVA10766:Organising the Organisation(生成树计数)
Organising the Organisation 题目链接:https://vjudge.net/problem/UVA-10766 Description: I am the chief of ...
- HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- bzoj 4723 [POI2017]Flappy Bird 模拟
[POI2017]Flappy Bird Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 482 Solved: 196[Submit][Status ...