以前东拼西凑来的,不知道都是哪些链接!
User 、Role 的对应关系是,一个用户有多个角色,因此,在 User 的实体中加入一个 Role 的属性 private List<Role> roles;对应一对多的关系。
- <select id="queryForList" resultMap="queryForListMap">
- SELECT
- u.id,
- u.username,
- u.password,
- r.id r_id,
- r.name r_name,
- r.jsms r_jsms,
- r.bz r_bz,
- r.jlzt r_jlzt,
- r.glbm r_glbm
- FROM
- user u
- LEFT JOIN
- role r
- ON
- u.id = r.userid
- </select>
- <resultMap id="queryForListMap" type="com.sica.domain.User">
- <id column="id" property="id" jdbcType="VARCHAR"/>
- <result column="username" property="username" jdbcType="VARCHAR"/>
- <result column="password" property="password" jdbcType="VARCHAR"/>
- <collection property="roles" javaType="java.util.List" ofType="com.sica.domain.Role">
- <id column="r_id" property="id" jdbcType="VARCHAR" />
- <result column="r_name" property="name" jdbcType="VARCHAR" />
- <result column="r_jsms" property="jsms" jdbcType="VARCHAR" />
- <result column="r_bz" property="bz" jdbcType="VARCHAR" />
- <result column="r_jlzt" property="jlzt" jdbcType="INTEGER" />
- <result column="r_glbm" property="glbm" jdbcType="VARCHAR" />
- </collection>
- </resultMap>
1.MyBatis实现一对一有几种方式?具体怎么操作的
有联合查询和嵌套查询,联合查询是几个表联合查询,只查询一次,
通过在resultMap里面配置association节点配置一对一的类就可以完成;
嵌套查询是先查一个表,根据这个表里面
的结果的外键id,去再另外一个表里面查询数据,也是通过association配置,但另外一个表
的查询通过select属性配置
- <resultMap id=”blogResult” type=”Blog”>
- <association property="author" column="blog_author_id" javaType="Author"
- select=”selectAuthor”/>
- </resultMap>
- <select id=”selectBlog” parameterType=”int” resultMap=”blogResult”>
- SELECT * FROM BLOG WHERE ID = #{id}
- </select>
- <select id=”selectAuthor” parameterType=”int” resultType="Author">
- SELECT * FROM AUTHOR WHERE ID = #{id}
- </select>
有联合查询和嵌套查询,联合查询是几个表联合查询,只查询一次,通过在resultMap里面配
置collection节点配置一对多的类就可以完成;
- <resultMap id="detailedBlogResultMap" type="Blog">
- <constructor>
- <idArg column="blog_id" javaType="int"/>
- </constructor>
- <result property="title" column="blog_title"/>
- <association property="author" column="blog_author_id" javaType=" Author">
- <id property="id" column="author_id"/>
- <result property="username" column="author_username"/>
- <result property="password" column="author_password"/>
- <result property="email" column="author_email"/>
- <result property="bio" column="author_bio"/>
- <result property="favouriteSection" column="author_favourite_section"/>
- </association>
- <collection property="posts" ofType="Post">
- <id property="id" column="post_id"/>
- <result property="subject" column="post_subject"/>
- <association property="author" column="post_author_id" javaType="Author"/>
- <collection property="comments" column="post_id" ofType=" Comment">
- <id property="id" column="comment_id"/>
- </collection>
结果的外键id,去再另外一个表里面查询数据,也是通过配置collection,但另外一个表的
查询通过select节点配置
3.使用resultType、ResultMap处理返回结果
resultType:指定返回值结果的完全限定名,处理多表查询的结果。多表查询需要定义vo封装查询的结果。只能用在单表查询或者定义vo的情况,如果是定义vo不能将关联的数据封装为需要获得某个类的对象
使用resultMap:不需要定义vo类,将关联数据对应的类,作为另外一个类的属性。
第一步:定义resultMap
第二步:引用resultMap
使用resultMap:用来多表关联的复杂查询中,通过需要将关联查询的数据封装为某个实体类对象,如果有特殊业务需要或者说明需要将管理数据封装为实体类的对象,使用resultMap
4.Xml映射文件中,除了常见的select|insert|updae|delete标签之外,还有哪些标签?
还有很多其他的标签,<resultMap>、<parameterMap>、<sql>、<include>、<selectKey>,加上动态sql的9个标签,trim|where|set|foreach|if|choose|when|otherwise|bind等,其中<sql>为sql片段标签,通过<include>标签引入sql片段,<selectKey>为不支持自增的主键生成策略标签。
choose (when, otherwise)标签
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
select * from t_blog where 1 = 1
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<otherwise>
and owner = "owner1"
</otherwise>
</choose>
</select>
parameterType使用了Java.lang.String类型
bind标签的value值得写法:value="'%'+_parameter.getEmail() +'%'"
SQL语句中使用:email like #{pattern}
,注意是#,而不是$
最后,测试时使用的代码也需要注意。<select id="findUserByFuzzyEmail" resultMap="BaseResultMap" parameterType="java.lang.String"> select id,username,email,status,createtime,updatetime from tb_user <bind name="pattern"value="'%'+_parameter.getEmail() +'%'" /> <where> <if test="email != null and email != ''"> email like #{pattern} </if> </where> </select>
selectKey 标签 生成主键
在insert语句中,在Oracle经常使用序列、在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键。
- <selectKey keyProperty="studentId" resultType="String" order="BEFORE">
- select nextval('student')
- </selectKey>
属性 | 描述 | 取值 |
keyProperty | selectKey 语句生成结果需要设置的属性。 | |
resultType | 生成结果类型,MyBatis 允许使用基本的数据类型,包括String 、int类型。 | |
order | 1:BEFORE,会先选择主键,然后设置keyProperty,再执行insert语句; 2:AFTER,就先运行insert 语句再运行selectKey 语句。 | BEFORE(orcle) AFTER(mysql) |
3、使用foreach进行sql语句拼接
在向sql传递数组或List,mybatis使用foreach解析,我们可以使用foreach中元素进行sql语句的拼接,请求数据。
通过一个例子来看:
需求:SELECT * FROM USER WHERE id=1 OR id=10 OR id=16
或者:SELECT * FROM USER WHERE id IN(1,10,16)
<if test="ids != null"> <foreach collection="ids"item="user_id"open="AND ("close=")" separator="or" > 每次遍历需要拼接的串 id= #{user_id} </foreach> </if>
其中,collection:指定输入对象中集合属性,item: 每个遍历生成对象,open:开始遍历时拼接串,close: 结束遍历是拼接的串,separator: 遍历的两个对象中需要拼接的串
使用set+if标签修改后,如果某项为null则不进行更新(如studentName为null则studentName不更新,sex,birthday更新),而是保持数据库原值。如下示例:
Xml代码
- <!-- 更新学生信息 -->
- <update id="updateStudent" parameterType="StudentEntity">
- UPDATE STUDENT_TBL
- <set>
- <if test="studentName!=null and studentName!='' ">
- STUDENT_TBL.STUDENT_NAME = #{studentName},
- </if>
- <if test="studentSex!=null and studentSex!='' ">
- STUDENT_TBL.STUDENT_SEX = #{studentSex},
- </if>
- <if test="studentBirthday!=null ">
- STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
- </if>
- <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">
- STUDENT_TBL.CLASS_ID = #{classEntity.classID}
- </if>
- </set>
- WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
- </update>
trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。
where例子的等效trim语句:
Xml代码
- <!-- 查询学生list,like姓名,=性别 -->
- <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">
- SELECT * from STUDENT_TBL ST
- <trim prefix="WHERE" prefixOverrides="AND|OR">
- <if test="studentName!=null and studentName!='' ">
- ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
- </if>
- <if test="studentSex!= null and studentSex!= '' ">
- AND ST.STUDENT_SEX = #{studentSex}
- </if>
- </trim>
- </select>
bind标签
parameterType使用了Java.lang.String类型
bind标签的value值得写法:value="'%'+_parameter.getEmail() +'%'"
SQL语句中使用: email like #{pattern}
,注意是#,而不是$
最后,测试时使用的代码也需要注意。
<select id="findUserByFuzzyEmail" resultMap="BaseResultMap"
parameterType="java.lang.String">
select id,username,email,status,createtime,updatetime from tb_user
<bind name="pattern"value="'%'+_parameter.getEmail() +'%'" />
<where>
<if test="email != null and email != ''">
email like #{pattern}
</if>
</where>
</select>
5、最佳实践中,通常一个Xml映射文件,都会写一个Dao接口与之对应,请问,这个Dao接口的工作原理是什么?Dao接口里的方法,参数不同时,方法能重载吗
答:Dao接口,就是Mapper接口,接口的全限名,就是映射文件中的namespace的值,接口的方法名,就是映射文件中MappedStatement的id值,接口方法内的参数,就是传递给sql的参数。Mapper接口是没有实现类的,当调用接口方法时,接口全限名+方法名拼接字符串作为key值,可唯一定位一个MappedStatement,举例:com.mybatis3.mappers.StudentDao.findStudentById,可以唯一找到namespace为com.mybatis3.mappers.StudentDao下面id = findStudentById的MappedStatement。在Mybatis中,每一个<select>、<insert>、<update>、<delete>标签,都会被解析为一个MappedStatement对象。
Dao接口里的方法,是不能重载的,因为是全限名+方法名的保存和寻找策略。
Dao接口的工作原理是JDK动态代理,Mybatis运行时会使用JDK动态代理为Dao接口生成代理proxy对象,代理对象proxy会拦截接口方法,转而执行MappedStatement所代表的sql,然后将sql执行结果返回。
6.使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值。
方法:在mapper中指定keyProperty属性,示例如下:
- <insert id="insertAndGetId" useGeneratedKeys="true" keyProperty="userId" parameterType="com.chenzhou.mybatis.User">
- insert into user(userName,password,comment)
- values(#{userName},#{password},#{comment})
- </insert>
7. Mybatis是如何将sql执行结果封装为目标对象并返回的?都有哪些映射形式?
第一种是使用<resultMap>标签,逐一定义列名和对象属性名之间的映射关系。
第二种是使用sql列的别名功能,将列别名书写为对象属性名,比如T_NAME AS NAME,对象属性名一般是name,小写,但是列名不区分大小写,Mybatis会忽略列名大小写,智能找到与之对应对象属性名
有了列名与属性名的映射关系后,Mybatis通过反射创建对象,同时使用反射给对象的属性逐一赋值并返回,那些找不到映射关系的属性,是无法完成赋值的。
8.Mybatis的Xml映射文件中,不同的Xml映射文件,id是否可以重复?
答:不同的Xml映射文件,如果配置了namespace,那么id可以重复;如果没有配置namespace,那么id不能重复;毕竟namespace不是必须的,只是最佳实践而已。
原因就是namespace+id是作为Map<String, MappedStatement>的key使用的,如果没有namespace,就剩下id,那么,id重复会导致数据互相覆盖。有了namespace,自然id就可以重复,namespace不同,namespace+id自然也就不同。
9.当实体类中的属性名和表中的字段名不一样
解决办法①:
通过在查询的sql语句中定义字段名的别名,让字段名的别名和实体类的属性名一致
<select id=”selectorder” parametertype=”int” resultetype=”me.gacl.domain.order”>
select order_id id, order_no orderno ,order_price price form orders where order_id=#{id};
</select>
解决办法②:
通过<resultMap>来映射字段名和实体类属性名的一一对应的关系
<resultMap type=”me.gacl.domain.order” id=”orderresultmap”>
<!–用id属性来映射主键字段–>
<id property=”id” column=”order_id”>
<!–用result属性来映射非主键字段–>
<result property = “orderno” column =”order_no”/>
<result property=”price” column=”order_price” />
</reslutMap>
10.IBatis和MyBatis在核心处理类分别叫什么?
IBatis里面的核心处理类交SqlMapClient, MyBatis里面的核心处理类叫做SqlSession