Spring开发web项目

Spring开发Web项目

Spring开发Web项目需要的jar包

spring-aop.jar--------------------开发AOp特性时需要的jar

spring-beans.jar-----------------处理bean的jar

spring-context.jar----------------处理spring上下文的jar

spring-core.jar-------------------spring核心jar

spring-expresiion.jar------------spring表达式

commons-logging.jar------------三方提供的日志jar

spring-web.jar--------------------web项目需要的jar

注意Web项目的jar包要放在lib目录中

Web项目如何初始化SpringIOC容器

当服务启动时(tomcat),通过监听器将SpringIOC容器初始化一次,spring-web.jar中已经实现。

web项目启动时,会自动加载web.xml,因此需要在web.xml文件中初始化IOC容器(加载监听器)。

1.初始化IOC容器(applicationContext.xml),需要告诉监听器此容器所在的位置:

通过标签

示例:

<context-param>
  <!-- 
	  监听器的父类ContextLoader中有一个contextConfigLocation属性,该属性值保存着
	  IOC容器(applicationContext.xml) 的位置
  -->
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  <!--
  	 配置spring-web.jar提供的监听器,此监听器可以在服务器启动时初始化IOC容器
  	 初始化IOC容器(applicationContext.xml),需要告诉监听器此容器所在的位置:
  	 通过<context-param>
   -->
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

2.使用默认约定的位置:

将IOC容器(applicationContext.xml)放入WEB-INF目录下,文件名必须为applicationContext.xml。


Web项目拆分及合并Spring配置文件

Web项目根据什么拆分?

  1. 根据三层结构拆分
    UI(html/css/jsp、Servlet)拆为 applicationController.xml
    Service 拆为 applicationService.xml
    Dao 拆为 applicationDao.xml
    公共数据库 拆为 applicationDB.xml
  2. 根据功能结构拆分
    学生相关配置 applicationContextStudent.xml
    班级相关配置 applicationContextClass.xml

多个Spring配置文件的的合并

1.在web.xml中将多个配置文件逐个加载(配置文件之间用逗号隔开)

<context-param>
  <!-- 
	  监听器的父类ContextLoader中有一个contextConfigLocation属性,该属性值保存着
	  IOC容器(applicationContext.xml) 的位置
  -->
  	<param-name>contextConfigLocation</param-name>
  	<param-value>
  		classpath:applicationContext.xml,
  		classpath:application-Dao.xml,
  		classpath:application-Service.xml,
  		classpath:application-Controller.xml
  	</param-value>
  </context-param>
 <listener>
  <!--
  	 配置spring-web.jar提供的监听器,此监听器可以在服务器启动时初始化IOC容器
  	 初始化IOC容器(applicationContext.xml),需要告诉监听器此容器所在的位置:
  	 通过<context-param>
   -->
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

2.通过*通配符(此方式原理上还是逐个加载)

<context-param>
  <!-- 
	  监听器的父类ContextLoader中有一个contextConfigLocation属性,该属性值保存着
	  IOC容器(applicationContext.xml) 的位置
  -->
  	<param-name>contextConfigLocation</param-name>
	<!-- * 代替了
			application-Dao.xml,
  		application-Service.xml,
  		application-Controller.xml
	 -->
  	<param-value>
  		classpath:applicationContext.xml,
  		classpath:application-*.xml,
  	</param-value>
  </context-param>
 <listener>
  <!--
  	 配置spring-web.jar提供的监听器,此监听器可以在服务器启动时初始化IOC容器
  	 初始化IOC容器(applicationContext.xml),需要告诉监听器此容器所在的位置:
  	 通过<context-param>
   -->
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

3.在web.xml只加载主配置文件,然后在主配置文件中加载其他配置文件

web.xml文件:

<context-param>
  <!-- 
	  监听器的父类ContextLoader中有一个contextConfigLocation属性,该属性值保存着
	  IOC容器(applicationContext.xml) 的位置
  -->
  	<param-name>contextConfigLocation</param-name>
	<!-- * 代替了
			application-Dao.xml,
  		application-Service.xml,
  		application-Controller.xml
	 -->
  	<param-value>
  		classpath:applicationContext.xml,
  		classpath:application-*.xml,
  	</param-value>
  </context-param>
 <listener>
  <!--
  	 配置spring-web.jar提供的监听器,此监听器可以在服务器启动时初始化IOC容器
  	 初始化IOC容器(applicationContext.xml),需要告诉监听器此容器所在的位置:
  	 通过<context-param>
   -->
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

主配置文件(可以逐个配置也可以通过*通配符):

<?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">
	<import resource="appliaction-Dao.xml"/>
	<import resource="appliaction-Service.xml"/>
	<import resource="appliaction-Controller.xml"/>

	<!-- <import resource="appliaction-*.xml"/> -->
</beans>

Spring整合Web项目

凡是出现属性的地方都通过Spring IOC配置,而不是去实例化或者直接赋值。

注意servlet(Web)容器和Sring IOC容器的连接

一般通过servlet的初始化方法进行连接

public class QueryStudentByIdServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
IStudentService studentService;
//set注入需要此方法
public void setStudentService(IStudentService studentService) {
this.studentService = studentService;
}
//servlet初始化方法,在初始化时获取Spring IOC容器中的bean对象
@Override
public void init() throws ServletException {
	//ApplicationContext context = new ClassPathXmlApplicationContext("application-Service.xml");
	//web项目获取Spring上下文对象(Spring IOC容器)
	ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
	//在servlet容器中,通过getBean获取IOC容器中的bean
	studentService =(IStudentService) context.getBean("studentService");
}
}

Spring整合MyBatis

思路:MyBatis是通过SqlSessionFactory对象来操作数据库,Spring整合MyBatis就是将MyBatis的SqlSessionFactory对象交给Spring。

整合步骤

  1. jar包
    mybatis-spring.jar
    spring-tx.jar
    spring-jdbc.jar
    spring-exoression.jar
    spring-context-support.jar
    spring-core.jar
    spring-context.jar
    spring-beans.jar
    spring-aop.jar
    spring-web.jar
    commons-logging.jar
    commons-dbcp.jar
    mysql-connector-java.jar
    mybatis.jar
    log4j.jar
    commons-pool.jar

  2. 准备 类-----表

  3. MyBatis配置文件conf.xml

  4. 通过mapper.xml文件,将类和表建立映射关系

  5. 之前使用Mybatis通过conf.xml产生SqlSessionFactory对象
    现在整合的时候,需要通过Spring管理SqlSessionFactory对象,因此SqlSessionFactory对象所需要的数据库信息不再放入conf.xml中,而是放在Spring配置文件中
    将Spring配置文件作为主配置文件,MyBatis配置文件通过Spring配置文件加载
    spring配置文件示例:

     <?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">
     
     <!-- 加载数据库信息文件db.properties -->
     <bean id="DBproperties" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
     	<property name="locations">
     		<array>
     			<value>classpath:db.properties</value>
     		</array>
     	</property>
     </bean>
     
     <!-- 配置数据库信息(替代MyBatis配置文件conf.xml) -->
     <!-- org.apache.commons.dbcp.BasicDataSource 为dbcp模式,因此不需要手动提交事务-->
     <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>
     	<property name="maxActive" value="${maxActive}"></property>
     	<property name="maxIdle" value="${maxIdle}"></property>
     </bean>
     
     <!-- 在SpringIOC容器中创建MyBatis的核心类SqlSessionFactory -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     	<!-- 数据库信息(数据源) -->
     	<property name="dataSource" ref="dataSource"></property>
     	<!-- 加载MyBatis配置文件conf.xml -->
     	<property name="configLocation" value="classpath:conf.xml"></property>
     </bean>
     
     </beans>
    

6.使用Spring-MyBatis整合产物开发程序
目标:通过Spring产生MyBatis最终操作需要的动态代理对象(mapper对象)
spring产生动态代理对象的3种方法

  • 1**.DAO层实现类继承SqlSessionDaoSupport类**
    DAO层实现类继承SqlSessionDaoSupport类,
    SqlSessionDaoSupport类提供了SqlSession属性

      package org.lyh.dao.impl;
      import org.apache.ibatis.session.SqlSession;
      import org.lyh.entity.Student;
      import org.lyh.mapper.StudentMapper;
      import org.mybatis.spring.support.SqlSessionDaoSupport;
      //继承SqlSessionDaoSupport类并且实现IStudentDao接口(改名为StudentMapper,为了更好地与studentMapper.xml文件配合)
      public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper{
      	public void addStudent(Student student) {
      		SqlSession sqlSession = super.getSqlSession();
      		StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
      		mapper.addStudent(student);
      	}
      }
    

在MyBatis配置文件中加载具体的mapper.xml(也可以在Spring文件中加载mapper.xml)

示例:在MyBatis配置文件中加载具体的mapper.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>
    <!-- 数据库信息放在了Spring配置文件中 -->
    <!-- 加载配置文件 -->
    <mappers>
    	<mapper resource="org/lyh/mapper/studentMapper.xml"/>
    </mappers>
</configuration>

示例:在Spring文件中加载mapper.xml

<!-- 在SpringIOC容器中创建MyBatis的核心类SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 数据库信息(数据源) -->
	<property name="dataSource" ref="dataSource"></property>
	<!-- 加载MyBatis配置文件conf.xml -->
<!-- 	<property name="configLocation" value="classpath:conf.xml"></property>-->
 	<!-- 加载mapper.xml文件(*代表当前路径的所有xml文件,也可以逐个加载) -->
	<property name="mapperLocations" value="org/lyh/mapper/*.xml"></property>
</bean>

因为MyBatis配置文件中所有配置都可以在Spring配置文件中完成,所以MyBatis配置文件就毫无用武之地了,就可以将其删去。


在Spring配置文件中将sqlSessionFactory对象交给 studentMapper(dao层)

	<!-- 在SpringIOC容器中创建MyBatis的核心类SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库信息(数据源) -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载MyBatis配置文件conf.xml -->
<!-- 	<property name="configLocation" value="classpath:conf.xml"></property>-->
 	<!-- 加载mapper.xml文件(*代表当前路径的所有xml文件,也可以逐个加载) -->
	<property name="mapperLocations" value="org/lyh/mapper/*.xml"></property>
	
	<!-- 将业务类注入到IOC容器中 -->
	<bean id="studentService" class="org.lyh.service.impl.StudentServiceImpl">
		<property name="studentMapper" ref="studentMapper"></property>
	</bean>
	<!--第一种方式生成mapper代理对象-->
	<bean id="studentMapper" class="org.lyh.dao.impl.StudentDaoImpl">
		<!-- 将sqlSessionFactory对象交给 studentMapper(dao层)-->
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
  • 2.省略掉第一种方式的实现类
    直接使用MyBatis提供的实现类:org.mybatis.spring.mapper.MapperFactoryBean

    <!–第一种方式生成mapper代理对象



    –>

      <!-- 第二种方式生成mapper代理对象 -->
       <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
      	<!--value直接写mapper接口,不要写mapper接口的实现类  -->
       	<property name="mapperInterface" value="org.lyh.mapper.StudentMapper"></property>
      	<!--将sqlSessionFactory对象交给 studentMapper(dao层)-->	
      	<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
       </bean>
    

缺点:每个mapper代理对象都要配置一次。

  • 3.批量配置mapper对象
    使用MyBatis提供的实现类:org.mybatis.spring.mapper.MapperScannerConfigurer

        <!-- 
        	第三种方式生成mapper代理对象(批量产生多个mapper)
        	批量产生的mapper在SpringIOC容器中的id值默认就是首字母小写的接口名(首字母小写的=id)
         -->
       <bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      	<!-- 指定批量产生哪个包中的mapper对象,包与包之间用逗号隔开 -->
      	<property name="basePackage" value="org.lyh.mapper"></property>
      	<!--将sqlSessionFactory对象交给 studentMapper(dao层)-->	
      	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
       </bean>
       
       <bean id="studentService" class="org.lyh.service.impl.StudentServiceImpl">
      	<property name="studentMapper" ref="studentMapper"></property>
      </bean>
    
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值