SSM整合示例 墨蓝 2023-10-06 07:25 13阅读 0赞 #### 没有标题 #### ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIxMDQwNTU5_size_16_color_FFFFFF_t_70] controller: package org.cduck.controller; import java.util.Map; import org.cduck.entity.Student; import org.cduck.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("stu") @Controller public class StuController { @Autowired StudentService stuService; @RequestMapping("query/{stuno}") public String queryStu(@PathVariable("stuno") int stuno,Map map) { Student student=stuService.queryStu(stuno); map.put("student", student); return "result"; } @RequestMapping(value = "add") public String addStudent(Student student) { stuService.addStudent(student); return "result"; } } student: package org.cduck.controller; import java.util.Map; import org.cduck.entity.Student; import org.cduck.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("stu") @Controller public class StuController { @Autowired StudentService stuService; @RequestMapping("query/{stuno}") public String queryStu(@PathVariable("stuno") int stuno,Map map) { Student student=stuService.queryStu(stuno); map.put("student", student); return "result"; } @RequestMapping(value = "add") public String addStudent(Student student) { stuService.addStudent(student); return "result"; } } mapper.java: package org.cduck.mapper; import org.cduck.entity.Student; public interface StudentMapper { public void addStudent(Student student); Student queryStu(int stuno); } mapper.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"> 该段的作用是可以给xml开启提示功能,但是需要联网,如果网络差 则无法实现提示功能 可以http://mybatis.org/dtd/mybatis-3-mapper.dtd下载下来,然后去XML里面添加就可实现提示功能 --> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 映射文件 --> <mapper namespace="org.cduck.mapper.StudentMapper"> <insert id="addStudent" parameterType="org.cduck.entity.Student"> insert into student(stuNo,stuName,stuAge) values(#{stuNo},#{stuName},#{stuAge}) </insert> <select id="queryStu" resultType="org.cduck.entity.Student" parameterType="int"> select * from student </select> </mapper> Service: 接口: package org.cduck.service; import org.cduck.entity.Student; import org.springframework.stereotype.Service; public interface StudentService { public void addStudent(Student student); Student queryStu(int stuno); } 实现类: package org.cduck.service; import org.cduck.entity.Student; import org.cduck.mapper.StudentMapper; import org.springframework.beans.factory.annotation.Autowired; public class StudentServiceImpl implements StudentService { //service依赖于Dao StudentMapper studentmapper; public void setStudentmapper(StudentMapper studentmapper) { this.studentmapper = studentmapper; } @Override public Student queryStu(int stuno) { Student student=studentmapper.queryStu(stuno); return student; } @Override public void addStudent(Student student) { studentmapper.addStudent(student); } } spring配置文件: 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" 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-4.3.xsd"> <!-- 配置数据源、mapper.xml --> <!-- 加载db.properties文件 --> <bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations"> <array> <value>classpath:db.properties</value> </array> </property> </bean> <!-- 配置数据库信息, --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="${url}"></property> <property name="driverClassName" value="${driver}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> </bean> <!-- mapper.xml --> <!-- 配置mybatis核心类SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 加载Mapper.xml文件 --> <property name="mapperLocations" value="classpath:org/cduck/mapper/*.xml"></property> </bean> <!-- Spring整合mybatis --> <bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 将mybatis的sqlSessionFactory交给Spring去管理 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <!--name="basePackage"的属性的作用:将org.cduck.mapper包中所有的接口 产生与之对应的动态代理对象(对象名就是首字母小写的接口名)。以便于操作mybatis --> <property name="basePackage" value="org.cduck.mapper"></property> </bean> <!-- =============================分割线============================= --> <bean id="studentService" class="org.cduck.service.StudentServiceImpl"> <property name="studentmapper" ref="studentMapper"></property> <!-- 注意:!! 这里的studentMapper在上面的<property name="basePackage" value="org.cduck.mapper"></property> 已经将其放入了IOC容器,所以直接注入就行。 但是studentmapper一直报错:因为这种方法是SET注入法,我的studentmapper没有写set方法所以报错 --> </bean> </beans> springMVC配置: 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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 配置视图解析器 --> <bean id="id" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- springMVC 基础配置、标配 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 将控制器所在包纳入IOC容器 --> <context:component-scan base-package="org.cduck.controller"></context:component-scan> </beans> 前端: result.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> ${requestScope.student.stuNo}<br> ${requestScope.student.stuName}<br> ${requestScope.student.stuAge}<br> </body> </html> index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <a href="stu/query/1">查询一号学生</a> <form action="stu/add" method="post"> <input name="stuNo" > <input name="stuName" > <input name="stuAge" > <input type="submit" value="Add"> </form> </body> </html> -------------------- web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SSMProject</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> <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> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <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>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIxMDQwNTU5_size_16_color_FFFFFF_t_70]: https://img-blog.csdnimg.cn/20200503134537434.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIxMDQwNTU5,size_16,color_FFFFFF,t_70
相关 SSM整合示例 没有标题 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nL 墨蓝/ 2023年10月06日 07:25/ 0 赞/ 14 阅读
相关 整合 SSM 1.1 相关依赖 <dependency> <groupId>org.springframework</groupId> <art 不念不忘少年蓝@/ 2022年12月05日 05:21/ 0 赞/ 33 阅读
相关 ssm整合 整合:spring4.2.5+mybatis3.2.8+springMVC+maven 环境:Myeclipse2014+mysql5.5.20+tomcat8+jdk1. 柔光的暖阳◎/ 2022年07月13日 11:19/ 0 赞/ 155 阅读
相关 ssm整合 手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis -------------------- > 我们看招聘信息的时候,经常 古城微笑少年丶/ 2022年07月12日 06:27/ 0 赞/ 44 阅读
相关 ssm整合 手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis -------------------- > 我们看招聘信息的时候,经常 墨蓝/ 2022年07月12日 06:27/ 0 赞/ 73 阅读
相关 ssm整合 在mybatis和spring整合后 , 在把springmvc整合进来 在maven里创建web工程 然后进行mybatis和spring的整合步骤(写在其他博客里) 超、凢脫俗/ 2022年02月12日 10:10/ 0 赞/ 445 阅读
相关 ssm整合 一、注入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mav 快来打我*/ 2022年01月23日 12:57/ 0 赞/ 454 阅读
相关 SSM--SSM整合 一、项目层级结构以及所需JAR包: ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9 傷城~/ 2022年01月21日 23:37/ 0 赞/ 486 阅读
相关 ssm整合 ssm整合 项目目录 ![1560559-20190805161522126-1893405258.png][] jar ![1560559-2019080 桃扇骨/ 2021年10月24日 02:56/ 0 赞/ 499 阅读
相关 SSM整合 前提:jdk,maven,tomcat,STS都已经安装配置好了 spring-4.3.7 + mybatis-3.3.0 + maven3.5+jdk8(这个组合会减少很多 悠悠/ 2021年09月28日 07:58/ 0 赞/ 463 阅读
还没有评论,来说两句吧...