MyBatis联合查询和修改例子

MyBatis现在还挺少人用的,刚玩的时候在网站查资料都没有很多贴研究它.走了很多弯路;

在此做了一个小例子,跟大家分享一下;

如果能对一些刚玩MyBatis的朋友一些帮助就再好不过了.

首先给大家配置MyBatis的前奏,毕竟什么框架都是配置出来的,大家得下载MyBatis的文档,上面有很详细的配置前奏.

我就不给大家贴出来了.我是用Spring和Struts2集成的.别怪我太自私啊!下次慢慢在贴出集成的例子,

 

先给大家sql的配置吧

我有一个总文件mybatis-config.xml,在总文件加载子文件,这样至少分工还算明细些.

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  3. <configuration>  
  4.     <typeAliases>  
  5.         <typeAlias alias="Account" type="com.cookiejoo.beans.Account" />  
  6.         <typeAlias alias="Weibo" type="com.cookiejoo.beans.Weibo" />  
  7.     </typeAliases>  
  8.     <mappers>  
  9.         <mapper resource="mybatisConfig/account-mapper.xml" />  
  10.         <mapper resource="mybatisConfig/weibo-mapper.xml" />  
  11.     </mappers>  
  12. </configuration>  

重点在这个weibo-mapper.xml里面,这个也一样自己写他的sql映射account-mapper.xml

由于就演示一个表weibo关联表account,关联都在weibo-mapper.xml里面写.account-mapper.xml就不给大家了,没什么大碍的.

配置文件的详细我都写了,大家靠自己的悟性吧.

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  3. <!-- 命名空间都以pojo类的包命名,很长,但是也好区分,文档上说这是MyBatis现在的规定了 -->  
  4. <mapper namespace="com.cookiejoo.beans.Weibo">  
  5.     <!-- 定义一张表,并且来个关联表 -->  
  6.     <resultMap type="Weibo" id="weiboJoinAccountResultMap">  
  7.         <id property="wId" column="w_id" />  
  8.         <result property="wContext" column="w_context" />  
  9.         <result property="wCreateTime" column="w_create_time" />  
  10.         <!-- 关联account表 -->  
  11.         <association property="account" column="w_acc_id"  
  12.             javaType="Account" resultMap="joinAccountResult" />  
  13.     </resultMap>  
  14.       
  15.     <!-- 关联表需要的列 -->  
  16.     <resultMap type="Account" id="joinAccountResult">  
  17.         <id property="aId" column="a_id" />  
  18.         <result property="aUsername" column="a_username" />  
  19.         <result property="aHeadImage" column="a_head_image" />  
  20.     </resultMap>  
  21.       
  22.     <!-- 定义没有关联的单表查询返回结果 -->  
  23.     <resultMap type="Weibo" id="weiboResultMap">  
  24.         <id property="wId" column="w_id" />  
  25.         <result property="wContext" column="w_context" />  
  26.         <result property="wCreateTime" column="w_create_time" />  
  27.         <!-- 关联account表 -->  
  28.         <association property="account" column="w_acc_id"  
  29.             javaType="Account" resultMap="joinAccountResult" />  
  30.     </resultMap>  
  31.       
  32.     <!-- 根据创建时间查询 -->  
  33.     <select id="findWeiboJoinAccount"  
  34.         resultMap="weiboJoinAccountResultMap" parameterType="Weibo">  
  35.         select w.w_id,w.w_context,w.w_create_time, a.a_id as  
  36.         w_acc_id,a.a_username,a.a_head_image from weibo w left outer  
  37.         join account a on w.w_acc_id = a.a_id where w.w_create_time >  
  38.         #{wCreateTime} order by w.w_create_time desc  
  39.     </select>  
  40.       
  41.     <!-- resultMap 返回 上面的结果 -->  
  42.     <select id="findAllWeibo" resultMap="weiboResultMap">  
  43.         select w.w_id,w.w_context,w.w_create_time, a.a_id  
  44.         from weibo w left join account a on w.w_acc_id = a.a_id  
  45.     </select>  
  46.   
  47.     <!-- 查询一条记录 -->  
  48.     <!-- 注意:此处的关联,看pojo类是怎么写的,如果查询出现什么错误,得看这里了 -->  
  49.     <select id="findAllWeiboById" parameterType="int"  
  50.          resultMap="weiboResultMap">  
  51.         select w.w_id,w.w_context,w.w_create_time, w.w_acc_id   
  52.         from weibo w  
  53.         where w.w_id = #{wId}  
  54.     </select>  
  55.       
  56.     <!--    
  57.     首先,如果你的数据库支持自动生成主键的字段(比如MySQL和SQL Server),  
  58.     那么你可以设置useGeneratedKeys=”true”,  
  59.     而且设置keyProperty到你已经做好的目标属性上。  
  60.     例如,如果上面的Author表已经对id使用了自动生成的列类型,那么语句可以修改为  
  61.     -->  
  62.     <insert id="addWeibo" parameterType="Weibo" useGeneratedKeys="true"  
  63.         keyProperty="wId">  
  64.         insert into weibo(w_acc_id,w_context,w_create_time)  
  65.         values(#{account.aId},#{wContext},#{wCreateTime})  
  66.     </insert>  
  67.       
  68.     <!-- 修改 -->  
  69.     <update id="updateWeibo" parameterType="Weibo">  
  70.         update weibo set w_acc_id = #{account.aId},w_context = #{wContext},w_create_time = #{wCreateTime}  
  71.         where w_id = #{wId}  
  72.     </update>  
  73.       
  74.     <!-- 删除 -->  
  75.     <delete id="deleteWeibo" parameterType="int">  
  76.         delete from weibo where w_id = #{wId}  
  77.     </delete>  
  78.   
  79. </mapper>  


pojo类:这里你要注意了, 怎么给数据做搜集,

[java]  view plain  copy
  1. package com.cookiejoo.beans;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class Weibo {  
  6.     private Integer wId;  
  7.     private Account account;  
  8.     private String wContext;  
  9.     private Date wCreateTime;  
  10.   
  11.     public Weibo() {  
  12.     }  
  13.   
  14.     public Integer getWId() {  
  15.         return wId;  
  16.     }  
  17.   
  18.     public void setWId(Integer wId) {  
  19.         this.wId = wId;  
  20.     }  
  21.   
  22.     public Account getAccount() {  
  23.         return account;  
  24.     }  
  25.   
  26.     public void setAccount(Account account) {  
  27.         this.account = account;  
  28.     }  
  29.   
  30.     public String getWContext() {  
  31.         return wContext;  
  32.     }  
  33.   
  34.     public void setWContext(String context) {  
  35.         wContext = context;  
  36.     }  
  37.   
  38.     public Date getWCreateTime() {  
  39.         return wCreateTime;  
  40.     }  
  41.   
  42.     public void setWCreateTime(Date createTime) {  
  43.         wCreateTime = createTime;  
  44.     }  
  45.   
  46. }  


 


两张表结构很简单

id都是自动增长的;

Table weibo

===========

w_id, w_acc_id, w_context, w_create_time

-----------

w_id             int(11) PK

w_acc_id         int(11)

w_context        varchar(2000)

w_create_time    datetime

 

Table account

=============

a_id, a_username, a_password, a_sex, a_phone, a_brithday, a_create_time, a_head_image

-------------

a_id             int(11) PK

a_username       varchar(45)

a_password       varchar(45)

a_sex            int(2)

a_phone          varchar(15)

a_brithday       datetime

a_create_time    datetime

a_head_image     varchar(45)


这个是java调用的例子,一个接口一个实现类,我用了Spring集成了,所以和单独的MyBatis例子有点出入,大家对着MyBatis文档做时就是获取getSqlSession这个不一样而已.

[java]  view plain  copy
  1. package com.cookiejoo.iservice.impl;  
  2.   
  3. import java.util.Date;  
  4. import java.util.List;  
  5.   
  6. import org.mybatis.spring.support.SqlSessionDaoSupport;  
  7. import com.cookiejoo.beans.Weibo;  
  8. import com.cookiejoo.iservice.IWeiboService;  
  9.   
  10. public class WeiboServiceImpl extends SqlSessionDaoSupport implements  
  11.         IWeiboService {  
  12.   
  13.     @SuppressWarnings("unchecked")  
  14.     public List<Weibo> findWeiboJoinAccount(Weibo w) {  
  15.         return getSqlSession().selectList(  
  16.                 "com.cookiejoo.beans.Weibo.findWeiboJoinAccount", w);  
  17.     }  
  18.   
  19.     @SuppressWarnings("unchecked")  
  20.     public List<Weibo> findAllWeibo() {  
  21.         return getSqlSession().selectList(  
  22.                 "com.cookiejoo.beans.Weibo.findAllWeibo");  
  23.     }  
  24.   
  25.     public void addWeibo(Weibo w) {  
  26.         w.setWCreateTime(new Date());  
  27.         getSqlSession().insert("com.cookiejoo.beans.Weibo.addWeibo", w);  
  28.     }  
  29.   
  30.     public void updateWeibo(Weibo w) {  
  31.         getSqlSession().update("com.cookiejoo.beans.Weibo.updateWeibo", w);  
  32.     }  
  33.   
  34.     public void deleteWeibo(Weibo w) {  
  35.         getSqlSession().delete("com.cookiejoo.beans.Weibo.deleteWeibo", w);  
  36.     }  
  37.   
  38.     public Weibo findAllWeiboById(Integer wId) {  
  39.         return (Weibo) getSqlSession().selectOne(  
  40.                 "com.cookiejoo.beans.Weibo.findAllWeiboById", wId);  
  41.     }  
  42.   
  43. }  


 

接着页面展示,我用jsp写的,用struts2做跳转...     myJsp.jsp

[html]  view plain  copy
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. <%@ taglib prefix="s" uri="/struts-tags"%>  
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
  6. <html xmlns="http://www.w3.org/1999/xhtml">  
  7.     <head>  
  8.         <meta http-equiv="content-type" content="text/html; charset=utf-8" />  
  9.         <title>Condition  by Free CSS Templates</title>  
  10.         <meta name="keywords" content="" />  
  11.         <meta name="description" content="" />  
  12.         <style type='text/css'>  
  13.         #mytable {  
  14.             padding: 0;  
  15.             margin: 0;  
  16.         }  
  17.           
  18.         th {  
  19.             color: #4f6b72;  
  20.             border-left: 1px solid #C1DAD7;  
  21.             border-right: 1px solid #C1DAD7;  
  22.             border-bottom: 1px solid #C1DAD7;  
  23.             border-top: 1px solid #C1DAD7;  
  24.             letter-spacing: 2px;  
  25.             text-transform: uppercase;  
  26.             text-align: left;  
  27.             padding: 6px 6px 6px 12px;  
  28.             background: #CAE8EA no-repeat;  
  29.         }  
  30.           
  31.         td {  
  32.             border-left: 1px solid #C1DAD7;  
  33.             border-right: 1px solid #C1DAD7;  
  34.             border-bottom: 1px solid #C1DAD7;  
  35.             background: #fff;  
  36.             padding: 6px 6px 6px 12px;  
  37.             color: #4f6b72;  
  38.         }  
  39.         </style>  
  40.     </head>  
  41.   
  42.     <body>  
  43.     -------------------------------------------------------------------------------------------  
  44.     <form action="findAllWeibo.action"><input value="findAllWeibo" type="submit"/>  
  45.     <input name="weibo.wCreateTime" type="text" value="2011-01-01"/>  
  46.     </form>  
  47.     -------------------------------------------------------------------------------------------  
  48.     <form action="addWeibo.action">  
  49.     <input name="weibo.wContext" type="text" value="2011-01-01"/><br>  
  50.     <input name="weibo.account.aId" type="text" value="1"/><br>  
  51.     <input name="weibo.wCreateTime" type="text" value="2011-01-01"/>  
  52.     <input value="addWeibo" type="submit"/>  
  53.     </form>  
  54.     -------------------------------------------------------------------------------------------  
  55.     <form action="updateWeibo.action">  
  56.     <input name="weibo.wContext" type="text" value="2011-02-01"/><br>  
  57.     <input name="weibo.account.aId" type="text" value="2"/><br>  
  58.     <input name="weibo.wCreateTime" type="text" value="2011-02-01"/>  
  59.     <input name="weibo.wId" type="text" value="54"/>  
  60.     <input value="updateWeibo" type="submit"/>  
  61.     </form>  
  62.       
  63.     <a href="findWeiboByBean.action">findWeiboByBean</a>  
  64.         <h3></h3>  
  65.         <br>  
  66.         <table id='mytable' cellspacing='0'  width='100%'>  
  67.             <tr>  
  68.             <th>id</th><th>context</th><th>createTime</th><th>aid</th><th>operator</th></tr>  
  69.             <c:forEach items="${weibos}" var="weibo">  
  70.                 <tr><td>${weibo.WId }</td><td>${weibo.WContext }</td><td>${weibo.WCreateTime }</td><td>${weibo.account.AId }</td><td><a href="deleteWeibo.action?weibo.wId=${weibo.WId }">delete</a></td></tr>  
  71.             </c:forEach>  
  72.         </table>  
  73.     </body>  
  74.       
  75.       
  76. </html>  
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值