这篇文章主要介绍了mybatis一对多两种mapper写法实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
mybatis一对多两种mapper写法
第一种
u.*,p.id as pid,p.user_id,p.name,p.icon,p.vsersion,p.tags,p.description,p.create_time as bcreate_time,p.update_time as bupdate_time,p.del_flag from t_user u LEFT JOIN t_plugin p ON u.id=p.user_id and u.del_flag=0 and p.del_flag=0 WHERE u.user_name LIKE CONCAT('%',#{name},'%') OR u.nick_name LIKE CONCAT('%',#{name},'%')
第二种
//column='id' 为关联查询所需条件
SELECT * FROM t_user WHERE del_flag = 0 AND ( user_name LIKE CONCAT( '%', #{name},'%') OR nick_name LIKE CONCAT( '%', #{name},'%')) //下个sql语句依赖上个 SELECT id,user_id as userId,name,icon,vsersion,tags,description, create_time as createTime ,update_time as updateTime ,del_flag as delFlag FROM t_plugin WHERE del_flag = 0 AND user_id = #{id}
补充知识:Mybatis 一个dao 对应多个Mapper.xml
由于项目中的mybatis的mapper是用mybatis generator自动生成的,但是生成的mapper满足不了我的业务,需要自己扩展,所以就研究了下、
添加接口
创建mapper.xml
修改配置
1.添加接口
在原dao中加个接口
/** ---------------自定义Mapper--------------- **/
List select(ClusterInstanceBO clusterInstanceBO);
2. 创建mapper.xml
PcacheClusterMapperExtend.xml
select from t_pcache_template and template_id = #{templateId} and template_name = #{templateName} and template_type = #{templateType} and create_user = #{createuser} and create_time = #{createTime,jdbcType=TIMESTAMP} order by create_time desc
3. 修改配置
项目目录:
添加mapper扫描路径
classpath:mybatis/mappers/*Mapper.xmlclasspath:mybatis/mappers/extend/*MapperExtend.xml helperDialect=MysqL reasonable=true supportMethodsArguments=true params=count=countsql autoRuntimeDialect=true
mybatis generator 已经过时了哦,太麻烦,耦合性高,建议使用通用Mapper,完美继承spring,springboot
学习地址:https://gitee.com/free/Mapper/wikis/Home