.[代码]Mybatis+Spring+MySQL跳至[1][全屏预览]?12345678910111213141516171819202122232425262728293031323334353637383940实体beanpackagecom.pb.mybatis.entity;publicclassUser{Integerage;//年龄Integergender;//性别Integerid;Stringmail;//邮箱Stringnickname;Stringpassword;Stringsite;//个人站点Stringusername;publicUser(){super();//TODOAuto-generatedconstructorstub}publicUser(Integerid){super();this.id=id;}publicUser(Stringusername,Stringpassword){super();this.username=username;this.password=password;}publicUser(Stringpassword,Stringusername,Stringnickname,Integergender,Integerage,Stringmail,Stringsite){super();this.password=password;this.username=username;this.nickname=nickname;this.gender=gender;this.age=age;this.mail=mail;this.site=site;}publicUser(Integerid,Integerage,Integergender,Stringmail,Stringnickname,Stringpassword,Stringsite,Stringusername){4142434445464748495051525354555657585960616263646566676869707172737475767778798081828384super();this.age=age;this.gender=gender;this.id=id;this.mail=mail;this.nickname=nickname;this.password=password;this.site=site;this.username=username;}publicIntegergetAge(){returnage;}publicIntegergetGender(){returngender;}publicIntegergetId(){returnid;}publicStringgetMail(){returnmail;}publicStringgetNickname(){returnnickname;}publicStringgetPassword(){returnpassword;}publicStringgetSite(){returnsite;}publicStringgetUsername(){returnusername;}publicvoidsetAge(Integerage){this.age=age;}publicvoidsetGender(Integergender){this.gender=gender;}publicvoidsetId(Integerid){this.id=id;}publicvoidsetMail(Stringmail){85868788899091929394959697989910010110210310410510610710810911011111211311this.mail=mail;}publicvoidsetNickname(Stringnickname){this.nickname=nickname;}publicvoidsetPassword(Stringpassword){this.password=password;}publicvoidsetSite(Stringsite){this.site=site;}publicvoidsetUsername(Stringusername){this.username=username;}}实体类的UserDaopackagecom.pb.mybatis.dao;importjava.util.List;importcom.pb.mybatis.entity.User;importcom.pb.mybatis.util.UserPage;/***定义实体内操作接口Dao*@authorVoishion*@version2012.12.30*/publicinterfaceUserDao{/***保存*@paramuser*/voidsave(Useruser);/***查询所有*@return*/ListUserfindAll();/***按ID查询*@paramid*@return411511611711811912012112212312412512612712812913013113213313413513*/UserfindById(Integerid);/***删除*@paramuser*/voiddelete(Integerid);/***更新*@parammap*/voidupdate(Useruser);/***分页查询*@parampage*@return*/ListUserfindByPage(UserPagepage);/***分页查询时得到符合条件的数据*@parampage*@return*/IntegergetCount(UserPagepage);}实体类UserDao的实现UserDaoImpl-----packagecom.pb.mybatis.dao.impl;importjava.util.List;importcom.pb.mybatis.dao.UserDao;importcom.pb.mybatis.entity.User;importcom.pb.mybatis.mapper.UserMapper;importcom.pb.mybatis.util.UserPage;publicclassUserDaoImplimplementsUserDao{//在此处注入一个UserMapperprivateUserMapperuserMapper;613713813914014114214314414514614714814915015115215315415515615715publicvoidsetUserMapper(UserMapperuserMapper){this.userMapper=userMapper;}@Overridepublicvoiddelete(Integerid){userMapper.delete(id);}@OverridepublicListUserfindAll(){returnuserMapper.findAll();}@OverridepublicUserfindById(Integerid){returnuserMapper.findById(id);}@Overridepublicvoidsave(Useruser){userMapper.save(user);//thrownewRuntimeException(Error);}@Overridepublicvoidupdate(Useruser){userMapper.update(user);}@OverridepublicListUserfindByPage(UserPagepage){returnuserMapper.findByPage(page);}@OverridepublicIntegergetCount(UserPagepage){returnuserMapper.getCount(page);}}我想大家现在都在关心UserDaoImpl中的UserMapper是从哪里来的:首先我们需要一个分页查询的中间类UserPage:815916016116216316416516616716816917017117217317417517617717817918packagecom.pb.mybatis.util;importcom.pb.mybatis.entity.User;/***User分页查询公共条件类*@authorVoishion*/publicclassUserPage{IntegerfirstRec;IntegerpageSize;Useruser;publicIntegergetFirstRec(){returnfirstRec;}publicvoidsetFirstRec(IntegerfirstRec){this.firstRec=firstRec;}publicIntegergetPageSize(){returnpageSize;}publicvoidsetPageSize(IntegerpageSize){this.pageSize=pageSize;}publicUsergetUser(){returnuser;}publicvoidsetUser(Useruser){this.user=user;}}其次就是UserMapper映射了~~~~~~~~packagecom.pb.mybatis.mapper;importjava.util.List;importcom.pb.mybatis.entity.User;importcom.pb.mybatis.util.UserPage;/***定义实体内操作接口*@authorVoishion*@version2012.12.30018118218318418518618718818919019119219319419519619719819920020120*/publicinterfaceUserMapper{/***保存*@paramuser*/voidsave(Useruser);/***查询所有*@return*/ListUserfindAll();/***按ID查询*@paramid*@return*/UserfindById(Integerid);/***删除*@paramuser*/voiddelete(Integerid);/***更新*@parammap*/voidupdate(Useruser);/***分页查询*@parampage*@return*/ListUserfindByPage(UserPagepage);/***分页查询时得到符合条件的数据*@parampage*@return220320420520620720820921021121221321421521621721821922022122222322*/IntegergetCount(UserPagepage);}注意UserMapper要与在同一个包下的UserMapper.xml同名,且在同一包下:以下就是UserMapper的映射文件UserMapper.xml文件?xmlversion=1.0encoding=UTF-8?!DOCTYPEmapperPUBLIC-//mybatis.org//DTDMapper3.0//EN!--Mapper文件最好放在与Dao接口同一目次下--mappernamespace=com.pb.mybatis.mapper.UserMapper!--定义数据库字段与实体对象