copy http://blog.csdn.net/caodegao/article/details/6735049
企业级项目实战(带源码)地址:http://zz563143188.iteye.com/blog/1825168
MyBatis现在还挺少人用的,刚玩的时候在网站查资料都没有很多贴研究它.走了很多弯路;
在此做了一个小例子,跟大家分享一下;
如果能对一些刚玩MyBatis的朋友一些帮助就再好不过了.
首先给大家配置MyBatis的前奏,毕竟什么框架都是配置出来的,大家得下载MyBatis的文档,上面有很详细的配置前奏.
我就不给大家贴出来了.我是用Spring和Struts2集成的.别怪我太自私啊!下次慢慢在贴出集成的例子,
先给大家sql的配置吧
我有一个总文件mybatis-config.xml,在总文件加载子文件,这样至少分工还算明细些.
[html] view plaincopy
重点在这个weibo-mapper.xml里面,这个也一样自己写他的sql映射account-mapper.xml
由于就演示一个表weibo关联表account,关联都在weibo-mapper.xml里面写.account-mapper.xml就不给大家了,没什么大碍的.
配置文件的详细我都写了,大家靠自己的悟性吧.
[html] view plaincopy
insert into weibo(w_acc_id,w_context,w_create_time)
values(#{account.aId},#{wContext},#{wCreateTime})
update weibo set w_acc_id = #{account.aId},w_context = #{wContext},w_create_time = #{wCreateTime}
where w_id = #{wId}
delete from weibo where w_id = #{wId}
pojo类:这里你要注意了, 怎么给数据做搜集,
[java] view plaincopy
package com.cookiejoo.beans;
import java.util.Date;
public class Weibo {
private Integer wId;
private Account account;
private String wContext;
private Date wCreateTime;
public Weibo() {
}
public Integer getWId() {
return wId;
}
public void setWId(Integer wId) {
this.wId = wId;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getWContext() {
return wContext;
}
public void setWContext(String context) {
wContext = context;
}
public Date getWCreateTime() {
return wCreateTime;
}
public void setWCreateTime(Date createTime) {
wCreateTime = createTime;
}
}
两张表结构很简单
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 plaincopy
package com.cookiejoo.iservice.impl;
import java.util.Date;
import java.util.List;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.cookiejoo.beans.Weibo;
import com.cookiejoo.iservice.IWeiboService;
public class WeiboServiceImpl extends SqlSessionDaoSupport implements
IWeiboService {
@SuppressWarnings("unchecked")
public List
return getSqlSession().selectList(
"com.cookiejoo.beans.Weibo.findWeiboJoinAccount", w);
}
@SuppressWarnings("unchecked")
public List
return getSqlSession().selectList(
"com.cookiejoo.beans.Weibo.findAllWeibo");
}
public void addWeibo(Weibo w) {
w.setWCreateTime(new Date());
getSqlSession().insert("com.cookiejoo.beans.Weibo.addWeibo", w);
}
public void updateWeibo(Weibo w) {
getSqlSession().update("com.cookiejoo.beans.Weibo.updateWeibo", w);
}
public void deleteWeibo(Weibo w) {
getSqlSession().delete("com.cookiejoo.beans.Weibo.deleteWeibo", w);
}
public Weibo findAllWeiboById(Integer wId) {
return (Weibo) getSqlSession().selectOne(
"com.cookiejoo.beans.Weibo.findAllWeiboById", wId);
}
}
接着页面展示,我用jsp写的,用struts2做跳转... myJsp.jsp
[html] view plaincopy
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
findWeiboByBean
id | context | createTime | aid | operator |
---|---|---|---|---|
${weibo.WId } | ${weibo.WContext } | ${weibo.WCreateTime } | ${weibo.account.AId } | delete |