SSM搭建

xiaoxiao2021-02-28  111

        暑假回到家闲了好几天,感冒了也好几天,刚把家里的电脑重装了系统,接下来,我写一个SSM框架的搭建 ,算是对之前知识的复习吧,下一篇,会写利用Maven来的搭问题建SSM框架(其实差不多,但是配置方面会着重讲),其实网上的教程也不少,我也遇到过很多问题,而且有的运行不成功,那时候都快疯了,所以我会把配置遇到的问题也写出来。

这个项目可以查看数据库信息 (mybatis没有用到逆向工程的,接下来的博客会有关于逆向工程的东西,因为暑假学习逆向工程,觉得逆向工程很方便,但是这篇就不讲了) 

文章最后,会有这个SSM项目链接(github地址)  代码里有很多注释,相信你们会明白的,有疑问,一起交流哟。

这个项目效果

第一步:搭建环境   (我用的是IDEA)

1.jar包(SSM所需jar包,所需jar包多,可以下载我的项目,里面有jar包,或者上网搜SSM所需jar包)

 在IDEA里导入jar包,相信你们都学到SSM搭建了,肯定会了

2.数据库搭建  

create table category( id int(20) not null auto_increment primary key, name varchar(20), age int(10), password varchar(20) )

数据可以自己插入

 

3. 项目目录

 

第二步  配置文件

 

1.web.xml 的书写

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- spring的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring mvc核心:分发servlet --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- spring mvc的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

2.springMVC.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:annotation-config/> <context:component-scan base-package="cn.lh.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。 并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。--> <mvc:annotation-driven /> <!-- servlet在找页面时,走的是dispatcherServlet路线。找不到的时候会报404 加上这个默认的servlet时候,servlet在找不到的时候会去找静态的内容。 --> <mvc:default-servlet-handler /> <!--<!– 视图定位 –> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>--> </beans>

3.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 向Spring容器注册以下四个BeanPostProcessor: 是为了让系统能够识别相应的注解。--> <context:annotation-config /> <!-- 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,如果扫描到有@Component @Controller@Service等这些注解的类, 则把这些类注册为bean--> <context:component-scan base-package="com.llh.service"/> <!--外在化参数配置的方式,不过该标签在spring配置文件中只能存在一份!!--> <context:property-placeholder location="classpath:db.properties"/> <!--//配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${jdbc.driver}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="typeAliasesPackage" value="cn.lh.pojo" /> <property name="dataSource" ref="dataSource"/><!--指向上面配置的数据库--> <property name="mapperLocations" value="classpath:com/llh/mapper/*.xml"/> <!--pagehelper插件,分页的时候会用到--> <!--<property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <!– 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 –> <property name="properties"> <value> helperDialect=mysql <!–reasonable=true–> <!–supportMethodsArguments=true–> <!–params=count=countSql–> <!–autoRuntimeDialect=true–> </value> </property> </bean> </array> </property>--> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.llh.mapper"/> </bean> </beans>

4.db.properties(在applicationContext.xml中配置)

 

 

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/blog jdbc.username=root jdbc.password=root

第三步  pojo类(就不写了)

 

第四步 mapper接口与mapper.xml

mapper 接口

 

package com.llh.mapper; import com.llh.pojo.Category; import java.util.List; /** * Created by LENOVO on 2017/9/1. */ public interface CategoryMapper { public List<Category> select(); }

mappee.xml 

<?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必须写CategoryMapper的完整类名--> <mapper namespace="com.llh.mapper.CategoryMapper"> <select id="select" resultType="com.llh.pojo.Category"> select * from category </select> </mapper>

第五步 service 虽然这个项目小,但是还是写了service层

 

 

package com.llh.service; import com.llh.pojo.Category; import java.util.List; /** * Created by LENOVO on 2017/9/1. */ public interface CategoryService { public List<Category> select(); }

接口实现类

 

 

package com.llh.service.impl; import com.llh.mapper.CategoryMapper; import com.llh.pojo.Category; import com.llh.service.CategoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Created by LENOVO on 2017/9/1. */ @Service("categoryService") public class CategoryServiceImpl implements CategoryService { @Autowired CategoryMapper categoryMapper; @Override public List<Category> select() { return categoryMapper.select(); } }

第六步  controller 层

package com.llh.controller; import com.llh.pojo.Category; import com.llh.service.CategoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.List; /** * Created by LENOVO on 2017/9/1. */ @Controller @RequestMapping("/category") public class CategoryController { @Autowired CategoryService categoryService; @RequestMapping("/select") public ModelAndView select(){ List<Category> inform = categoryService.select(); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("inform",inform); modelAndView.setViewName("/WEB-INF/jsp/category.jsp"); return modelAndView; } }

第七步 前端页面 

 

index.jsp

 

<%-- Created by IntelliJ IDEA. User: LENOVO Date: 2017/9/1 Time: 16:50 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <% response.sendRedirect("/category/select");%> </body> </html>

category.xml

 

 

<%-- Created by IntelliJ IDEA. User: LENOVO Date: 2017/9/1 Time: 18:34 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>信息查看</title> </head> <body> <table align='center' border='1' cellspacing='0'> <tr> <td>ID</td> <td>Name</td> <td>Age</td><td>Password</td> <td>修改</td> <td>删除</td> </tr> <c:forEach items="${inform}" var="student"> <tr> <td>${student.id}</td> <td>${student.name}</td> <td>${student.age}</td> <td>${student.password}</td> <td><a href="change.jsp?id=${c.id}">修改</a></td> <td><a href="delete.jsp?id=${c.id}">删除</a></td> </tr> </c:forEach> </table> </body> </html>

 

项目地址   https://github.com/boheningmeng/170901_SSM_blog

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-85545.html

最新回复(0)