大致的把ssm基础知识都过了一遍,那么就来做一个ssm整合的小项目吧,练练手!
因为是练整合的流程,所以业务就是基础的CRUD啦。
一个用户管理系统,管理员可以对用户账号进行增删改查(你没听错,就这么简单!!!!)
打开idea,左上角选择file–>new–>project,选择maven,然后点击next

这里设定自己的项目名和groupid,然后finish即可。

在pom.xml中导入我们需要用的依赖和maven的静态资源过滤:
junit junit 4.13.2 mysql mysql-connector-java 8.0.30 com.mchange c3p0 0.9.5.5 javax.servlet servlet-api 2.5 javax.servlet.jsp jsp-api 2.2 javax.servlet jstl 1.2 org.mybatis mybatis 3.5.11 org.mybatis mybatis-spring 3.0.0 org.springframework spring-webmvc 5.1.9.RELEASE org.springframework spring-jdbc 5.1.9.RELEASE org.projectlombok lombok 1.16.10 provided src/main/java **/*.properties **/*.xml false src/main/resources **/*.properties **/*.xml false
导入后,记得更新依赖包
点击项目文件夹,然后右键选择 add framework support,来添加web支持

在idea的右上角的位置:

点击 edit configurations然后配置tomcat

然后会弹出下面这样的界面,点击加号,找到local tomcat

这里我使用的是tomcat9,然后端口号和tomcat名可以自行修改

选择 deployment,添加artfacts

点击左上角 file ,然后选择project structure,弹出窗口后选择artifacts,然后找到自己当前项目的artifacts,在其根目录下新建一个lib文件夹,然后右键选择add of…library files 将项目中的依赖全部放入lib文件夹,然后ok即可


初始数据添加:

新建一个包 com/gothic/sunset/pojo 然后新建User.java实体类
package com.gothic.sunset.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor //有参构造
@NoArgsConstructor //无参构造
public class User {private int id; //id号private String userName;//用户名private String password;//密码
}
这里我使用了lombok,来自动注入,避免手写有参无参构造以及对应的getter和setter…
org.projectlombok lombok 1.16.10 provided
@Data:该注解定义在JavaBean上,给JavaBean产生getter(),setter(),无参构造器,tostring(),hashcode(),equals()
@NoArgsConstructor:产生无参构造器
@Getter:产生getter()
@Setter:产生setter()
@ToString:产生toString()
@RequiredArgsConstructor + @NonNull:可以用来定义有参构造器
@AllArgsConstructor:产生全属性的有参构造
在com/gothic/sunset/dao 下新建UserMapper.java 接口
package com.gothic.sunset.dao;import com.gothic.sunset.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;import java.util.List;public interface UserMapper {//添加用户public int addUser(User user);//删除用户 根据idpublic int delUser(@Param("id") int id);//更新用户信息 根据idpublic int updateUser(User user);//查询单个用户 根据id查public User findUserById(@Param("id") int id);//查询所有用户信息public List selectAllUser();//根据用户名查询用户public List findUserByName(@Param("userName") String userName);
}
在resources 目录下,新建 com/gothic/sunset/dao 目录,然后在这个包下新建UserMapper.xml映射文件
insert into(id,userName,password)values(#{userName},#{password}) delete from user where id = #{id} update userset userName = #{userName},password =#{password}where id =#{id}
在resources目录下,新建mybatis-config.xml文件
在resources 目录下,新建jdbc.properties文件,用于存放数据库连接用户名密码验证…
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
在resources目录下,新建spring-dao.xml配置文件
这里如果sqlSessionFactory注入数据库连接池,中写成了value=“dataSource” 这样的话,就会出现一个异常
org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'javax.sql.DataSource' for property 'dataSource'.
新建包 com/gothic/sunset/service 在这个包里新建UserService.java接口
package com.gothic.sunset.service;import com.gothic.sunset.pojo.User;import java.util.List;public interface UserService {//添加用户public int addUser(User user);//删除用户 根据idpublic int delUser( int id);//更新用户信息 根据idpublic int updateUser(User user);//查询单个用户 根据id查public User findUserById(int id);//查询所有用户信息public List selectAllUser();//根据用户名查询用户public List findUserByName( String userName);
}
与UserService.java接口在同一目录下,新建UserServiceImpl.java接口实现类
package com.gothic.sunset.service;import com.gothic.sunset.dao.UserMapper;
import com.gothic.sunset.pojo.User;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService{private UserMapper userMapper;public void setUserMapper(UserMapper userMapper) {this.userMapper = userMapper;}@Overridepublic int addUser(User user) {return userMapper.addUser(user);}@Overridepublic int delUser(int id) {return userMapper.delUser(id);}@Overridepublic int updateUser(User user) {return userMapper.updateUser(user);}@Overridepublic User findUserById(int id) {return userMapper.findUserById(id);}@Overridepublic List selectAllUser() {return userMapper.selectAllUser();}@Overridepublic List findUserByName(String userName) {return userMapper.findUserByName(userName);}
}
这里需要说明的是,可以使用自动装配bean结合注解@AutuWired的方式去做,我这里(刚回忆整合)就先用手动settter注入的方式。
然后这里的话,事务横切aop,这个小例子还没使用,so…就没有配置,也没有导入aspectj这个依赖包。
在 com/gothic/sunset/controller包下,新建UserController.java类
package com.gothic.sunset.controller;import com.gothic.sunset.pojo.User;
import com.gothic.sunset.service.UserService;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;import java.util.List;@Controller
@RequestMapping("/user")
public class UserController {// controller层调用service层,service层调用dao层 mvc架构,我就不多说了//@Qualifier注解 别名@Autowired@Qualifier("userServiceImpl")private UserService userService;@RequestMapping("/allUser")public String selectAllUser(Model model){//使用业务层调用dao层查询出数据,通过model对象渲染到前台页面List userList = userService.selectAllUser();model.addAttribute(userList);return "allUser";}//添加书籍,首先需要跳转到添加用户的表单页面@RequestMapping("/toAddUser")public String toAddUser(){//接收到前端请求后,跳到添加用户表单页面return "addUser";}//接收添加用户表单的数据,进行正式的添加用户,添加完成后,重定向到所有用户页面@RequestMapping("addUser")public String addUser(User user){userService.addUser(user);System.out.println(user.toString());return "redirect:/user/allUser";}//更新用户,与添加用户流程基本一样@RequestMapping("toUpdateUser")public String toUpdateUser(Model model,int id){User user = userService.findUserById(id);model.addAttribute("user",user);//跳转到用户修改页面,同时将要修改的用户的信息传递过去return "updateUser";}//正式更新用户@RequestMapping("updateUser")public String updateUser(User user){System.out.println(user.toString());userService.updateUser(user);System.out.println(user.getId());return "redirect:/user/allUser";}//删除就直接删除用户即可@RequestMapping("delUser")public String delUser(int id){userService.delUser(id);return "redirect:/user/allUser";}//查询用户 根据用户名查询@RequestMapping("/queryUser")public String queryUser(String userName,Model model){List userList = userService.findUserByName(userName);model.addAttribute(userList);return "allUser";}
}
将三个spring配置文件,都引入到一个新建的applicationContext.xml(Resources目录下新建)中。
DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext.xml 1 DispatcherServlet / encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encodingFilter /* 15
这里需要注意的是,前端控制器里面,最终绑定的是整合总的配置文件applicationContext.xml,别配错了,否则就会告诉你找不到某些bean,其实配错了也能根据提示来修改。
index.jsp页面
<%--Created by IntelliJ IDEA.User: gothiDate: 2022/12/3Time: 10:28To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
首页 点击进入
在/WEB-INF/jsp/目录下新建jsp页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--Created by IntelliJ IDEA.User: gothiDate: 2022/12/3Time: 15:25To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
查询所有用户
addUser.jsp
<%--Created by IntelliJ IDEA.User: gothiDate: 2022/12/3Time: 19:10To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
用户添加
用户添加
updateUser.jsp
<%--Created by IntelliJ IDEA.User: gothiDate: 2022/12/3Time: 19:14To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
用户更新
修改用户信息
首页:

用户列表页:

添加用户页面:

点击添加后:

这里是因为主键自增的,之前测试了一些数据,所以会显示id号为10.
修改用户页面:

修改测试:

根据用户名查询:


这里相同的用户名,会查出多条用户数据,因为我这里只有一条名为大漠孤烟的用户名的数据,所以只显示一条。
ssm果然配置真繁琐,不过如果一层一层来的话,也不算太复杂,多练习!经过大致10多天的时间,二刷结束,接下来就是做一些ssm的练手项目,熟悉一下流程了。
然后,因为时间匆忙写的略微有点简单,而且前台比较丑,等过段时间,做一个真正的ssm项目,并且做完以后准备学习springboot,和redis…最后再把前台的相关技术也认真学学!!!!