提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
这是在做课程设计的项目之前,学习如何使用SSM的一个练手项目,其中有框架搭建以及列表的增删改查和分页功能的实现,此下内容大多是个人学习感悟,如有不妥请多指正交流。
一、框架搭建
本人的SSM框架搭建是在b站学习的,框架搭建不需要自己编写,从其他地方将框架复制过来学会即可,知道框架中的各个位置所发挥的作用即可。
在此附上框架学习的视频,UP主讲的很清晰,还有相应的文件。
SSM框架中三个板块,Dao层,Service层以及Controller层。
Dao层是最底层,负责调用与修改数据库的数据;Service层是中间层,负责Dao层与Controller层之间的数据传输与连接,Controller层是最上层,负责处理网页传来的相关数据并返回数据给网页。
举个例子就是,饭店。
顾客(网页)来吃饭,先向服务员(Controller层)点餐(数据请求),服务员将顾客点的菜(数据请求)写到后厨窗口(Service层),后厨(Dao层)根据窗口要求处理食材(数据库数据)并放到窗口,服务员取菜给顾客。
这样想会不会好理解SSM的大致作用呢?
接下来说说具体的项目,一个学生信息列表,建议是在分别学习好Spring,SpringMVC以及MyBatis三大板块后学着做整合。
最后我的文件与框架创建如下图所示。
jsp页面文件如下图所示,后续配置文件会设置好jsp文件的前后缀
web.xml的作用是用来配置DispatcherServlet的,用于拦截网页url请求以及分发。
下面的filter用于解决编码乱码问题。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>springLearn</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!-->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!--注意这里不能配置成/*-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--encodingFilter-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
此框架是将applicationContext.xml中的内容分成三个部分进行编写,此文件就是将三个分文件进行整合。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>
Mybatis-config.xml文件用于配置别名和扫描dao层接口文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置别名 -->
<typeAliases>
<package name="com.pojo"/>
</typeAliases>
<!-- 配置扫描dao层接口 -->
<mappers>
<package name="com.dao"/>
</mappers>
</configuration>
spring-dao.xml用于SSM框架中最底层的相关文件配置,也就是与数据库连接那一层。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<!-- cj是mysql8.0的一个特性 -->
<property name="url" value="jdbc:mysql://localhost:3306/student?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- mybatis配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"/>
</bean>
</beans>
spring-mvc.xml是视图解析器与注解的配置,即配置jsp文件的前后缀以及pojo层实体类方法的别名扫描,便于写方法更加方便
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1.开启SpringMVC注解驱动 -->
<mvc:annotation-driven/>
<!-- 2.静态资源默认servlet配置-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<!-- /WEB-INF/jsp/+视图名+.jsp -->
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.controller"></context:component-scan>
</beans>
spring-service就是service方法的扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.service"></context:component-scan>
</beans>
二、具体方法的编写步骤
1.Controller层方法
@RequestMapping中填写的是网页的url,即在浏览器输入http://localhost:8080/StudentSSM/pagelistStudent,这里的StudentSSM是项目名称,后面的pagelistStudent就是@的内容
而要跳转的页面就是setViewName中的内容,这里面填的应该是jsp页面的名称(由于在配置文件中已经设置了前缀和后缀,此处直接填jsp页面名称即可)
package com.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.pojo.Page;
import com.pojo.Student;
import com.service.StudentService;
@Controller
public class Studentcontroller {
@Autowired
StudentService studentservice;
@RequestMapping("/listStudent")
public ModelAndView listStudent() {
ModelAndView mav = new ModelAndView();
List<Student> stduents=studentservice.list();
mav.addObject("students",stduents);
mav.setViewName("listStudent");
return mav;
}
@RequestMapping("/pagelistStudent")
public ModelAndView pagelistStudent(Page page) {
ModelAndView mav = new ModelAndView();
int total = studentservice.total();
//设置页码防溢出
if(page.getstart()<0)
page.setstart(0);
if(page.getstart()>total)
page.setstart(total-1);
page.calculatelast(total);
List<Student> pagestudents=studentservice.pagelist(page);
mav.addObject("pagestudents",pagestudents);
mav.setViewName("listStudent");
return mav;
}
@RequestMapping("/getStudent")
public ModelAndView getStudent(int sno) {
ModelAndView mav = new ModelAndView();
Student student = studentservice.get(sno);
List<Student> get = new ArrayList<Student>();
get.add(student);
mav.addObject("get",get);
mav.setViewName("getStudent");
return mav;
}
@RequestMapping("/deleteStudent")
public ModelAndView deleteStudent(int sno,Page page) {
ModelAndView mav = new ModelAndView();
studentservice.delete(sno);
int total = studentservice.total();
if(page.getstart()<0)
page.setstart(0);
if(page.getstart()>total)
page.setstart(total-1);
page.calculatelast(total);
List<Student> delete=studentservice.pagelist(page);
mav.addObject("delete",delete);
mav.setViewName("listStudent");
return mav;
}
@RequestMapping("/beforeinsertStudent")
public ModelAndView beforeinsertStudent() {
ModelAndView mav = new ModelAndView();
mav.setViewName("insertStudent");
return mav;
}
@RequestMapping("/insertStudent")
public ModelAndView insertStudent(int sno,String sname,Page page) {
ModelAndView mav = new ModelAndView();
studentservice.insert(sno,sname);
int total = studentservice.total();
if(page.getstart()<0)
page.setstart(0);
if(page.getstart()>total)
page.setstart(total-1);
page.calculatelast(total);
List<Student> insert=studentservice.pagelist(page);
mav.addObject("insert",insert);
mav.setViewName("listStudent");
return mav;
}
@RequestMapping("/beforeupdateStudent")
public ModelAndView beforeupdateStudent(int id,int sno,String sname) {
ModelAndView mav = new ModelAndView();
mav.addObject("id",id);
mav.addObject("sno",sno);
mav.addObject("sname",sname);
mav.setViewName("updateStudent");
return mav;
}
@RequestMapping("/updateStudent")
public ModelAndView updateStudent(int id,int sno,String sname) {
ModelAndView mav = new ModelAndView();
studentservice.update(id,sno,sname);
List<Student> update = studentservice.list();
mav.addObject("update",update);
mav.setViewName("listStudent");
return mav;
}
}
2.Service层方法
通过接口以实现Controller层调用方法与调用Dao层的方法
package com.service;
import java.util.List;
import com.pojo.Page;
import com.pojo.Student;
public interface StudentService {
List<Student> list();
List<Student> pagelist(Page page);
int total();
void delete(int sno);
Student get(int sno);
void insert(int sno,String sname);
void update(int id,int sno,String sname);
}
package com.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dao.StudentMapper;
import com.pojo.Page;
import com.pojo.Student;
import com.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService{
@Autowired
StudentMapper studentmapper;
@Override
public List<Student> list(){
return studentmapper.list();
}
@Override
public Student get(int sno) {
return studentmapper.get(sno);
}
@Override
public void delete(int sno) {
studentmapper.delete(sno);
}
@Override
public void insert(int sno,String sname) {
studentmapper.insert(sno,sname);
}
@Override
public void update(int id,int sno,String sname) {
studentmapper.update(id,sno,sname);
}
@Override
public List<Student> pagelist(Page page) {
// TODO Auto-generated method stub
return studentmapper.pagelist(page);
}
@Override
public int total() {
// TODO Auto-generated method stub
return studentmapper.total();
}
}
3.Dao层方法
Dao层就调用底层方法
package com.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.pojo.Page;
import com.pojo.Student;
public interface StudentMapper {
public List<Student> list();
public List<Student> pagelist(Page page);
public int total();
public void delete(int sno);
public Student get(int sno);
public void insert(@Param("sno")int sno,@Param("sname")String sname);
public void update(@Param("id")int id,@Param("sno")int sno,@Param("sname")String sname);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:命名空间,填写对应接口的全限定类名 -->
<mapper namespace="com.dao.StudentMapper">
<!-- id:list 对应接口的方法-->
<select id="list" resultType="Student">
select * from student
</select>
<select id="pagelist" resultType="Student">
select * from student
<if test="start!=null and count!=null">
limit #{start},#{count}
</if>
</select>
<select id="total" resultType="int">
select count(*) from student
</select>
<select id="get" resultType="Student" parameterType="int">
select * from student where Sno = #{sno}
</select>
<delete id="delete" parameterType="Student">
delete from student where Sno = #{sno}
</delete>
<insert id="insert" parameterType="Student">
insert into student(Sno,Sname) value(#{sno},"${sname}")
</insert>
<update id="update" parameterType="Student">
update student set Sno=#{sno},Sname=#{sname} where id=#{id}
</update>
</mapper>
三、具体网页显示
最后会显示这样的一个表格,其中名字学号都与实际无关,只是用作表单内数据。
总结
总的来说,将框架搭建好并且理解相关方法的编写后,会让工作变得极其简单,付出时间理解就好了。