MVC,即Model,View,Controller。
MVC可以理解为一种代码组织方式,它可以为代码的编写降低一些成本。
下面通过一个项目的代码来体验一下MVC:
项目简介:在数据库中有一张表account,存放着用户的账号、密码、用户名、余额。本项目要实现对account表的增删查改。
account表的定义:
create table account(
id int primary key auto_increment,
account_name varchar(30),
account_pwd varchar(30),
account_no varchar(30),
account_balance int
);
entity层Account类定义:
/**
*
* @TableName account
*/
@Data
public class Account implements Serializable {
//
private Integer id;
private String accountName;
//
private String accountPwd;
//
private String accountNo;
//
private Integer accountBalance;
private static final long serialVersionUID = 1L;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Account other = (Account) that;
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()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getAccountName() == null) ? 0 : getAccountName().hashCode());
result = prime * result + ((getAccountPwd() == null) ? 0 : getAccountPwd().hashCode());
result = prime * result + ((getAccountNo() == null) ? 0 : getAccountNo().hashCode());
result = prime * result + ((getAccountBalance() == null) ? 0 : getAccountBalance().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
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("]");
return sb.toString();
}
}
mapper层的AccountMapper接口定义:
/**
* @author Administrator
* @description 针对表【account】的数据库操作Mapper
* @createDate 2022-08-12 16:19:39
* @Entity com.wn.ssm080812.entity.Account
*/
public interface AccountMapper {
int deleteByPrimaryKey(Long id);
int insert(Account record);
int insertSelective(Account record);
Account selectByPrimaryKey(Long id);
Account login(String accountName,String accountPwd);
List<Account> queryAll();
int updateByPrimaryKeySelective(Account record);
int updateByPrimaryKey(Account record);
}
service层的AccountService接口定义:
public interface AccountService {
int deleteByPrimaryKey(Long id);
int insert(Account record);
int insertSelective(Account record);
Account selectByPrimaryKey(Long id);
Account login(String accountName,String accountPwd);
List<Account> queryAll();
int updateByPrimaryKeySelective(Account record);
int updateByPrimaryKey(Account record);
}
service层的AccountServiceImpl类定义:
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public int deleteByPrimaryKey(Long id) {
return accountMapper.deleteByPrimaryKey(id);
}
@Override
public int insert(Account record) {
return accountMapper.insert(record);
}
@Override
public int insertSelective(Account record) {
return 0;
}
@Override
public Account selectByPrimaryKey(Long id) {
return accountMapper.selectByPrimaryKey(id);
}
@Override
public Account login(String accountName, String accountPwd) {
return accountMapper.login(accountName,accountPwd);
}
@Override
public List<Account> queryAll() {
return accountMapper.queryAll();
}
@Override
public int updateByPrimaryKeySelective(Account record) {
return 0;
}
@Override
public int updateByPrimaryKey(Account record) {
return accountMapper.updateByPrimaryKey(record);
}
}
Controller层的AccountController定义:
@RestController
@RequestMapping("account")
public class AccountController {
@Autowired
private AccountService accountService;
@GetMapping(value = "loginDo")
@ResponseBody
public Result login(String accountName, String accountPwd, HttpSession session){
Account account = accountService.login(accountName, accountPwd);
if(account!=null){
session.setAttribute("account",account);
return Result.ok();
}else {
return Result.error("用户名或密码错误");
}
}
@GetMapping(value = "queryAll")
@ResponseBody
public Result queryAll(){
List<Account> list = accountService.queryAll();
return Result.ok().data("list",list);
}
@GetMapping(value = "queryById")
@ResponseBody
public Result queryById(Long id){
Account account = accountService.selectByPrimaryKey(id);
return Result.ok().data("account",account);
}
@PostMapping(value = "/addDo")
@ResponseBody
public Object add(Account account){
int result = accountService.insert(account);
HashMap<String, Integer> map = new HashMap<>();
map.put("code",result>0 ? 200 : 500);
return map;
}
@GetMapping(value = "delete")
@ResponseBody
public Object delete(Long id){
int result = accountService.deleteByPrimaryKey(id);
HashMap<String, Integer> map = new HashMap<>();
map.put("code",result>0 ? 200 : 500);
return map;
}
@PostMapping(value = "update")
@ResponseBody
public void update(Account account){
int result = accountService.updateByPrimaryKey(account);
HashMap<String, Integer> map = new HashMap<>();
map.put("code",result>0 ? 200 : 500);
}
}
Controller层的IndexController定义:
@Controller
public class IndexController {
@RequestMapping("account")
public String toAccount(){
return "account/account";
}
@RequestMapping("login")
public String toLogin(){
return "/WEB-INF/static/login.html";
}
@RequestMapping("admin")
public String toAdmin(){
return "/WEB-INF/static/admin.html";
}
}
MVC中的M对应项目的service层、mapper层,V对应项目的webapp.WEB-INF.templates(存放页面),C对应项目的controller层。
1.页面用于展示数据。
2.controller层用于接收不同的HTTP请求并将数据返回给网页。
3.service层用于接收controller层的调用,实现具体业务,并将数据返回给controller层。
4.mapper层用于接收service层的调用,实现对数据库中account表的增删查改,并将数据返回给service层。
MVC使用面向接口编程的风格,这样使得各层实现类的替换比较简单,使用实现类的上层类中几乎无需变更,特别是在使用了IOC(Inversion Of Control)容器的时候。
因为采用了分层管理代码,在开发时,如果应用报错,可以将问题锁定在某一层。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。