SSM框架原理以及流程

Posted You295

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM框架原理以及流程相关的知识,希望对你有一定的参考价值。

一:原理

1.springmvc:
1).客户端发送请求到DispacherServlet(分发器)

2).由DispacherServlet控制器查询HanderMapping,找到处理请求的Controller

3).Controller调用业务逻辑处理后,返回ModelAndView

4).DispacherSerclet查询视图解析器,找到ModelAndView指定的视图

5).视图负责将结果显示到客户端

2.spring

平时开发接触最多的估计就是IOC容器,它可以装载bean(也就是我们Java中的类,当然也包括service dao里面的),有了这个机制,我们就不用在每次使用这个类的时候为它初始化,很少看到关键字new。另外spring的aop,事务管理等等都是我们经常用到的。

3.Mybatis

mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。

4.各项作用:

SSM框架是spring MVC ,spring和mybatis框架的整合,是标准的MVC模式,将整个系统划分为表现层,controller层,service层,DAO层四层

使用spring MVC负责请求的转发和视图管理

spring实现业务对象管理,mybatis作为数据对象的持久化引擎

二:开发流程

1.新建maven项目

2.配置整合文件

2.1 配置pom.xml文件—引入依赖

 <properties>
		<project.spring.version>5.3.9</project.spring.version>
	</properties>



	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${project.spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${project.spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${project.spring.version}</version>
			<scope>test</scope>
		</dependency>


		<!-- mybatis -->


		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.5.7</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.25</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.2.6</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.32</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.32</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>2.0.6</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.20</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.12.4</version>
		</dependency>
	</dependencies>

2.2 配置web.xml文件

 
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/mybatis/spring-*.xml</param-value>
	</context-param>

	<!-- 启动业务loc容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  
  
  
  
     <!-- 启动mvc容器 -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- springmvc文件位置 -->
			<param-value>classpath:spring/mvc/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>

2.3 配置springmvc

  <!-- 扫描位置 -->
     <context:component-scan base-package="com.dyit.ssm.entity"/>
     <!-- 静态资源放行 -->
      <mvc:resources location="static/" mapping="static/**"/>
      <!-- 驱动注解 -->
      <mvc:annotation-driven/>

2.4 配置mybatis

1).配置spring-mybatis.xml

   <!-- 管理范围 -->
      <context:component-scan base-package="com.dyit.ssm">
         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>      
      </context:component-scan>
      
     <context:property-placeholder location="classpath:spring/mybatis/druid/druid.properties"/>
     
     <!-- 数据源 -->
     <bean id="dataSource"
		class="com.alibaba.druid.pool.DruidDataSource">
		<property name="url" value="${druid.url}" />
		<property name="username" value="${druid.username}" />
		<property name="password" value="${druid.password}" />
		<property name="driverClassName" value="${druid.driver}" />
	</bean>
	
	<!-- 容器创建SqlSessionFactory -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="typeAliasesPackage" value="com.dyit.ssm.entity"/>
		<property name="mapperLocations" value="classpath:spring/mybatis/mapper/*.mapper.xml"/>
	</bean>
     
     <!-- 配置接口 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.dyit.ssm.mapper"/>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
	</bean>
	
	<!-- 事务 -->
	<bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- 让spring管理起来 -->
	<tx:annotation-driven transaction-manager="txManger"/>

2).配置druid.properties 数据源(数据库相关配置)

druid.url=jdbc:mysql://192.168.1.8:3306/book_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
druid.username=root
druid.password=12345
druid.driver=com.mysql.cj.jdbc.Driver

3.配置Book.mapper.xml(数据库的映射类)

在此我以数据库的 增删改查 为例

<mapper namespace="com.dyit.ssm.mapper.IBookMapper">

	<sql id="findAllBook">
		SELECT * FROM book_tab
	</sql>

	<resultMap type="Book" id="BookMap">
		<id property="id" column="book_id" />
		<result property="title" column="book_title" />
		<result property="price" column="book_price" />
		<result property="author" column="book_author" />
	</resultMap>


	<insert id="save">
		INSERT INTO book_tab(book_title,book_author,book_price)
		VALUES(#{title},#{author},#{price})
	</insert>
	
	<delete id="deleteById">
		DELETE FROM book_tab WHERE book_id=#{id}
	</delete>
	
	
	<insert id="update">
		UPDATE book_tab SET book_title=#{title},
		book_author=#{author},
		
		book_price=#{price}
		WHERE book_id=#{id}
	</insert>

	<select id="findAll" resultMap="BookMap">
		<include refid="findAllBook" />
	</select>


	

</mapper>

3.java代码—测试

1)写实体类

@Alias("Book")  //别名
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
	
	private Integer id; //id
	private String title;  //书名
	private double price;  //价格
	private String author;  //作者

}

2)写mapper类–对数据库的操作接口

@Mapper
public interface IBookMapper {
   
	void save(Book book);  //添加
	void deleteById(int id); //根据id删除
	void update(Book book); //修改
	List<Book> findAll(); //全查询
}

3)写service接口

public interface IBookService {
	
	void save(Book book);  //添加
	void deleteById(int id); //根据id删除
	void update(Book book); //修改
	List<Book> findAll();

}

4)写service接口的实现类

@Service
public class BookServiceImpl implements IBookService{
	@Autowired
	private IBookMapper mapper; //注入mapper
	@Override
	public void save(Book book) { 
		mapper.save(book);
		
	}

	@Override
	public void deleteById(int id) {
		mapper.deleteById(id);
		
	}

	@Override
	public void update(Book book) {
		mapper.update(book);
		
	}

	@Override
	public List<Book> findAll() {
		
		return mapper.findAll();
	}

}

5)写service测试类

@RunWith(SpringJUnit4ClassRunner.class) //测试注解
@ContextConfiguration(locations = "classpath:spring/mybatis/spring-mybatis.xml") //控制文件路径
public class ssmTest {
	
	@Autowired
	private IBookService ibs;  //注入service

	@Test
	public void testSave() {  //添加
		ibs.save(new Book(4, "西游外传", 66.66, "孙悟空"));
	}

	@Test
	public void testDeleteById() {  //删除
		ibs.deleteById(2);;
	}

	@Test
	public void testUpdate() {  //修改
		ibs.save(new Book(4, "西游前传", 88.66, "唐三藏"));
	}

	@Test
	public void testFindAll() {  //全查询
		System.out.println(ibs.findAll());
	}

}

6.以添加图书为例查看测试结果展示

注:因没有写日志配置类,故报红

成功添加进数据库

以上是关于SSM框架原理以及流程的主要内容,如果未能解决你的问题,请参考以下文章

SSM框架原理以及流程

跪解:ssh框架的基本原理?

SSM框架项目搭建系列—SSM框架的简介及搭建流程

zan框架入门(三)——原理浅析

ssm框架物资审批功能

DataX框架的设计、运行原理详解