一、Spring思维导图

二、Spring

1、基本概念

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

spring主要用来开发java应用,构建J2EE平台的web应用。其核心就是提供一种新的机制管理业务对象及其依赖关系。

2、Spring的流程图

解析:上面是在Struts结构图的基础上加入了spring流程图,在web.xml配置文件中加入了spring的监听器,在struts.xml配置文件中添加

<constant name="struts.objectFactory" value="spring" />

是告知Struts2运行时使用spring来管理对象,spring在其中主要做的就是注入实例,所有需要类的实例都由spring管理。

3、spring的优点

容器:spring是一个容器,包含并管理对象的生命周期和配置。可以配置每个bean如何被创建,基于一个可配置原型prototype,你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例。
支持AOP:spring提供对AOP的支持,它允许将一些通用任务,如安全、事物、日志等进行集中式处理,从而提高了程序的复用性。
轻量级框架:spring是轻量级框架,其基本的版本大约2M。
控制反转:spring通过控制反转实现松耦合。对象们给他们依赖,而不是对象本身,方便解耦,简化开发。
方便程序测试:spring提供了Junit4的支持,可以通过注解方便的测试spring程序。
降低java EE API的使用难度:spring对java EE开发中非常难用的一些API(比如JDBC),都提供了封装,使这些API应用难度大大降低。
方便集成各种优秀框架:spring内部提供了对各种优秀框架(如Struts、mybatis)的直接支持。
支持声明式事务处理:只需要通过配置就可以完成对事务的管理,而无须手动编程。

4、spring的缺点
  • 依赖反射,反射影响进程。
  • 太过于依赖设计模式。
  • 控制器过于灵活。
  • 不支持分布式应用。

三、SpringMVC

1、基本概念

属于spring框架的一部分,用来简化MVC架构的web应用程序开发。

2、SpringMVC的优点
  1. 拥有强大的灵活性,非侵入性和可配置性
  2. 提供了一个前端控制器dispatcherServlet,开发者无需额外开发控制器对象
  3. 分工明确,包括控制器、验证器、命令对象、模型对象、处理程序映射视图解析器,每一个功能实现由一个专门的对象负责完成
  4. 可以自动绑定用户输入,并正确的转换数据类型
  5. 可重用的业务代码:可以使用现有的业务对象作为命令或表单对象,而不需要去扩展某个特定框架的基类。
3、SpringMVC的缺点
  1. servlet API耦合难以脱离容器独立运行
  2. 太过于细分,开发效率低

四、mybatis

1、基本概念

mybatis是一个简化和实现了java数据持久层的开源框架,它抽象了大量的JDBC冗余代码,并提供了一个简单易用的API和数据库交互。

2、mybatis的优点
  1. 与JDBC相比,减少了50%以上的代码量。
  2. mybatis是最简单的持久化框架,小巧并且简单易学。
  3. mybatis灵活,不会对应用程序或者数据库的限售设计强加任何影响,SQL写在XML里,从程序代码中彻底分离,降低耦合度,便于统一管理和优化,可重用。
  4. 提供XML标签,支持编写动态SQL语句(XML中使用if,else)。
  5. 提供映射标签,支持对象与数据库的ORM字段关系映射(在XML中配置映射关系,也可以使用注解)

3、mybatis的缺点

  1. SQL语句的编写工作量较大,对开发人员的SQL语句编写有一定的水平要求。
  2. SQL语句过于依赖数据库,不能随意更换数据库。
  3. 拼接复杂SQL语句时不灵活。

五、前置知识总结

Java框架总结

Spring AOP基础知识总结

Spring常用注解(绝对经典)

SpringMVC中put和post如何选择

@RequestParam、@ModelAttribute、@RequestBody的区别

mybatis常用注解(绝对经典)

【MyBatis基础知识总结 1】SQL注入

【MyBatis基础知识总结 2】MyBatis-Plus

【MyBatis基础知识总结 3】MyBatis一级缓存和二级缓存

【MyBatis 基础知识总结 4】动态sql

【MyBatis 基础知识总结 5】SqlSessionFactory和SqlSession

【MyBatis基础知识总结 6】Statement、PreparedStatement和CallableStatement

六、Java程序员简历上的第一个项目

Spring、SpringMVC、MyBatis整合
1、大体框架

2、引入jar包

3、配置

与spring整合时,mybatis的配置文件conf.xml(数据源+mapper.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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
	<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="locations">
			<array>
				<value>classpath:db.properties</value>
			</array>
		</property>
	</bean>
 
	<!-- 配置数据库信息(替代mybatis的配置文件confx.ml) -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
	</bean> 
	
	<!-- 在springIOC中创建mybatis的核心类SqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="mapperLocations" value="classpath:com/guor/mapper/\*.xml"></property>
	</bean> 
 
	<bean id="studentService" class="com.guor.service.impl.StudentServiceImpl">
		<property name="studentMapper" ref="studentMapper"></property>
	</bean>
	
	<!-- 批量产生mapper对象在IOC中的id值默认就是接口名-->
	<bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
		<!-- 指定批量产生哪个包的mapper对象 -->
		<property name="basePackage" value="com.guor.mapper"></property>  
	</bean>
</beans>

数据库配置

driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
username=orcl
password=orcl
4、编写代码

(1)entity

package com.guor.entity;
 
public class Student {
	private int id;
	private String name;
	private int age;
 
	...
}

(2)mapper

package com.guor.mapper;
 
import com.guor.entity.Student;
 
public interface StudentMapper {
	public void addStudent(Student student);
 
	public Student queryStudentByStuNo(int id);
}

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.guor.mapper.StudentMapper">
	<select id="queryStudentByStuNo" parameterType="int" resultType="com.guor.entity.Student">
		select * from student where id = #{stuNo}
	</select>
	
	<insert id="addStudent" parameterType="com.guor.entity.Student">
		insert into student(id,name,age) values (#{id},#{name},#{age})
	</insert>
</mapper>

(3)StudentService

package com.guor.service;
 
import com.guor.entity.Student;
 
public interface IStudentService {
	public void addStudent(Student student);
	public Student queryStudentByStuNo(int id);
}

StudentServiceImpl

package com.guor.service.impl;
 
import com.guor.entity.Student;
import com.guor.mapper.StudentMapper;
import com.guor.service.IStudentService;
 
public class StudentServiceImpl implements IStudentService {
	private StudentMapper studentMapper;
	
	public void setStudentMapper(StudentMapper studentMapper) {
		this.studentMapper = studentMapper;
	}
 
	@Override
	public void addStudent(Student student) {
		studentMapper.addStudent(student);
	}
 
	@Override
	public Student queryStudentByStuNo(int id) {
		return studentMapper.queryStudentByStuNo(id);
	}
}

(4)controller

package com.guor.controller;
 
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.guor.entity.Student;
import com.guor.service.IStudentService;
 
@Controller
@RequestMapping("studentController")
public class StudentController {
	@Autowired
	@Qualifier("studentService")
	private IStudentService studentService;
 
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}
 
	@RequestMapping("queryStudentByStuNo/{id}")//映射
	public String queryStudentByStuNo(@PathVariable("id") Integer id,Map<String,Object> map) {
		Student student = studentService.queryStudentByStuNo(id);
		map.put("student", student);
		return "result";
	}
}

(5)配置文件

<?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:mvc="http://www.springframework.org/schema/mvc" 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/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd 
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<!-- 扫描有注解的包 -->
	<context:component-scan base-package="com.guor.controller"></context:component-scan>
	
	<!-- 配置视图解析器(InternalResourceViewResolver) -->
	<bean 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>
	
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐