当前位置: 首页 > article >正文

项目实战-编写ssm整合配置文件

1、父工程pom.xml

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>6.0.6</spring.version>
        <junit.version>5.3.1</junit.version>
        <lombok.version>1.18.20</lombok.version>
        <mysql.version>8.0.25</mysql.version>
        <druid.version>1.2.8</druid.version>
        <logback.version>1.2.3</logback.version>
        <jakarta.version>9.1.0</jakarta.version>
        <jackson.version>2.15.0</jackson.version>
        <hibernate.validator.version>8.0.0.Final</hibernate.validator.version>
        <fileupload.version>1.3.1</fileupload.version>
        <mybatis.version>3.5.11</mybatis.version>
        <mybatis-spring.version>3.0.2</mybatis-spring.version>
        <pagehelper.version>5.1.11</pagehelper.version>
    </properties>

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

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>

            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit.version}</version>
            </dependency>

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

            <dependency>
                <groupId>jakarta.annotation</groupId>
                <artifactId>jakarta.annotation-api</artifactId>
                <version>2.1.1</version>
            </dependency>

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

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

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>

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

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

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

            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>${logback.version}</version>
            </dependency>

            <dependency>
                <groupId>jakarta.platform</groupId>
                <artifactId>jakarta.jakartaee-web-api</artifactId>
                <version>${jakarta.version}</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>

            <!-- 校验注解实现-->
            <dependency>
                <groupId>org.hibernate.validator</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>${hibernate.validator.version}</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate.validator</groupId>
                <artifactId>hibernate-validator-annotation-processor</artifactId>
                <version>${hibernate.validator.version}</version>
            </dependency>

            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${fileupload.version}</version>
            </dependency>

            <!-- mybatis依赖 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>

            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${mybatis-spring.version}</version>
            </dependency>

            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>${pagehelper.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

2、pom.xml

<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
      <groupId>jakarta.platform</groupId>
      <artifactId>jakarta.jakartaee-web-api</artifactId>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
    </dependency>
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
    </dependency>
  </dependencies>

3、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">

		<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:springmvc.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>

			<context-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>classpath:spring.xml</param-value>
			</context-param>

			<listener>
				<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
			</listener>

</web-app>

4、springmvc.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">
    
    <context:component-scan base-package="com.atguigu.schedule" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <mvc:default-servlet-handler/>

    <mvc:annotation-driven/>
</beans>

5、jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///schedule_db
jdbc.user=root
jdbc.pwd=123456

6、schedule_db.sql

CREATE DATABASE schedule_db CHARACTER SET utf8 ;

USE `schedule_db`;

CREATE TABLE `sys_schedule` (
  `sid` int NOT NULL AUTO_INCREMENT COMMENT '日程id',
  `uid` int DEFAULT NULL COMMENT '用户id',
  `title` varchar(50) DEFAULT NULL COMMENT '标题',
  `completed` int DEFAULT NULL,
  PRIMARY KEY (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

insert  into `sys_schedule`(`sid`,`uid`,`title`,`completed`) values 
(1,1,'学习Java',0),
(2,1,'学习HTML5',1),
(3,2,'学习Css',0),
(4,1,'学习JavaScript',0),
(5,1,'学习Spring',1),
(6,1,'学习123',0),
(7,2,'学习456',0);

CREATE TABLE `sys_user` (
  `uid` int NOT NULL AUTO_INCREMENT,
  `username` varchar(20) DEFAULT NULL,
  `userPwd` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`uid`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

insert  into `sys_user`(`uid`,`username`,`userPwd`) values 
(1,'lina','123456'),
(2,'admin','123456');

7、spring.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:tx="http://www.springframework.org/schema/tx"
       xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

    <context:component-scan base-package="com.atguigu.schedule">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.pwd}"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <property name="typeAliasesPackage" value="com.atguigu.schedule.pojo"/>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="reasonable">true</prop>
                            <prop key="helperDialect">mysql</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <mybatis-spring:scan base-package="com.atguigu.schedule.mapper"/>
</beans>

8、logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <!-- 指定日志输出的位置,ConsoleAppender表示输出到控制台 -->
    <appender name="STDOUT"
              class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- 日志输出的格式 -->
            <!-- 按照顺序分别是:时间、日志级别、线程名称、打印日志的类、日志主体内容、换行 -->
            <pattern>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- 设置全局日志级别。日志级别按顺序分别是:TRACE、DEBUG、INFO、WARN、ERROR -->
    <!-- 指定任何一个日志级别都只打印当前级别和后面级别的日志。 -->
    <root level="DEBUG">
        <!-- 指定打印日志的appender,这里通过“STDOUT”引用了前面配置的appender -->
        <appender-ref ref="STDOUT" />
    </root>

    <!-- 根据特殊需求指定局部日志级别,可以是包名或全类名。 -->
    <logger name="com.atguigu.schedule" level="DEBUG" />

</configuration>

 

SSM(Spring + SpringMVC + MyBatis)是一种流行的Java Web开发框架,它结合了Spring、SpringMVC和MyBatis三个主要组件,为开发者提供了一种高效、灵活和易于维护的Web应用程序开发解决方案。

以下是SSM框架的主要作用:

  1. 简化开发:SSM框架简化了Java Web应用程序的开发过程。通过使用Spring和SpringMVC,开发者可以轻松地实现依赖注入、AOP(面向切面编程)等功能,从而降低了代码的复杂度和耦合度。MyBatis则提供了方便的ORM(对象关系映射)功能,使得数据库操作更加简单和直观。
  2. 增强可维护性:SSM框架采用了分层设计,将应用程序分为表现层、业务逻辑层和数据访问层。这种分层设计使得代码结构清晰、模块化程度高,便于维护和扩展。
  3. 灵活性:SSM框架允许开发者根据具体需求进行定制和扩展。例如,开发者可以根据需要调整MyBatis的SQL语句,或者使用Spring的自定义注解来实现特定的业务逻辑。
  4. 提高性能:SSM框架具有较高的性能。通过使用MyBatis直接操作数据库,可以减少不必要的对象创建和销毁等操作,从而提高应用程序的性能。此外,Spring的缓存机制也可以提高应用程序的响应速度。
  5. 广泛的应用场景:SSM框架适用于各种类型的应用程序,包括Web、移动端、微服务等。由于其灵活性和可扩展性,SSM框架可以满足不同领域和场景的开发需求。

总之,SSM框架在Java Web开发中具有重要的作用,它提供了一种高效、可维护、灵活且性能优越的解决方案,适用于各种类型的应用程序开发。


http://www.kler.cn/a/154074.html

相关文章:

  • sqli—labs靶场 5-8关 (每日4关练习)持续更新!!!
  • 富格林:安全指正规防欺诈套路
  • 2.5D视觉——Aruco码定位检测
  • WebAssembly在桌面级应用开发中的探索与实践
  • Qt 之 qwt和QCustomplot对比
  • 创建vue3项目步骤
  • React Native 更换淘宝镜像提升包下载速度
  • 【算法】动态规划中的路径问题
  • Oracle常见内置程序包的使用Package
  • Vue 3.0 响应性 深入响应性原理
  • nacos配置变更导致logback日志异常
  • torch查看GPU信息常用代码
  • 向zabbix服务端发请求的例子
  • Oracle忘记所有密码怎么办
  • JS事件代理(事件委托)
  • react 字轮播滚动
  • 百度/抖音/小红书/微信搜索品牌形象优化怎么做?
  • Linux MIPI 调试中常见的问题
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • Qt_一个由单例引发的崩溃
  • Arkts开发UIAbility组件生命周期启动模式开发详解【鸿蒙专栏-19】
  • 工业机器视觉megauging(向光有光)使用说明书(五,轻量级的visionpro)
  • 【存储测试】fio存储性能测试工具
  • 流媒体方案之FFmepeg——实现物联网视频监控项目
  • 【【vivado ip 抽象成C语言的本质】】
  • 混合使用Windows和Linux子系统的工具和命令