欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ssm框架整合演示

程序员文章站 2022-04-15 18:56:35
ssm框架开发整合演示ssm框架是指spring+springMVC+MyBatis 整合开发开发,这里我将使用mysql做一个简单查询学生信息并展示到jsp页面上的演示springSpring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。springMVCM 代表 模型(Model)模型是什么呢? 模型就是数据,就是 dao,beanV 代表 视图(View)视图是什么呢? 就是网页, JSP,用来展示模型中的数据C 代表 控制器(controller)控制器是什么?...

ssm框架开发整合演示

ssm框架是指spring+springMVC+MyBatis 整合开发开发,这里我将使用mysql做一个简单查询学生信息并展示到jsp页面上的演示

spring

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

springMVC

M 代表 模型(Model)
模型是什么呢? 模型就是数据,就是 dao,bean
V 代表 视图(View)
视图是什么呢? 就是网页, JSP,用来展示模型中的数据
C 代表 控制器(controller)
控制器是什么? 控制器的作用就是把不同的数据(Model),显示在不同的视图(View)上,Servlet 扮演的就是这样的角色。

MyBatis

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。

这里只对其做了简单描述,如想知道详情可自行百度

1.导入所需要的jar包

ssm框架整合演示

2.在mysql数据库中建student表分别插入数据

ssm框架整合演示

3.创建一个实体类Student

package com.liu.entity;

public class Student {
	private long stuId;
	private String stuName;
	private int stuAge;
	public long getStuId() {
		return stuId;
	}
	public void setStuId(long stuId) {
		this.stuId = stuId;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public int getStuAge() {
		return stuAge;
	}
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}
	@Override
	public String toString() {
		return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuAge=" + stuAge + "]";
	}
	
}

注意:这里的属性名和类型要与数据库中的一致。

4.在dao层建查询的接口

package com.liu.dao;

import java.util.List;

import com.liu.entity.Student;

public interface StudentDao {
	public List<Student> findAll();
}

5.在mapper包下新建studentMapper.xml,用于对mysql的操作

<?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">
 <!-- 指向接口 -->
 <mapper namespace="com.liu.dao.StudentDao">
 	
 	<select id="findAll" resultMap="getUserMap">
 		select * from student;
 	</select>
 	
 	<!-- 配置resulMap -->
 	<resultMap type="Student" id="getUserMap">
 		<id column="stuId" property="stuId"/>
 		<result column="stuName" property="stuName"/>
 		<result column="stuAge" property="stuAge"/>
 	</resultMap>
 </mapper>

配置的resultMap要与实体类属性名和数据库的字段名相同

6.在src下建spring.xml创建mybatis的sqlsessionFactory对象

<?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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        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/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd">
     
     <!-- 引入db.properties -->    
     <context:property-placeholder location="classpath:db.properties"/>   
	

<!-- 数据库连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${driver}"></property>
		<property name="jdbcUrl" value="${url}"></property>
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property>
	</bean>
	
	<!-- 创建mybatis的sqlsessionFactory对象 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="mapperLocations" value="classpath:com/liu/mapper/*.xml"></property>
		<!-- 配置取简单类名路径 -->
		<property name="typeAliasesPackage" value="com.liu.entity"></property>
	</bean>
	
	<!-- 配置mapperScannerConfigurer -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.liu.dao"></property>
		<!-- 将com.liu.dao包中所有接口产生与之对应的动态代理对象(对象名就是首字母小写的接口名) -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	
</beans>

7.编写db.properties设置连接mysql

ssm框架整合演示

8.在web.xml中引入spring.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>SSM</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>
  
  <!-- web项目中引入spring -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:spring.xml</param-value>
  </context-param>
  
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  
</web-app>

9.编写service接口

package com.liu.service;

import java.util.List;

import com.liu.entity.Student;

public interface StudentService {
	
	List<Student> findAl();
}

10.编写service的初始(逻辑基本写在这)

package com.liu.service.impl;

import java.util.List;

import com.liu.dao.StudentDao;
import com.liu.entity.Student;
import com.liu.service.StudentService;

public class StudentServiceImpl implements StudentService{
	//service依赖于dao
	private StudentDao studentDao;
	
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public List<Student> findAl() {
		
		return studentDao.findAll();
	}

}

在spring中注入依赖

	<!-- 依赖注入 -->
	<bean id="studentService" class="com.liu.service.impl.StudentServiceImpl">
		<property name="studentDao" ref="studentDao"></property>
	</bean>

11.在src下创建springmvc.xml并在web.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:aop="http://www.springframework.org/schema/aop"
        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/mvc 
         http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd">
         
         <!-- 扫描有注解的包 -->
         <context:component-scan base-package="com.liu.controller"></context:component-scan>
         
         <!-- 配置视图解析器 -->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	         <property name="prefix" value="/views/"></property>
	         <property name="suffix" value=".jsp"></property>
         </bean>
		
		<!-- 对静态动态资源的处理,使用servict -->
		<mvc:annotation-driven></mvc:annotation-driven>
		<mvc:default-servlet-handler/>
         
</beans>

在web.xml中配置

 <!-- 引入springmvc -->
    <!-- 拦截全部请求交给mvc处理 -->
  <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>

12.编写控制层StudentController

package com.liu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liu.entity.Student;
import com.liu.service.StudentService;

@RequestMapping("controller")
@Controller
public class StudentController {
	//控制器依赖于service
	@Autowired
	private StudentService studentService;
	

	@RequestMapping("findAll")
	public String findAll(ModelMap map) {
		List<Student> student = studentService.findAl();
		map.put("students", student);
		return "findAll";
	}
}

13.编写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="controller/findAll">查询学生</a>
</body>
</html>

14.编写返回的/views/findAll.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<c:forEach items="${requestScope.students}" var="student">
		${student.stuId}--${student.stuName}--${student.stuAge}<br>
	</c:forEach>
</body>
</html>

15.启动测试

ssm框架整合演示
当点击查询学生时,跳转到/views/findAll.jsp并在jsp中展示出查到的内容
ssm框架整合演示
至此,简单的ssm整合查询student成功显示!

本文地址:https://blog.csdn.net/qq_44846864/article/details/109255474