ssm整合

Spring框架:
Spring框架为了解决软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前非常复杂的企业级开发。Spring解决了业务对象,功能模块之间的耦合,不仅在javase,web中使用,大部分Java应用都可以从Spring中受益。
Spring是一个轻量级控制反转(IoC)面向切面(AOP)的容器。

SpringMVC框架:
Spring MVC属于SpringFrameWork 3.0版本加入的一个模块,为 Spring 框架提供了构建 Web 应用程序的能力。现在可以 Spring框架提供的SpringMVC模块实现web应用开发,在web项目中可以无缝使用Spring和Spring MVC框架。
web项目使用的其他框架:Struts1(一般不用),Struts 2(旧有项目使用)。

MyBatis框架:
MyBatis是一个优秀的基于java的持久层框架,内部封装了jdbc,开发者只需要关注sql语句本身,而不需要处理加载驱动、创建连接、创建 statement、关闭连接,资源等繁杂的过程。

MyBatis通过xml注解两种方式将要执行的各种sql语句配置起来,并通过java对象和sql 的动态参数进行映射生成最终执行的 sql 语句,最后由 mybatis 框架执行 sql 并将结果映射为 java 对象并返回。



使用步骤

1. 数据库mydb,创建表格classroom,编写数据

2. 创建maven模块

3. 导入依赖 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bjpowernode</groupId>
    <artifactId>web1214</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.2.8.RELEASE</spring.version>
    </properties>

    <build>

        <resources>
            <!--资源不过滤,需要打包-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!--资源不过滤-->
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <!--JDK插件-->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!--spring-web-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--spring-mvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--spring-context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--Spring test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--junit test-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!--JSTL-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!--mysql driver-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>

        <!--servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

        <!--connection pool-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>

        <!--spring-jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>


    </dependencies>


</project>

4. 创建webapp文件夹,配置web.xml

5. 编写配置文件

applicationContext.xml

扫描组件、配置数据源、整合mybatis(简化了持久层代码)

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

        <!--扫描组件,让Spring托管-->
            <context:component-scan base-package="com.bjpowernode" />

        <!--加载配置-->
            <!-- ignore-unresolvable为true时,配置文件${}找不到对应占位符的值,直接赋值'${}' -->
            <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>

            <!--配置数据源druid-->
                    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
                        <!--driverClassName可以不写,智能匹配-->
                        <property name="driverClassName" value="${jdbc.driver}" />
                        <property name="url" value="${jdbc.url}" />
                        <property name="username" value="${jdbc.username}" />
                        <property name="password" value="${jdbc.password}" />
                    </bean>

            <!--整合MyBatis-->
                    <!-- 配置 SqlSessionFactory -->
                    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                        <!--读取mybatis的主配置-->
                        <property name="configLocation" value="classpath:mybatis.xml"/>
                        <!--衔接数据源,目前使用的是数据库连接池-->
                        <property name="dataSource" ref="dataSource" />
                        <!--批量起别名,放在beans包中【也就是实体】-->
                        <property name="typeAliasesPackage" value="com.bjpowernode.beans" />
                    </bean>
                    <!-- 配置Spring整合MyBatis,扫描包 -->
                    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                        <!--注册MyBatis的映射文件-->
                        <property name="basePackage" value="com.bjpowernode.mapper" />
                        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
                    </bean>
</beans>
spring-mvc.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

        <!--扫描组件,让Spring托管-->
            <context:component-scan base-package="com.bjpowernode.controller" />

        <!--视图解析器-->
            <!--前缀目录为:/WEB-INF/jsp/-->
            <!--后缀扩展名为:.jsp-->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/jsp/" />
                <property name="suffix" value=".jsp" />
            </bean>

        <!--启用注解驱动-->
            <mvc:annotation-driven />

        <!--如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理-->
            <mvc:default-servlet-handler />
</beans>

 

jdbc.properties
#数据库
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
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>
    <!--全局配置-->
    <settings>
        <!--SQL日志-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
</configuration>
web.xml

乱码处理、启动spring上下文、注册SpringMvc DispatcherEncodingFilter的核心处理器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
            <!--乱码处理-->
                    <!--Spring自带的CharacterEncodingFilter过滤器-->
                    <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>

        <!--启动SpringContext-->
            <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </context-param>
            <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
            </listener>

        <!--注册SpringMVC DispatcherServlet的核心处理器-->
            <servlet>
                <servlet-name>DispatcherServlet</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>
            <!--配置SpringMVC拦截路径-->
            <servlet-mapping>
                <servlet-name>DispatcherServlet</servlet-name>
                <url-pattern>/</url-pattern>
            </servlet-mapping>
</web-app>

6. 编写实体类beans

7. 编写持久层mapper

8. 编写业务层services

9. 编写index.jsp

10. 编写处理器controller

11. 配置Tomcat,添加war exploded包进去后,运行测试



总结:mybatis使持久层的代码简化了很多,不需要处理加载驱动、创建连接、创建 statement、关闭连接,资源等繁杂的过程。

spring-mvc使用注解也很方便。

posted @ 2022-12-17 22:34  鹿先森JIAN  阅读(22)  评论(0编辑  收藏  举报