SSM的框架搭建的配置信息

本文详细介绍如何将MyBatis与Spring框架进行整合,并配置数据源、事务管理及视图解析器等内容。涵盖必要的jar包及其作用说明,包括配置文件的具体设置。
  • 必要的jar包

image

  • 关于jar包作用说明:

    • 1.Dpcp的jar包就是数据库的连接池配合mybatis 来进行使用的。
    • 2.Commons-logging 用来记录日志的
    • 3.Commons-pool就是和dpcp配合使用的
    • 4.Mybatis 在dao层来进行操作数据库的数据源的框架
    • 5.Mybatis-Spring 和Spring框架做为一个融合的这样mybatis interface将会自动添加到ioc的容器里
    • 6.Mysql -connector mysql 的驱动jar包
    • 7.Spring- aop 用来实现Spring的面向切片编程(其实说额很牛逼,其实就是在外面又包了一层封装而已,增强了原来的功能)
    • 8.Aspect 配合aop的实现的 用来实现增强的功能
    • 9.Spring beans 自定义的对象放入到ioc容器里
    • 10.Spring context 提供在基础IoC功能上的扩展服务,此外还提供许多企业级服务的支持,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存以及各种视图层框架的封装等.
    • 11.Spring-core spring 的核心包整合所有的功能的
    • 12.Spring expression 规定了一些Spring的才能使用的标签和表达式
    • 13.Spring jdbc Spring自身也提供了一个操作数据的jar但是封装的并不是很完全,所以功能不像mybatis那么完美
    • 14.Spring -orm object relation mapping 对象关系映射 通过xml的配置一个对象,可以自动将其映射为一个对象
    • 15.Spring - tx 对于context 的依赖jar是对事务处理的
    • 16.Web:对于web的基本功能的支持
    • 17.Web-mvc :符合文本mvc的基本的模型
  • web.xml文件

    • 配置一个Spring的监听器
1
2
3
4
<!--在项目启动时开始加载spring配置文件  -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  • 2.配置Spring的文件的位置
1
2
3
4
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
  • 3.配置防止内存溢出的
1
2
3
<!--防止內存溢出  -->
<listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
  • 4.Spring-mvc的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 核心控制器  必要 -->
<servlet>
<servlet-name>dispatcher</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>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
  • 5.编码方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <!-- 编码方式 过滤器  必要 -->
    <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
  • Spring的配置文件

  • 配置扫描器:排除spring-mvc所使用的controller的注解
1
2
3
4
5
<context:component-scan base-package="com.*">
<context:exclude-filter
type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
  • 如果加载了properties的文件 可以通过外部加载文件
1
<context:property-placeholder location="classpath:jdbc.properties"/>
  • 配置数据源方式:
1
2
3
4
5
6
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/phone?characterEncoding=UTF-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
  • 配置事务管理器
1
2
3
  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
  • 配置mybatis的sqlsession对象放入ioc里面
1
2
3
4
5
6
7
8
9
10
<!-- 3. 配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!--
配置mybatis配置文件所在路径
classpath: 代表了配置文件被存放在了src下
-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>Spring-Mvc的配置文件
  • 这里配置一个mapper扫描,这样产生一个mapper 就就将放入到ioc里,不需要我们指定注解了
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 
4.配置mybatis自动扫描mapper文件
作用:
1.新增了一个mapper文件后,不需要在到mybatis配置文件中去注册
2.会将dao层的类自动存放到ioc容器中,dao层的类不需要写@Repository 注解了
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--
mapper文件以及接口所在的包路径
-->
<property name="basePackage" value="com.dao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
  • Spring-mvc的配置

  • 自动扫描的配置
1
2
3
4
5
<!-- 自动扫描 记得修改包的路径 -->
<context:component-scan base-package="com.controller">
<context:exclude-filter
type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
  • 视图解析器的相关配置
1
2
3
4
5
<!-- 视图解析器 记得关注前缀 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
  • 静态资源的访问
1
2
3
<!-- 静态资源访问  -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值