1.JAVA SSM(Spring+SpringMVC+MyBatis)XML文件配置和Jar包

前言:因为辞职了,所以在现在也就没用Maven,Spring Boot了,自己看的简洁明了就好(不过maven不用导包,直接中央仓库下载是真的舒服,舒服的同时就是要注意一下那些东西的版本问题)。

先来个自己积攒的最基础SSM的jar包和一些XML文件配置的链接吧:
https://download.csdn.net/download/kk907528318/10910571

不论是Spring还是SpringMVC都要在Web.XML里面配置,所以先放上这个配置。
servlet是在Web.xml里面配置的
---------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_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>AjaxJson</display-name>
	
	 <!-- 使用XML配置SpringMVC时加上一下配置,解决静态资源的访问
  	   注解模式使用其他方法解决
   -->
   <servlet-mapping>     
	    <servlet-name>default</servlet-name>    
	    <url-pattern>*.jpg</url-pattern>       
	</servlet-mapping>
	 <servlet-mapping>     
	    <servlet-name>default</servlet-name>    
	    <url-pattern>*.html</url-pattern>       
	</servlet-mapping>      
	<servlet-mapping>         
	    <servlet-name>default</servlet-name>      
	    <url-pattern>*.js</url-pattern>      
	</servlet-mapping>      
	<servlet-mapping>          
	    <servlet-name>default</servlet-name>         
	    <url-pattern>*.css</url-pattern>        
	</servlet-mapping> 
	
	<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>

	<!-- 当项目启动的时候就去读取spring的配置文件 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>


	<!-- web.xml中Spring MVC的配置 -->
	<servlet>
		<servlet-name>mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
<!-- servlet -->
	
</web-app>

1.Spring

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。
基础的三个bean、dao、mapper层可以通过mybatis-generator-core来生成映射文件,方便快捷(只需要改一下generatorConfig.xml文件里的数据库参数配置就行了)。
IOC:

依赖注入(Dependency Injection)和控制反转(Inversion of Control)是同一个概念。
具体含义是:当某个角色(可能是一个Java实例,调用者)需要另一个角色(另一个Java实例,被调用者)的协助时,在传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在Spring里,创建被调用者的工作不再由调用者来完成,因此称为控制反转;创建被调用者实例的工作通常由Spring容器来完成,然后注入调用者,因此也称为依赖注入。所谓依赖注入,是指程序运行过程中,如果需要调用另一个对象协助时,无须在代码中创建被调用者,而是依赖于外部的注入。

AOP:面向切面编程

面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
Spring 的Aop使用的动态代理的技术将公共业务 比如说登录验证,记录日志等 形成一个切面,加入具体业务,而让具体业务从这些繁琐的公共业务中解脱出来,它们只需要专注于自己的业务,而这些公共的业务会自动加入。

---------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:p="http://www.springframework.org/schema/p"
	
	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-4.1.xsd
	
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.1.xsd
	
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
	
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
	
	">
	<!-- 引入注解工具 -->
	<context:annotation-config/>
	<context:component-scan base-package="com.*"/>
	
	<!-- spring与mybatis整合     ,数据库连接池交给 spring来管理 -->
	
	<!-- 去加jdbc的配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
 	<!-- 配置数据库连接池 -->
 	<!-- 由Spring管理数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      	<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="${dbcp.maxActive}" />
		<property name="maxIdle" value="${dbcp.maxIdle}" />
		<property name="minIdle" value="${dbcp.minIdle}" />
		<property name="maxWait" value="${dbcp.maxWait}" />
    </bean>
 	
 	<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 自动扫描mapping.xml文件 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
		<property name="mapperLocations" value="classpath:com/mapper/*.xml"></property>
		<!-- 加载mybatis的配置文件 -->
		 <property name="configLocation" value="classpath:mybatis-config.xml" /> 
	</bean>
	
	<!-- DAO接口所在包名,Spring会自动查找其下的类,并且自动生成其动态代理类!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
	
	<!-- 
			比如说扫描 到StudentMapper.java文件,就相当于
				//得到接口的动态代理类
            StudentMapper stuMapper = session.getMapper(StudentMapper.class);
           	并且会将动态代理类配置到spring IOC容器
	 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
	</bean>

	 <!--*************** 事务管理器  *****************-->
	 <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	 </bean>
	
	
	<!-- 将事务配置成一个切面 ,给服务层的增加/删除/修改等业务添加事务 !!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.*.*(..))"/>
	</aop:config>
	
	<!-- 通知  要执行的代码 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 哪些地方需要加入切面 -->
			<tx:method name="save*"   propagation="REQUIRED" />
			<tx:method name="add*"    propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="del*"    propagation="REQUIRED" />
			<tx:method name="delete*"    propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="destroy*" propagation="REQUIRED" />
			
			<tx:method name="*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	
</beans>

<!-- 
	    任意公共方法的执行:
		execution(public * *(..))
	 任何一个以“set”开始的方法的执行:
		execution(* set*(..))
	AccountService 接口中任意方法的执行:
		execution(* com.lqx.service.AccountService.*(..))
	定义在service包里的任意方法的执行:
		execution(* com.lqx.service.*.*(..))
	定义在service包和所有子包里的任意类的任意方法的执行:
		execution(* com.lqx.service..*.*(..))
	 -->

2.SpringMVC

在我刚开始学习用Spring+MyBatis写网页时,要写servlet,一个请求对应一个servlet(代码冗余度就会提高,对后期代码的维护就会增加难度),小功能,小程序没问题,如果是一个稍微大一点的网页,这样就非常不方便,JAVA文件会急剧增多,眼花缭乱。后来工作需要学了MVC这个东西,就基本不用SM(Spring+MyBatis),基本用的SSM(Spring+SpringMVC+MyBatis)。加上SpringMVC 这个东西有什么好处呢?一个controller里面可以有好多的方法(方法就相当于servlet),这样就可以对来自网页的请求进行归类,让项目文件的结构得到一定的优化,在后期进行代码维护时更加的方便(工程文件结构简单明了,方便你我他)。

---------spring-mvc.xml基础配置---------

<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans"   
    xmlns:context="http://www.springframework.org/schema/context"   
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:p="http://www.springframework.org/schema/p"   
    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-4.1.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/mvc   
    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    ">
    <!-- spring MVC的配置文件 -->
    
    <!-- 开启SpringMVC的注解 -->
    <!-- 引入注解的工具 -->
	<context:annotation-config/>
	<context:component-scan base-package="com.controller"/>
	
	<!-- 开户Spring MVC的注解 -->
	<mvc:annotation-driven/>
	<!-- 开启静态资源的访问 -->
	<mvc:default-servlet-handler/>
	
    
    <!-- 视图分解器  指定 Controller 跳转的页面的前缀和后缀  
    	request.getRequestDispatcer("user/list").forward(request,response);
    -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/"/>  
        <property name="suffix" value=".jsp"/>  
    </bean>
 
    
</beans>

3.MyBatis

在有了Spring后,MyBatis的XML文件就是个空文件了,啥都不要弄了

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值