1.Spring
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。
2.SpringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
3.mybatis
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
页面发送请求给控制器,控制器调用业务层处理逻辑,逻辑层向持久层发送请求,持久层与数据库交互,后将结果返回给业务层,业务层将处理逻辑发送给控制器,控制器再调用视图展现数据
4.执行流程
5.功能列表
新增
删除
修改
查询
分页
6.案例(用户管理)
DAO(Data Access Object) 数据访问源
@Repository
public interface UserDao {
/**
* 根据id查询学生信息
*/
@Select("select * from user where id = #{id} and id is not null")
public User findOne(Integer id);
@Select("select * from user limit 2")
public List<User> findAll();
@Insert("insert into user values(null,#{username},#{phone},#{password},#{email})")
public int insertOne(User user);
@Delete("delete from user where id = #{id}")
public int deleteOne(Integer id);
@Update("update user set username=#{username},phone=#{phone},password=#{password},email=#{email} where id = #{id}")
public int updateOne(User user);
@Select("select count(*) from user")
public int totalCount();
@Select("select * from user limit #{begin},#{size}")
public List<User> findAllByPagination(@Param("begin") int begin, @Param("size") int size);
@Select("<script>select * from user\n" +
" <where>\n" +
"<if test=\"user != null\">\n"+
" <if test=\"user.username != null and user.username != ''\">\n" +
" username like #{user.username}\n" +
" </if>\n" +
" <if test=\"user.phone != null and user.phone != ''\">\n" +
" phone = #{user.phone}\n" +
" </if>\n" +
" <if test=\"user.email != null and user.email != ''\">\n" +
" email = #{user.email}\n" +
" </if>\n" +
"</if>\n"+
"</where>\n" +
"limit #{begin},#{size}</script>")
public List<User> findByCondition(@Param("user") User user, @Param("begin") int begin, @Param("size") int size);
}
Service业务逻辑层
接口
public interface UserService {
public User findOne(Integer id);
public boolean insertOne(User user);
public boolean deleteOne(Integer id);
public boolean updateOne(User user);
public List<User> findAll();
public int totalCount();
public PageBean<User> findAllByPagination(int currentPage, int pageSize);
public PageBean<User> findByCondition(User user, int currentPage, int pageSize);
}
实现
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User findOne(Integer id) {
return userDao.findOne(id);
}
@Transactional(rollbackFor = {Exception.class})
@Override
public boolean insertOne(User user) {
return userDao.insertOne(user) > 0;
}
@Transactional(rollbackFor = {Exception.class})
@Override
public boolean deleteOne(Integer id) {
return userDao.deleteOne(id) > 0;
}
@Transactional(rollbackFor = {Exception.class})
@Override
public boolean updateOne(User user) {
return userDao.updateOne(user) > 0;
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public int totalCount() {
return userDao.totalCount();
}
@Override
public PageBean<User> findAllByPagination(int currentPage, int pageSize) {
int begin = (currentPage - 1) * pageSize;
List<User> users = userDao.findAllByPagination(begin, pageSize);
int totalCount = this.totalCount();
return new PageBean<>(users, totalCount);
}
@Override
public PageBean<User> findByCondition(User user, int begin, int size) {
if (!Objects.isNull(user)) {
if (user.getUsername() != null && !(" ").equals(user.getUsername()) && user.getUsername().length() > 0){
user.setUsername("%"+user.getUsername()+"%");
}
}
List<User> list = userDao.findByCondition(user, begin, size);
int totalCount = this.totalCount();
return new PageBean<>(list,totalCount);
}
}
Controller(视图控制层)
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public String list(HttpSession session) {
List<User> users = userService.findAll();
session.setAttribute("users", users);
int recordsCount = userService.totalCount();
session.setAttribute("count", recordsCount);
session.setAttribute("currentPage", 1);
return "userlist";
}
@GetMapping("/addUser")
public String showAdd() {
return "addUser";
}
@PostMapping("/addUser")
public String showList(User user, HttpSession session) {
userService.insertOne(user);
return list(session);
}
@GetMapping("/delUser")
public String showList2(User user, HttpSession session) {
userService.deleteOne(user.getId());
return list(session);
}
@GetMapping("/echo")
public String echoUser(Integer id, Model model) {
User user = userService.findOne(id);
model.addAttribute("user", user);
return "updUser";
}
@PostMapping("/updUser")
public String updUser(User user, HttpSession session) {
userService.updateOne(user);
return list(session);
}
@GetMapping("/pagination")
public String queryAllByPagination(@RequestParam("begin") int currentPage,
@RequestParam("size") int pageSize,
HttpSession session) {
PageBean<User> userPageBean = userService.findAllByPagination(currentPage, pageSize);
setProps(userPageBean, session, currentPage);
return "userlist";
}
@GetMapping("/condition")
public String findByCondition(@RequestParam(value = "user",required = false) User user,
@RequestParam(value = "begin", defaultValue = "1") int currentPage,
@RequestParam(value = "size", defaultValue = "2") int pageSize,
HttpSession session) {
if (user != null) {
session.setAttribute("u", user.getUsername());
session.setAttribute("p", user.getPhone());
session.setAttribute("e", user.getEmail());
}
PageBean<User> userPageBean = userService.findByCondition(user, currentPage, pageSize);
setProps(userPageBean, session, currentPage);
return "userlist";
}
private void setProps(PageBean<User> userPageBean, HttpSession session, int currentPage) {
List<User> users = userPageBean.getRows();
int totalCount = userPageBean.getTotalCount();
session.setAttribute("users", users);
session.setAttribute("count", totalCount);
session.setAttribute("currentPage", currentPage);
}
}
Jsp页面展示
用户列表
<%@ page import="com.axu.pojo.User" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户列表</title>
<style>
form{
margin-left: 50%;
transform: translate(-35%);
}
#footer{
display: flex;
justify-content: space-around;
}
</style>
</head>
<body>
<h1 align="center">用户列表信息</h1>
<form action="/condition" method="get">
<input type="text" name="username" placeholder="请输入您要查询的用户名">
<input type="text" name="phone" placeholder="请输入您要查询的用户的手机号">
<input type="text" name="email" placeholder="请输入您要查询的用户的邮箱">
<input type="submit" value="查询">
</form>
<button style="margin-left: 95%"><a href="/addUser">新增</a></button>
<table align="center" border="1px solid #000" cellpadding="3px" cellspacing="0">
<tr align="center">
<th width="150px">id</th>
<th>用户名</th>
<th>手机号</th>
<th>密码</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<%
List<User> users = (List<User>) session.getAttribute("users");
for (User user : users) {
%>
<tr align="center">
<td><%=user.getId()%>
</td>
<td><%=user.getUsername()%>
</td>
<td><%=user.getPhone()%>
</td>
<td><%=user.getPassword()%>
</td>
<td><%=user.getEmail()%>
</td>
<td>
<button><a href=/echo?id=<%=user.getId()%>>编辑</a></button> <button><a
href=/delUser?id=<%=user.getId()%>>删除</a></button>
</td>
</tr>
<%
}
%>
</table>
<div style="padding-left: 62%;color: orange">总计:<%=session.getAttribute("count")%>条数据</div>
<div id="footer">
<div>
页码:
<button><a
href="/condition?username=<%=session.getAttribute("u")%>&phone=<%=session.getAttribute("p")%>&email
=<%=session.getAttribute("e")%>&begin=1&size=2">1
</a></button>
<button><a
href="/condition?username=<%=session.getAttribute("u")%>&phone=<%=session.getAttribute("p")%>&email
=<%=session.getAttribute("e")%>&begin=2&size=2">2
</a></button>
<button><a
href="/condition?username=<%=session.getAttribute("u")%>&phone=<%=session.getAttribute("p")%>&email
=<%=session.getAttribute("e")%>&begin=3&size=2">3
</a></button>
<button><a
href="/condition?username=<%=session.getAttribute("u")%>&phone=<%=session.getAttribute("p")%>&email
=<%=session.getAttribute("e")%>&begin=4&size=2">4
</a></button>
</div>
<div>默认显示<font color="orange">2</font>条数据</div>
</div>
<div style="margin-left: 19%">第 <font color="red"><%=session.getAttribute("currentPage")%></font> 页</div>
</body>
</html>
新增用户信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加用户</title>
</head>
<body>
<h1>添加用户信息</h1>
<form action="/addUser" method="post">
<input type="text" name="username" placeholder="请输入用户名"><br>
<input type="text" name="phone" placeholder="请输入手机号"><br>
<input type="password" name="password" placeholder="请输入密码"><br>
<input type="text" name="email" placeholder="请输入邮箱"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
更新用户信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户修改</title>
</head>
<body>
<h1>修改用户信息</h1>
<form action="/updUser" method="post">
<input type="hidden" name="id" value="${user.id}"><br>
<input type="text" name="username" placeholder="请输入用户名" value="${user.username}"><br>
<input type="text" name="phone" placeholder="请输入手机号" value="${user.phone}"><br>
<input type="password" name="password" placeholder="请输入密码" value="${user.password}"><br>
<input type="text" name="email" placeholder="请输入邮箱" value="${user.email}"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
7.思维导图
8.分析
分层思想很重要,以上的代码耦合性还是很重,分层让代码变得简洁、见名知意、易维护,降低了维护成本,为什么要降低代码的耦合性?首先耦合性高的情况下,代码不易维护,牵一发而动全身,动一处,可能就会导致整个项目瘫痪,大大提高了时间成本,SSM框架的分层思想,大大降低了代码的耦合性, 方便Code View以及Code refactoring(重构)!!!