MVC,即Model、View、Controller。MVC可以理解为一种代码组织方式,可以为代码编写减少一些成本。下面通过一个项目的代码来体验一下MVC:项目介绍:数据库中有一个account表,里面存放了用户的账号、密码、用户名、余额。本项目是实现账户表的增删改查。account表的定义:createtableaccount(idintprimarykeyauto_increment,account_namevarchar(30),account_pwdvarchar(30),account_novarchar(30),account_balanceint);entity层账户类定义:/****@TableNameaccount*/@DatapublicclassAccountimplementsSerializable{//privateIntegerid;私人字符串帐户名;//私有字符串accountPwd;//私有字符串帐户号;//私有整数账户余额;privatestaticfinallongserialVersionUID=1L;@Overridepublicbooleanequals(Objectthat){if(this==that){返回真;}if(that==null){返回false;}if(getClass()!=that.getClass()){返回false;}帐户其他=(帐户)那;return(this.getId()==null?other.getId()==null:this.getId().equals(other.getId()))&&(this.getAccountName()==null?other.getAccountName()==null:this.getAccountName().equals(other.getAccountName()))&&(this.getAccountPwd()==null?other.getAccountPwd()==null:this.getAccountPwd().equals(other.getAccountPwd()))&&(this.getAccountNo()==null?other.getAccountNo()==null:this.getAccountNo().equals(other.getAccountNo()))&&(this.getAccountBalance()==null?other.getAccountBalance()==null:this.getAccountBalance().equals(other.getAccountBalance()));}@OverridepublicinthashCode(){finalintprime=31;整数结果=1;结果=素数*结果+((getId()==null)?0:getId().hashCode());结果=素数*结果+((getAccountName()==null)?0:getAccountName().hashCode());结果=素数*结果+((getAccountPwd()==null)?0:getAccountPwd().hashCode());结果=素数*结果+((getAccountNo()==null)?0:getAccountNo().hashCode());结果=素数*结果+((getAccountBalance()==null)?0:getAccountBalance().hashCode());返回结果;}@OverridepublicStringtoString(){StringBuildersb=newStringBuilder();sb.append(getClass().getSimpleName());sb.append("[");sb.append("Hash=").append(hashCode());sb.append(",id=").append(id);sb.append(",accountName=").append(accountName);sb.append(",accountPwd=").append(accountPwd);sb.append(",accountNo=").append(accountNo);sb.append(",accountBalance=").append(accountBalance);sb.append(",serialVersionUID=").append(serialVersionUID);sb.append("]");返回sb.toString();}}mapper层的AccountMapper接口定义:/***@authorAdministrator*@description针对表【account】的数据库操作Mapper*@createDate2022-08-1216:19:39*@Entitycom.wn.ssm080812.entity.Account*/publicinterfaceAccountMapper{intdeleteByPrimaryKey(Longid);intinsert(账户记录);intinsertSelective(账户记录);账户selectByPrimaryKey(Longid);账户登录(StringaccountName,StringaccountPwd);列表<账户>queryAll();intupdateByPrimaryKeySelective(账户记录);intupdateByPrimaryKey(Accountrecord);}service层的AccountService接口定义:publicinterfaceAccountService{intdeleteByPrimaryKey(Longid);intinsert(账户记录);intinsertSelective(账户记录);账户selectByPrimaryKey(Longid);账户登录(StringaccountName,StringaccountPwd);列表<账户>queryAll();intupdateByPrimaryKeySelective(账户记录);intupdateByPrimaryKey(Accountrecord);}service层的AccountServiceImpl类定义:@ServicepublicclassAccountServiceImplimplementsAccountService{@AutowiredprivateAccountMapperaccountMapper;@Override酒吧licintdeleteByPrimaryKey(Longid){返回accountMapper.deleteByPrimaryKey(id);}@Overridepublicintinsert(Accountrecord){returnaccountMapper.insert(record);}@OverridepublicintinsertSelective(账户记录){返回0;}@OverridepublicAccountselectByPrimaryKey(Longid){returnaccountMapper.selectByPrimaryKey(id);}@OverridepublicAccountlogin(StringaccountName,StringaccountPwd){returnaccountMapper.login(accountName,accountPwd);}@OverridepublicList
