mybatis部分
一、接口编程配置
1.mybatis配置
从sprin mvc和mybatis整合的笔记(一)中,提取关键点配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- mybatis接口编程,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 接口代码所在的包路径 -->
<property name="basePackage" value="com.lzj.test.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
2.编写接口代码
package com.lzj.test.mapper;
import org.apache.ibatis.annotations.Param;
import com.lzj.test.dto.UsrAddress;
public interface IAddressMapper {
public UsrAddress loadPrimaryAddress(@Param("usrOnlyid")Long usrOnlyid, @Param("addressType")Long addressType);
public int updateAddress(UsrAddress usrAddress);
}
3.mapper配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace对应接口的全路径 -->
<mapper namespace="com.lzj.test.mapper.IAddressMapper">
<resultMap type="com.lzj.test.dto.UsrAddress" id="usrAddressResult">
<id column="ADDRESSID" property="addressid" />
<result column="USR_ONLYID" property="usrOnlyid" />
<result column="USR_ADDRESS" property="usrAddress" />
<result column="POSTAL_CODE" property="postalCode" />
<result column="COUNTRY" property="country" />
<result column="PROVINCE" property="province" />
<result column="CITY" property="city" />
<result column="USR_NAME" property="usrName" />
</resultMap>
<!-- id对应接口中的方法名 -->
<select id="loadPrimaryAddress" parameterType="java.lang.Long" resultMap="usrAddressResult">
SELECT d.PROVINCE AS province,d.CITY AS city,d.USR_NAME AS usrName,
FROM USRADDRESS d
WHERE d.USR_ONLYID=#{usrOnlyid}
AND d.ADDRESS_TYPE=#{addressType}
AND d.DEL_FLAG=1
LIMIT 1;
</select>
<update id="updateAddress" parameterType="com.lzj.test.dto.UsrAddress">
UPDATE USRADDRESS d
SET d.USR_ADDRESS = #{usrAddress}
WHERE d.USR_ONLYID = #{usrOnlyid} ;
</update>
</mapper>
二、Mapper XML 文件
1.单个关联查询
mapper配置:
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<association property="author" 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"/>
</association>
</resultMap>
<select id="selectBlog" resultMap="blogResult">
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
A.id as author_id,
A.username as author_username,
A.password as author_password,
A.email as author_email,
A.bio as author_bio
from Blog B left outer join Author A on B.author_id = A.id
where B.id = #{id}
</select>
javabean:
public class Blog{
private int id;
private String title;
private int author_id;
private Author author;
//.......
}
2.集合关联查询
mapper配置:
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<!-- ofType:集合里面的javabean -->
<collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<result property="body" column="post_body"/>
</collection>
</resultMap>
<select id="selectBlog" resultMap="blogResult">
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
P.id as post_id,
P.subject as post_subject,
P.body as post_body,
from Blog B
left outer join Post P on B.id = P.blog_id
where B.id = #{id}
</select>
javabean:
public class Blog{
private int id;
private String title;
private int author_id;
private List<Post> posts;
//.......
}
3.mybatis支持动态sql,类似jstl的if choose 语句
具体使用查看mybatis文档
其他:
mybatis中文在线文档:http://www.mybatis.org/mybatis-3/zh/index.html
mybaits常见面试题:http://www.cnblogs.com/huajiezh/p/6415388.html