基于ssm三大框架实现登录注册功能的配置过程

第一步  ssm jar包整合,本人的下载资源里面有整合的jar包

主要有:ssm整合jar包 ,jstl,数据库连接,josn junit,等jar包


第二步,建立各类包和配置文件,尽量把各个配置文件分开,统一放在一个自己建立的config文件夹中,容易区分,后面也好检查更改

主要配置,mybatis,spring—mybatis,和springmvc,以及web.xml和jdbc(数据库连接信息)

jdbc配置文件

jdbc.DriverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/hello
jdbc.username=root
jdbc.password=518189

mybatis配置文件

说明:如何单独使用mybatis时,mybatis配置文件中需要配置环境,也就是标签<environment>里面的信息

ssm中这些事,由spring来做,在mybatis中只需要类的别名,用法如下

 <typeAliases>  
            设置这个包下面的所有类的别名  默认是类名小写,也就是这个包里面所有的类就可以使用类名的小写代替自己了
            <package name="cn.itsource.domain"/>  
            设置单个类的别名       
       alias:取的别名    
      type:这个别名所对应的Java类    别名使用的时候与大小写无关
        <typeAlias alias="Product" type="cn.itsource.domain.Product"/>  
    </typeAliases>
总结;

typeAlias:为某个java类起名②package:为某个包下所有类批量起别名(常用)

 ③@Alias:批量起别名的情况下使用,为某个类型指定新的别名(避免别名重复)

配置文件

<?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>
<!-- 注意事项:单独使用mybatis的时候,需要配置环境(数据源和事务管理器)
但是当mybatis和spring进行整合的时候,交给spring容器来进行管理
-->
<typeAliases>
<package name="cn.edu360.ssm.model"/>
</typeAliases>    
</configuration>

spring—mybatis配置文件,里面包含了spring和mybatis的整合配置

我们知道单独使用mybatis时,需要底下这样来,来创建 SqlSessionFactoryBuilder()实现一个会话,调用mapper操作数据库,现在通过spring的控制反转,让spring来做,但是需要配置。

       InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sessionFactory.openSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.deleteStudent("1007");
        session.commit();//提交

spring—mybatis配置文件

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://www.springframework.org/schema/mvc"

 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-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 加载配置文件,加载数据的连接信息 -->
    <context:property-placeholder location="classpath:jdbc/jdbc.properties" />

    <!-- 数据源,使用dbcp -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.DriverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!-- sqlSessinFactory在mybatis单独使用的时候
    咱们封装了一个工具类来获取该单例,但是和spring整合之后
    完全交给spring的ioc容器来实例化该对象
     -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 加载mybatis的配置文件 -->
        <property name="configLocation" value="classpath:mybatis/mybatis.xml" />
        <!-- 数据源 的信息-->

        <property name="dataSource" ref="dataSource"/>

     <!-- 获取所有,mapper的信息 注意后面的通配符-->     

  <property name="mapperLocations" value="classpath:cn/edu/ssm/mapper/*.xml"></property>

    </bean>
    <!-- spring和mybatis整合的jar包,提供了
    自动生成mapper代理对象的机制,
    只需要配置mapper接口的包路径
    和sqlsessionfactory
    -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.edu.ssm.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>       
    </bean>
</beans>

springmvc配置文件

由于springmvc就属于spring所以不需要整合,之前怎么配置,现在还怎么配置

<beans xmlns="http://www.springframework.org/schema/beans"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://www.springframework.org/schema/mvc"

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-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- 注解的映射器和适配器 -->
    <mvc:annotation-driven/>    
    <!-- 附件扫描器 会扫描指定包下面的controller、service、repository等注解
    springIoc容器会帮助咱们创建和依赖注入po,注意这个包的范围尽量大点不能只具体到controller层。
    -->
    <context:component-scan base-package="cn.edu.ssm"/>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置jsp路径的前缀 -->
        <property name="prefix" value="/WEB-INF/pages/" />
        <!-- 配置jsp路径的后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
      <mvc:resources location="/html/" mapping="/html/**"/>  
</beans>

web.xml

web.xml需要把spring的配置文件和springmvc的配置文件加载进来。

<?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">
    <!-- 配置上下文参数,指定spring配置文件的位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/spring-mybatis.xml
        </param-value>
    </context-param>
    <!-- 配置spring监听器 ,加载上下文参数,监听springIOC容器的创建工作 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Springmvc的核心控制器 -->
    <servlet>
        <servlet-name>dispatchServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- <servlet>
    <servlet-name>addPic</servlet-name>
        <servlet-class>/WEB-INF/pages/enterpriseManage/addPic.jsp</servlet-class>
    </servlet> -->
    <servlet-mapping>
        <servlet-name>dispatchServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <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>

</web-app>

以上就是ssm配置文件的全过程,由于源代码太多就不在这一一添加了,只要配置文件会配置,注解会使用,其实ssm很简单的。



猜你喜欢

转载自blog.csdn.net/aA518189/article/details/79592641
今日推荐