【SSM

编程入门 行业动态 更新时间:2024-10-15 12:34:15

【<a href=https://www.elefans.com/category/jswz/34/1769255.html style=SSM"/>

【SSM

最近在学习用SSM框架搭建的服务器来对数据库进行一系列操作,SSM配置完以后就主要是对Service、Controller、ServiceImpl三个层进行代码实现。数据库的帐号表名是Account,三个属性分别是 account_id(主键), account_name, account_psd;用户表名是User,属性分别是 user_name(主键), user_gender, user_birthdey(前期建表手误,将错就错),user_tel,user_mail。

1.查:

根据Id查(Id是主键)

Sevice层定义一个方法:

public interface AccountService {Account getAccountById(Integer account_id);
}

Controller层实现:

    @RequestMapping("/account/id/{account_id}")@ResponseBodypublic Account getAccountById(@PathVariable Integer account_id) {Account account = accountService.getAccountById(account_id);return account;}

对应的Service实现层ServiceImpl:

        @Overridepublic Account getAccountById(Integer account_id) {Account account = accountMapper.selectByPrimaryKey(account_id);return account;}

根据Name查(不是主键)

Sevice层定义一个方法:

public interface AccountService {Account getAccountByName(String account_name);
}

Controller层实现:

        @RequestMapping("/account/name/{account_name}")@ResponseBodypublic Account getAccountByName(@PathVariable String account_name) {Account account = accountService.getAccountByName(account_name);return account;}

对应的Service实现层ServiceImpl:

        @Overridepublic Account getAccountByName(String account_name) {AccountExample example = new AccountExample();//添加查询条件Criteria criteria = example.createCriteria();criteria.andAccountNameEqualTo(account_name);//criteria.getAllCriteria();List<Account> a_list = accountMapper.selectByExample(example);if (a_list != null && a_list.size() > 0) {Account account = a_list.get(0);return account;}return null;}

查找全部

Sevice层定义一个方法:

public interface AccountService {List<Account> findAccountAll();
}

Controller层实现:

        @RequestMapping("/account")public String AccountList(Model model) {List<Account> alist = accountService.findAccountAll();//传递数据至前端model.addAttribute("alist", alist);//返回对应视图,"allaccount"是跳转到的jsp的名字return "allaccount";}

对应的Service实现层ServiceImpl:

        @Overridepublic List<Account> findAccountAll() {AccountExample example = new AccountExample();List<Account> alist = accountMapper.selectByExample(example);return alist;}

前端页面allaccount.jsp内容:

<%@ taglib prefix="c" uri="" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --><title>Bootstrap 101 Template</title><!-- Bootstrap --><link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css" rel="stylesheet"><!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 --><!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 --><!--[if lt IE 9]><script src="@3.7.3/dist/html5shiv.min.js"></script><script src=".js@1.4.2/dest/respond.min.js"></script><![endif]-->
</head>
<body>
<%--class="container-fluid"  style="background-color: #4cae4c"--%>
<div class="container"><table class="table table-bordered table-striped table-hover table-condensed"><thead><tr><th>帐号Id</th><th>帐号名</th><th>帐号密码</th></tr></thead><tbody><c:forEach var="account" items="${alist}"><tr><th>${account.accountId}</th><th>${account.accountName}</th><th>${account.accountPsd}</th></tr></c:forEach></tbody></table>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="${pageContext.request.contextPath}/static/js/jquery-3.2.1.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"></script>
</body>
</html>

 

更多推荐

【SSM

本文发布于:2024-03-10 09:36:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1727609.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SSM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!