首页 > 其他分享 >7.整合SSM(配置文件)

7.整合SSM(配置文件)

时间:2022-11-11 17:36:46浏览次数:54  
标签:xml xmlns 配置文件 Spring SSM jdbc 整合 org xsi

7.整合SSM

1.环境要求:

环境:

  • IDEA
  • MySQL 5.7.19
  • Tomcat 9
  • Maven 3.6

要求:

  • 需要熟练掌握MySQL数据库,Spring,JavaWeb及MyBatis知识,简单的前端知识;

2.数据库表

创建一个存放书籍数据的数据库表

CREATE DATABASE `ssmbuild`;

USE `ssmbuild`;

DROP TABLE IF EXISTS `books`;

CREATE TABLE `books` (
`bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT(11) NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT  INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');

3.基本环境搭建

1.新建一个Maven项目!ssmbuild,添加web的支持

2.导入相关的pom依赖

<!--依赖:junit,数据库驱动,连接池,servlet,jsp,mybatis,mybatis-Spring,Spring-->
    <dependencies>
        <!--Junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--数据库驱动Mysql-->
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!-- 数据库连接池 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <!--Servlet - JSP -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <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.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--导入Lombok依赖-->
        <!-- <https://mvnrepository.com/artifact/org.projectlombok/lombok> -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>

    </dependencies>

3.Maven资源过滤器问题

 <!--静态资源导出问题-->
    <!--在build中配置resources,来防止我们资源导出失败的问题-->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

4.建立基本结构和配置框架

  • com.itxiaofei.pojo
  • com.itxiaofei.mapper
  • com.itxiaofei.service
  • com.itxiaofei.controller
  • Mybatis-config.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "<http://mybatis.org/dtd/mybatis-3-config.dtd>">
<configuration>

    <!--引入外部配置文件-->
    <properties resource="database.properties">
        <property name="username" value="root"/>
        <property name="password" value="111222"/>
    </properties>
    <!--配置数据源,交给Spring去做-->

    <!--类型别名:开启才可以给实体类起别名-->
    <typeAliases>
        <package name="com.itxiaofei.pojo"/>
    </typeAliases>

    <!--绑定接口:mapper-->
    <mappers>
        <mapper class="com.itxiaofei.mapper.BookMapper"/>
    </mappers>

</configuration>
  • database.properties
#外部配置文件
jdbc.driver=com.mysql.jdbc.Driver
#如果使用的是Mybatis8.0+,增加一个时区的配置;&serviceTimezone=Asia/shanghai
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&userUnicode=true&serviceTimezone=Asia/shanghai&amp;characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
  • applicationContext.xml
<?xml version="1.0" encoding="UTF8"?>
<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>">
		<!--导入三大配置文件到ioc容器中-->
    <import resource="classpath:spring-mvc.xml"/>
    <import resource="classpath:Spring-service.xml"/>
    <import resource="classpath:Spring-Mapper.xml"/>

</beans>
  • 编写Spring-Mybatis整合配置文件——Spring-Mybatis.xml
<?xml version="1.0" encoding="UTF8"?>
<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>">

    <!--1.管理数据库配置文件-->
    <context:property-placeholder location="classpath:database.properties"/>
    <!--2.连接池
    dbcp:半自动化操作,不能自动连接
    c3p0:自动化操作(自动化的加载配置文件,并且可以自动设置到对象中!)
    druid:
    hikari:
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>
    <!--3.sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定Mybatis的配置文件-->
        <property name="configLocation" value="classpath:Mybatis-Config.xml"/>
    </bean>
    <!--4.配置Mapper接口扫描包,动态实现了Mapper接口可以注入到Spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--要扫描的Mapper包-->
        <property name="basePackage" value="com.itxiaofei.mapper"/>
    </bean>

</beans>
  • Spring整合Service层——Spring-Service.xml
<?xml version="1.0" encoding="UTF8"?>
<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>">

    <!--1.扫描service下的bean-->
    <context:component-scan base-package="com.itxiaofei.service"/>

    <!--2.将我们的所有业务类,注入到Spring,可以通过配置,或者注解实现-->
    <bean id="BookServiceImpl" class="com.itxiaofei.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

    <!--声明式事务配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--AOP事务支持-->
</beans>
  • 编写Spring-MVC层(web.xml and Spring-MVC.xml)

    web.xml

<?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">
    <!--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:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--SpringMVC提供的解决乱码过滤器:encodingFilter-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--Session过期时间-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>
   Spring-MVC.xml
<?xml version="1.0" encoding="UTF8"?>
<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>"
       xsi:schemaLocation="<http://www.springframework.org/schema/beans>
       <http://www.springframework.org/schema/beans/spring-beans.xsd>
       <http://www.springframework.org/schema/mvc>
       <http://www.springframework.org/schema/mvc/spring-mvc.xsd> <http://www.springframework.org/schema/context> <https://www.springframework.org/schema/context/spring-context.xsd>">

    <!--1.开启注解驱动-->
    <mvc:annotation-driven/>
    <!--2.静态资源过滤-->
    <mvc:default-servlet-handler/>
    <!--3.扫描包:controller-->
    <context:component-scan base-package="com.itxiaofei.controller"/>
    <!--4.配置jsp 显示ViewResolver视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!--后缀-->
        <property name="suffix" value=".jsp" />
        
    </bean>
</beans>

配置文件到此,暂时结束,配置地狱到现在也先告一段落!

标签:xml,xmlns,配置文件,Spring,SSM,jdbc,整合,org,xsi
From: https://www.cnblogs.com/itxiaofei/p/16881193.html

相关文章

  • Spark3.1.2与Iceberg0.12.1整合-hadoop和hive的catalog,DDL,隐藏分区(按年,月,天,小时),create
    Spark3.1.2与Iceberg0.12.1整合Spark可以操作Iceberg数据湖,这里使用的Iceberg的版本为0.12.1,此版本与Spark2.4版本之上兼容。由于在Spark2.4版本中在操作Iceberg时不支持D......
  • 促进整合经营管理,贝锐蒲公英打造连锁门店收银系统远程互联方案
    伴随市民生活水平的日益提高,如今大家对于便捷性消费购物的需求也越来越大。在众多国外品牌连锁便利店入驻的同时,更多优秀的国内连锁加盟品牌也渐渐地开始崭露头角。由于连......
  • SpringBoot07(springboot整合MyBatis)
    一、整体思路解析:二、步骤分析:1-搭建springboot的工程(在idea里面搭建,略)2-引入MyBatis的起步依赖,添加msql驱动<?xmlversion="1.0"encoding="UTF-8"?><projectxm......
  • Spring与Struts2的整合案例及XML配置的具体使用(四)
    东家蝴蝶西家飞,白骑少年今日归。愿,所有迷茫的人,都不再迷茫的,愿,所有努力工作的人,都可以得到应有的回报,愿,所有的人,都可以找到回家的方向,愿,再无苦痛,再无离别。上一章简单介绍......
  • Spring整合Mybatis分析与编码
    Spring整合Mybatis分析与编码正文Mybatis在开发的过程中,必须要经过的步骤有数据表-实体类-实体类别名-mapper接口-mapper文件实现-mapper文件注册-mybatisAPI......
  • springboot整合项目-商城项目订单系统以及aop监测
    订单系统1.持久层1.将数据插入到订单表中insertintot_order(oid除外所有的字段)values(字段的值)2.将数据还要插入订单项的表中insertintot_order_item(oid除......
  • SpringBoot 整合mybatis-plus
    SpringBoot整合mybatis-plus1、导入Maven依赖<dependencies><!--web依赖--><dependency><groupId>org.springframework.boot</groupId>......
  • SpringBoot整合Redis(十九)
    二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。上一章简单介绍了多数据源配置MyBatisPlus(十八),如果没有看过,​​请观看上一章​​一.Redis的介绍和安装......
  • SpringBoot整合Velocity(十二)
    二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。上一章简单介绍了SpringBoot整合FreeMarker(十一),如果没有看过,​​请观看上一章​​学习整合之前,可以看一......
  • SSM框架整合(十五)
    你未看此花时,则此花与汝心同归于寂,你来看此花时,此花颜色一时明白起来,便知此花不在你的心外。上一章简单介绍了SpringMVC的拦截器(十四),如果没有看过,​​请观看上一章​​......