spring mvc+mybatis笔记(三)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值