Java SSM配置文件案例详解


Posted in Java/Android onAugust 30, 2021

先对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>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <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配置文件内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Java/Android 相关文章推荐
分析Netty直接内存原理及应用
Jun 14 Java/Android
SpringBoot2 参数管理实践之入参出参与校验的方式
Jun 16 Java/Android
IDEA使用SpringAssistant插件创建SpringCloud项目
Jun 23 Java/Android
SpringCloud的JPA连接PostgreSql的教程
Jun 26 Java/Android
新手初学Java网络编程
Jul 07 Java/Android
springboot 多数据源配置不生效遇到的坑及解决
Nov 17 Java/Android
Android Rxjava3 使用场景详解
Apr 07 Java/Android
java中为什么说子类的构造方法默认访问的是父类的无参构造方法
Apr 13 Java/Android
详细介绍Java中的CyclicBarrier
Apr 13 Java/Android
Java实现超大Excel文件解析(XSSF,SXSSF,easyExcel)
Jul 15 Java/Android
IDEA中sout快捷键无效问题的解决方法
Jul 23 Java/Android
Java实现贪吃蛇游戏的示例代码
Sep 23 Java/Android
java调用Restful接口的三种方法
Aug 23 #Java/Android
JVM钩子函数的使用场景详解
Java中CyclicBarrier和CountDownLatch的用法与区别
Aug 23 #Java/Android
SpringBoot整合Mybatis Generator自动生成代码
Aug 23 #Java/Android
Java面试题冲刺第十九天--数据库(4)
Java获取e.printStackTrace()打印的信息方式
Aug 07 #Java/Android
Java移除无效括号的方法实现
Aug 07 #Java/Android
You might like
php判断类是否存在函数class_exists用法分析
2014/11/14 PHP
Smarty缓存机制实例详解【三种缓存方式】
2019/07/20 PHP
简单的无缝滚动程序-仅几行代码
2007/05/08 Javascript
Javascript isArray 数组类型检测函数
2009/10/08 Javascript
JQuery中getJSON的使用方法
2010/12/13 Javascript
ExtJS4 Grid改变单元格背景颜色及Column render学习
2013/02/06 Javascript
一个简单的瀑布流效果(主体形式自写)
2013/05/27 Javascript
关闭浏览器输入框自动补齐 兼容IE,FF,Chrome等主流浏览器
2014/02/11 Javascript
JavaScript中的setMilliseconds()方法使用详解
2015/06/11 Javascript
详解RequireJS按需加载样式文件
2017/04/12 Javascript
vue router仿天猫底部导航栏功能
2017/10/18 Javascript
vue组件jsx语法的具体使用
2018/05/21 Javascript
vue.js 实现评价五角星组件的实例代码
2018/08/13 Javascript
vue自定义js图片碎片轮播图切换效果的实现代码
2019/04/28 Javascript
jquery 插件重新绑定的处理方法分析
2019/11/23 jQuery
js回调函数仿360开机
2019/12/26 Javascript
JS eval代码快速解密实例解析
2020/04/23 Javascript
解决vue无法侦听数组及对象属性的变化问题
2020/07/17 Javascript
python通过文件头判断文件类型
2015/10/30 Python
基于Python中单例模式的几种实现方式及优化详解
2018/01/09 Python
Python3实现爬虫爬取赶集网列表功能【基于request和BeautifulSoup模块】
2018/12/05 Python
详解Python3网络爬虫(二):利用urllib.urlopen向有道翻译发送数据获得翻译结果
2019/05/07 Python
Python 3.8中实现functools.cached_property功能
2019/05/29 Python
python的命名规则知识点总结
2019/10/04 Python
python3 动态模块导入与全局变量使用实例
2019/12/22 Python
python虚拟环境模块venv使用及示例
2020/03/04 Python
在Anaconda3下使用清华镜像源安装TensorFlow(CPU版)
2020/04/19 Python
CSS 说明横向进度条最后显示文字的实现代码
2020/11/10 HTML / CSS
使用HTML5做个画图板的方法介绍
2013/05/03 HTML / CSS
html5实现多图片预览上传及点击可拖拽控件
2018/03/15 HTML / CSS
加拿大时尚床上用品零售商:QE Home | Quilts Etc
2018/01/22 全球购物
GUESS Factory加拿大:牛仔裤、服装及配饰
2019/09/20 全球购物
一套C#面试题
2013/10/09 面试题
生物化学研究助理员求职信
2013/10/09 职场文书
2019年怎样才能撰写出优秀的自荐信
2019/03/25 职场文书
css中有哪些方式可以隐藏页面元素及区别
2022/06/16 HTML / CSS