一、简述
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis,实质上Mybatis对ibatis进行一些改进。 现在托管到gitHub上,下载:https://github.com/mybatis/mybatis-3/releases
MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动、创建connection、创建statement、手动设置参数、结果集检索等jdbc繁杂的过程代码。
这个demo是针对mybatis学习而进行的简单的crud操作,并通过jsonp跨域将查询封装的数据发送至前台。关于mybatis这个框架的原理我就不再赘述了,上实例!
二、运行环境
2.1 软件环境
Eclipse + Tomcat7 + Mysql
2.2 软件架构
servlet+ mybatis框架 + html + bootstrap框架 + ajax + jsonp
2.3 数据库表结构
CREATE TABLE `stu` (
`sno` varchar(10) NOT NULL,
`sname` varchar(20) NOT NULL,
`sage` int(10) DEFAULT NULL,
`ssex` varchar(5) DEFAULT NULL,
PRIMARY KEY (`sno`),
UNIQUE KEY `sno` (`sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
2.4 主要jar包
三、例子结构
3.1 web项目结构
3.2 前台项目结构
四、主要实现代码
4.1 mybatis.xml
4.2 StudentMapper.xml
INSERT INTO stu
VALUES
sno,
sname,
sage,
ssex,
#{sno},
#{sname},
#{sage},
#{ssex},
update stu
set sname = #{sname},
ssex = #{ssex},
sage = #{sage}
where sno = #{sno}
delete from stu where sno = #{sno}
4.3 Mapper实现类StudentDaoImpl.java
public class StudentDaoImpl implements StudentDao {
private SqlSession sqlSession;
public List findAllStudent() {
sqlSession = SqlSessionUtil.getSession();
List list = sqlSession.selectList("findAllStudent");
sqlSession.commit();
sqlSession.close();
return list;
}
public List findPageStudent(Integer startSize, Integer pageSize) {
sqlSession = SqlSessionUtil.getSession();
Map map = new HashMap();
map.put("startSize", startSize);
map.put("pageSize", pageSize);
List list = sqlSession.selectList("findPageStudent",map);
sqlSession.commit();
sqlSession.close();
return list;
}
public int doInsert(Student stu) {
sqlSession = SqlSessionUtil.getSession();
Integer result = sqlSession.insert("doInsert",stu);
sqlSession.commit();
sqlSession.close();
return result;
}
public int doDeleteBySno(String sno) {
sqlSession = SqlSessionUtil.getSession();
Integer result = sqlSession.delete("doDeleteBySno",sno);
sqlSession.commit();
sqlSession.close();
return result;
}
@Override
public int doUpdateStudent(Student stu) {
sqlSession = SqlSessionUtil.getSession();
Integer result = sqlSession.update("doUpdateStudent",stu);
sqlSession.commit();
sqlSession.close();
return result;
}
public int findCount() {
sqlSession = SqlSessionUtil.getSession();
Integer result = sqlSession.selectOne("findCount");
sqlSession.commit();
sqlSession.close();
return result;
}
}
4.4 StudentServiceImpl.java
private StudentDao stuDao = new StudentDaoImpl();
public List findAllStudent() {
return stuDao.findAllStudent();
}
public PageBean findPageStudent(Integer currentPage,
Integer pageSize) {
Integer count = stuDao.findCount();
Integer allPages = count/pageSize == 0 ? count/pageSize
: count/pageSize + 1;
if(currentPage > allPages){
currentPage = allPages;
}
if(currentPage < 1){
currentPage = 1;
}
PageBean pb = new PageBean();
Integer startSize = (currentPage-1)*pageSize;
List list = stuDao.findPageStudent(startSize, pageSize);
pb.setAllPages(allPages);
pb.setCount(count);
pb.setCurrentPage(currentPage);
pb.setCurrentCount(pageSize);
pb.setList(list);
return pb;
}
public int doInsert(Student stu) {
return stuDao.doInsert(stu);
}
public int doDeleteBySno(String sno) {
return stuDao.doDeleteBySno(sno);
}
public int doUpdateStudent(Student stu) {
return stuDao.doUpdateStudent(stu);
}
4.5 servlet主要操作的方法
4.5.1 分页查询方法
private void sendPageData(HttpServletRequest request,
HttpServletResponse response) throws IOException{
int pageSize = 5;
int currentPage = 1;
String currPage = request.getParameter("currentPage");
if(currPage!=null){
currentPage = Integer.parseInt(currPage);
}
PageBean pb = stuService.findPageStudent(currentPage, pageSize);
JSONObject json = JSONObject.fromObject(pb);
PrintWriter writer = response.getWriter();
String callback = request.getParameter("callback");
String result = callback + "(" + json.toString() + ")";
writer.write(result);
}
4.5.2 数据更新操作
private void doUpdate(HttpServletRequest request,
HttpServletResponse response) throws IOException{
String sno = request.getParameter("sno");
String sname = request.getParameter("sname");
String ssex = request.getParameter("ssex");
Integer sage = null;
if(request.getParameter("sage")!=null){
sage = Integer.parseInt(request.getParameter("sage"));
}
System.out.println("姓名:"+sname+"\t性别:"+ssex+"\t学号:"+sno+"\t年纪:"+sage);
Student stu = new Student();
stu.setSno(sno);
stu.setSname(sname);
stu.setSsex(ssex);
stu.setSage(sage);
JSONObject json = new JSONObject();
if(stuService.doUpdateStudent(stu)==1){
json.put("msg", "修改成功");
}else{
json.put("msg", "修改失败");
}
PrintWriter writer = response.getWriter();
String callback = request.getParameter("callback");
String result = callback + "(" + json.toString() + ")";
writer.write(result);
}
4.5.3 通过学号删除学生操作
private void doDeleteBySno(HttpServletRequest request,
HttpServletResponse response) throws IOException{
String sno = request.getParameter("sno");
JSONObject json = new JSONObject();
if(stuService.doDeleteBySno(sno)==1){
json.put("msg", "删除成功");
}else{
json.put("msg", "删除失败");
}
PrintWriter writer = response.getWriter();
String callback = request.getParameter("callback");
String result = callback + "(" + json.toString() + ")";
writer.write(result);
}
五、总结
对于mybatis框架的使用,主要还是在映射文件的配置,将数据库内所查出的数据与实体类的映射,从而实现数据的持久化操作。
MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动、创建connection、创建statement、手动设置参数、结果集检索等jdbc繁杂的过程代码。
对于前台页面,我会在下一篇文章中写出,如上面观点你们有所异议,欢迎大家给我指出。