Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
页面发送请求给控制器,控制器调用业务层处理逻辑,逻辑层向持久层发送请求,持久层与数据库交互,后将结果返回给业务层,业务层将处理逻辑发送给控制器,控制器再调用视图展现数据
@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 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 findAllByPagination(@Param("begin") int begin, @Param("size") int size);
@Select("")
public List findByCondition(@Param("user") User user, @Param("begin") int begin, @Param("size") int size);
}
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 findAll();
public int totalCount();
public PageBean findAllByPagination(int currentPage, int pageSize);
public PageBean 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 findAll() {
return userDao.findAll();
}
@Override
public int totalCount() {
return userDao.totalCount();
}
@Override
public PageBean findAllByPagination(int currentPage, int pageSize) {
int begin = (currentPage - 1) * pageSize;
List users = userDao.findAllByPagination(begin, pageSize);
int totalCount = this.totalCount();
return new PageBean<>(users, totalCount);
}
@Override
public PageBean 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 list = userDao.findByCondition(user, begin, size);
int totalCount = this.totalCount();
return new PageBean<>(list,totalCount);
}
}
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public String list(HttpSession session) {
List 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 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 userPageBean = userService.findByCondition(user, currentPage, pageSize);
setProps(userPageBean, session, currentPage);
return "userlist";
}
private void setProps(PageBean userPageBean, HttpSession session, int currentPage) {
List users = userPageBean.getRows();
int totalCount = userPageBean.getTotalCount();
session.setAttribute("users", users);
session.setAttribute("count", totalCount);
session.setAttribute("currentPage", currentPage);
}
}
<%@ page import="com.axu.pojo.User" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
用户列表
用户列表信息
id
用户名
手机号
密码
邮箱
操作
<%
List users = (List) session.getAttribute("users");
for (User user : users) {
%>
<%=user.getId()%>
<%=user.getUsername()%>
<%=user.getPhone()%>
<%=user.getPassword()%>
<%=user.getEmail()%>
<%
}
%>
总计:<%=session.getAttribute("count")%>条数据
第 <%=session.getAttribute("currentPage")%> 页
新增用户信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加用户
添加用户信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
用户修改
修改用户信息
分层思想很重要,以上的代码耦合性还是很重,分层让代码变得简洁、见名知意、易维护,降低了维护成本,为什么要降低代码的耦合性?首先耦合性高的情况下,代码不易维护,牵一发而动全身,动一处,可能就会导致整个项目瘫痪,大大提高了时间成本,SSM框架的分层思想,大大降低了代码的耦合性, 方便Code View以及Code refactoring(重构)!!!