广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java SSM配置文件案例详解
  • 681
分享到

Java SSM配置文件案例详解

2024-04-02 19:04:59 681人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

先对Spring SpringMVC和Mybatis单独进行配置,最后对三者进行整合配置 spring 实际使用中,一般会使用注解+xml配置来实现spring功能,其中xml配置对

先对Spring SpringMVC和Mybatis单独进行配置,最后对三者进行整合配置

spring

实际使用中,一般会使用注解+xml配置来实现spring功能,其中xml配置对上文进行总结,配置内容如下


<?xml version="1.0" encoding="UTF-8"?>
<!--在使用spring 相关jar包的时候进行配置 每个jar包对应一个xmlns和schemaLocation路径-->
<!--格式基本相关 只要修改相关的关键字-->
<beans xmlns="Http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx = "http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--    开启注解扫描 ,才可以使用注解 可以使用use-default-filter配合include-filer和exclude-filter使用    -->
    <context:component-scan base-package="com.jdbcTemplate" ></context:component-scan>

<!--    开启aop-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

<!--    JdbcTemplate使用-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="jdbc:Mysql:/book"/>
        <property name="username" value = "root"/>
        <property name="passWord" value = "LRY990722"/>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    </bean>

    <bean id="'jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    <bean/>


    <!--创建事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--注入数据源-->
    <property name="dataSource" ref="dataSource"></property>
        <!--开启事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager" ></tx:annotation-driven>

</bean>
</beans>

springMVC

web.xml配置文件


<!-- 配置Springmvc前端控制器,对浏览器发送的请求统一进行处理 -->
<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.WEB.servlet.DispatcherServlet</servlet-class>
    <!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 -->
    <init-param>
        <!-- contextConfigLocation为固定值 -->
        <param-name>contextConfigLocation</param-name>
        <!-- 使用classpath:表示从类路径查找配置文件,例如Maven工程中的src/main/resources -->
        <param-value>classpath:springMVC.xml</param-value>
    </init-param>
    <!-- 
 		作为框架的核心组件,在启动过程中有大量的初始化操作要做
		而这些操作放在第一次请求时才执行会严重影响访问速度
		因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时
	-->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <!--
        设置springMVC的核心控制器所能处理的请求的请求路径
        /所匹配的请求可以是/login或.html或.js或.CSS方式的请求路径
        但是/不能匹配.jsp请求路径的请求
    -->
    <url-pattern>/</url-pattern>
</servlet-mapping>

springMVC.xml配置文件


<!-- 自动扫描包 -->
<context:component-scan base-package="com.atguigu.mvc.controller"/>

<!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    <property name="order" value="1"/>
    <property name="characterEncoding" value="UTF-8"/>
    <property name="templateEngine">
        <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
            <property name="templateResolver">
                <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    
                    <!-- 视图前缀 -->
                    <property name="prefix" value="/WEB-INF/templates/"/>
    
                    <!-- 视图后缀 -->
                    <property name="suffix" value=".html"/>
                    <property name="templateMode" value="HTML5"/>
                    <property name="characterEncoding" value="UTF-8" />
                </bean>
            </property>
        </bean>
    </property>
</bean>

<!-- 
   处理静态资源,例如html、js、css、jpg
  若只设置该标签,则只能访问静态资源,其他请求则无法访问
  此时必须设置<mvc:annotation-driven/>解决问题
 -->
<mvc:default-servlet-handler/>

<!-- 开启mvc注解驱动 -->
<mvc:annotation-driven>
    <mvc:message-converters>
        <!-- 处理响应中文内容乱码 -->
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="defaultCharset" value="UTF-8" />
            <property name="supportedMediaTypes">
                <list>
                    <value>text/html</value>
                    <value>application/JSON</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

mybatis

Mybatis-config.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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="LRY990722"/>
            </dataSource>
        </environment>
    </environments>

<!--    mapper.xml都需要在mybatis核心配置文件中配置-->
    <mappers>
        <mapper resource="com/example/mapper/UserMapper.xml"/>
    </mappers>
</configuration>

mapper.xml中配置文件如下:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
    <select id="selectUser" resultType="com.example.pojo.User">
        select * from user
    </select>
</mapper>

有时候读取不到mapperl.xml配置的原因,提示java.io.IOException: Could not find resource,原因可能有
1)配置文件是否在Resource路径下;

2)如果在src/main/java目录下,需要在项目的pom.xml中加入;


    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>***.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>***.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

使得可以读取src/main/java下的xml和properti文件

3)如果还没有结果,看是否是自己的多层路径的问题,有时候创建文件时候例如com.example.mapper不是三层路径,可以展开看一下。

SSM整合

SSM整合配置

到此这篇关于Java SSM配置文件案例详解的文章就介绍到这了,更多相关Java SSM配置文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java SSM配置文件案例详解

本文链接: https://www.lsjlt.com/news/133948.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java SSM配置文件案例详解
    先对Spring SpringMVC和Mybatis单独进行配置,最后对三者进行整合配置 Spring 实际使用中,一般会使用注解+xml配置来实现spring功能,其中xml配置对...
    99+
    2022-11-12
  • Java sdk安装及配置案例详解
    1.安装Java SDK开发环境。 首先去官网下载Java SDK,http://www.oracle.com/technetwork/java/javase/downloads/j...
    99+
    2022-11-12
  • php之php.ini配置文件讲解案例
    [PHP]  ; PHP还是一个不断发展的工具,其功能还在不断地删减  ; 而php.ini的设置更改可以反映出相当的变化,  ; 在使用新的PHP版本...
    99+
    2022-11-12
  • Java JConsole远程连接配置案例详解
    JConsole远程连接还是有一点坑的。这里记录一下配置过程,好记性不如烂笔头。 1.在远程机的tomcat的catalina.sh中加入配置: JAVA_OPTS="$JAVA...
    99+
    2022-11-12
  • Python ini配置文件示例详解
    目录INI介绍关于configparserINI文件格式读取配置文件写入配置文件总结INI介绍 INI是英文“初始化”(initialization)的缩写,...
    99+
    2022-11-13
  • ssm框架下web项目,web.xml配置文件的作用(详解)
    1. web.xml中配置了CharacterEncodingFilter,配置这个是拦截所有的资源并设置好编号格式。encoding设置成utf-8就相当于request.setCharacterEncoding("UTF-8");for...
    99+
    2023-05-30
    ssm框架 web.xml 配置
  • Java 操作Properties配置文件详解
    1 简介:JDK提供的java.util.Properties类继承自Hashtable类并且实现了Map接口,是使用一种键值对的形式来保存属性集,其中键和值都是字符串类型。java.util.Properties类提供了getProper...
    99+
    2023-05-31
    java properties
  • SpringBoot中YAML配置文件实例详解
    目录一、YAML 简介1、什么是 YAML ?2、优点3、扩展名4、语法规则5、格式二、三种配置文件1、properties 类型2、yml 类型3、yaml 类型4、优先级三、YA...
    99+
    2023-05-15
    spring boot yaml yaml配置文件 springboot yaml配置文件
  • Nginx配置文件nginx.conf的基本配置实例详解
    目录前言1. Nginx配置样例2. Nginx负载均衡方式2.1 轮询2.2 权重2.3 Nginx解决集群共享session问题的方案3. Nginx动静分离(静态资源...
    99+
    2022-11-13
  • Java CharacterEncodingFilter过滤器的理解和配置案例详解
    在web项目中我们经常会遇到当前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题,这些问题的原因就是因为我们...
    99+
    2022-11-12
  • C#读写Config配置文件案例
    一、简介 应用程序配置文件(App.config)是标准的 XML 文件,XML 标记和属性是区分大小写的。它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用...
    99+
    2022-11-13
  • redis-配置文件详解
    1.基本配置daemonize no 是否以后台进程启动databases 16 创建database的数量(默认选中的是database 0) save 900 1 #刷新快照到硬盘中,必须满足两者...
    99+
    2022-10-18
  • package.json文件配置详解
    package.json 是npm init命令初始化后,在项目的根目录下自动生成的配置文件,它定义了这个项目的配置信息以及所需要的各种模块,npm install根据这个命令,自动下载所需的模块。pack...
    99+
    2022-06-04
    详解 文件 package
  • redis 配置文件详解
    bind 0.0.0.0                 #绑定redis服务器网卡IP,默认为127.0.0.1,即本地...
    99+
    2022-10-18
  • redis配置文件详解
    位置 find / -name redis.conf units单位 # Redis configuration file example. # # Note that in order to read the configu...
    99+
    2022-01-15
    redis配置文件详解
  • postgresql.conf配置文件详解
    postgresql.conf文件是PostgreSQL数据库系统的主配置文件,它包含了数据库服务器的各种配置选项。下面是postg...
    99+
    2023-09-13
    详解
  • prometheus 配置文件详解
    目录 prometheus 配置文件详解 简介配置文件 原始配置文件内容global字段alerting 字段 alert_relabel_configsalertmanagersrule_fi...
    99+
    2023-09-05
    prometheus 服务器 linux java 运维
  • SpringBoot多环境配置及配置文件分类实例详解
    目录二、配置文件分类2.1 代码演示2.1.1 环境准备2.1.2 验证1级和2级的优先级2.1.3 验证2级和4级的优先级一、多环境配置 在工作中,对于开发环境、测试环境、生产环境...
    99+
    2022-11-13
    SpringBoot多环境配置 SpringBoot配置文件
  • 详解java配置文件的路径问题
    详解java配置文件的路径问题各种语言都有自己所支持的配置文件,配置文件中有很多变量是经常改变的。不将程序中的各种变量写死,这样能更方便地脱离程序本身去修改相关变量设置。 那么我们需要读取配置文件,是需要获取配置文件的路径。那么配置文件的路...
    99+
    2023-05-31
    java 配置文件 ava
  • Redis2.8配置文件中文详解
    add by zhj : 没找到本文的原文。另外,redis配置文件中文翻译 也翻译的不错,可以与本文对照看。两篇文章都是以Redis2.8来介绍的 在Redis中直接启动redis-server服务时, ...
    99+
    2022-06-04
    中文 配置文件 详解
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作