秦皇岛建设局局官方网站,萧县做网站,自适应网站什么做,浙江seo41_构造器映射 构造器映射#xff1a;XML Mapper项目0503构造器映射 ConstructorArgs注解项目0504构造器映射_注解 构造器映射#xff1a;
构造器映射能控制MyBatis调用指定、有参数的构造器来创建结果集要映射的Java对象
XML Mapper
在resultMap…/元素内添加XML Mapper项目0503构造器映射 ConstructorArgs注解项目0504构造器映射_注解 构造器映射
构造器映射能控制MyBatis调用指定、有参数的构造器来创建结果集要映射的Java对象
XML Mapper
在resultMap…/元素内添加constructor…/子元素来实现构造器映射。 constructor…/元素可包含多个idArg…/和arg…/子元素它们用于定义数据列与构造参数之间的对应关系。 其中idArg…/元素用于定义作为标识属性的构造参数它们可指定如下属性 - column指定列名。 - javaType指定属性的Java类型。通常需要指定 调用指定构造器需要根据构造器参数类型来匹配 - jdbcType指定该列的JDBC类型。一般无需指定MyBatis可以自动推断。 - typeHandler为该数据列与属性值之间的转换指定类型处理器。 - name指定构造参数的参数名。要用Param或-parameters编译选项 - select该属性引用另一个select…/元素的id指定使用嵌套查询来检索数据 - resultMap该属性引用另一个resultMap…/元素的id完成嵌套结果集的映射
项目0503构造器映射
映射文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
!-- 根元素是mapper该元素的namespace属性值相当于该mapper的唯一标识 --
mapper namespaceorg.itcheng.app.dao.NewsMapper!-- SQL的id需要与Mapper接口的方法名相同 --insert idsaveNewsinsert into news_inf values (null, #{title}, #{content})/insert!-- 定义SQL语句 --update idupdateNewsupdate news_inf set news_title #{title}, news_content #{content}where news_id#{id}/update!-- SQL的id需要与Mapper接口的方法名相同 --delete iddeleteNewsdelete from news_inf where news_id #{xyz} /delete!-- 使用resultMap来完成结果集与Java对象之间的映射关系--select idfindNews resultMapnewsMapselect * from news_inf where news_id #{id} /select!-- resultMap专门定义一个映射规则完成结果集与Java对象之间的映射关系 --resultMap typenews idnewsMapconstructor!-- 映射标识属性对应的构造器参数 --idArg columnnews_id javaTypeint /arg columnnews_title javaTypestring/!-- 此处包含几个参数映射就表明调用带几个参数的构造器 --/constructorresult columnnews_content propertycontent/!-- 映射映射普通属性 --/resultMap/mapperConstructorArgs注解
ConstructorArgs注解对应于 constructor…/元素 该注解的value属性可指定多个Arg注解 Arg相当于 idArg…/和arg…/的综合体当它的id属性为true代表idArg…/元素。
项目0504构造器映射_注解
package org.itcheng.app.dao;import java.util.List;import org.apache.ibatis.annotations.Arg;
import org.apache.ibatis.annotations.ConstructorArgs;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.itcheng.app.domain.News;// Mapper组件相当于DAO组件
public interface NewsMapper
{Insert(insert into news_inf values (null, #{title}, #{content}))int saveNews(News news);Update(update news_inf set news_title #{title}, news_content #{content}\r\n where news_id#{id})int updateNews(News news);Delete(delete from news_inf where news_id #{xyz})void deleteNews(Integer id);Select(select * from news_inf where news_id #{id})ConstructorArgs({Arg(column news_id, javaType Integer.class, id true),Arg(column news_title, javaType String.class)})Results({Result(column news_content, propertycontent)})ListNews findNews(Integer id);
}