亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java SSM配置文件案例詳解

 更新時間:2021年08月30日 11:02:03   作者:胖虎學Java  
這篇文章主要介紹了Java SSM配置文件案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下

先對Spring SpringMVC和Mybatis單獨進行配置,最后對三者進行整合配置

Spring

實際使用中,一般會使用注解+xml配置來實現(xiàn)spring功能,其中xml配置對上文進行總結,配置內(nèi)容如下

<?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/>


    <!--創(chuàng)建事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--注入數(shù)據(jù)源-->
    <property name="dataSource" ref="dataSource"></property>
        <!--開啟事務注解-->
    <tx:annotation-driven transaction-manager="transactionManager" ></tx:annotation-driven>

</bean>
</beans>

springMVC

web.xml配置文件

<!-- 配置SpringMVC的前端控制器,對瀏覽器發(fā)送的請求統(tǒng)一進行處理 -->
<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 通過初始化參數(shù)指定SpringMVC配置文件的位置和名稱 -->
    <init-param>
        <!-- contextConfigLocation為固定值 -->
        <param-name>contextConfigLocation</param-name>
        <!-- 使用classpath:表示從類路徑查找配置文件,例如maven工程中的src/main/resources -->
        <param-value>classpath:springMVC.xml</param-value>
    </init-param>
    <!-- 
 		作為框架的核心組件,在啟動過程中有大量的初始化操作要做
		而這些操作放在第一次請求時才執(zhí)行會嚴重影響訪問速度
		因此需要通過此標簽將啟動控制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>

<!-- 
   處理靜態(tài)資源,例如html、js、css、jpg
  若只設置該標簽,則只能訪問靜態(tài)資源,其他請求則無法訪問
  此時必須設置<mvc:annotation-driven/>解決問題
 -->
<mvc:default-servlet-handler/>

<!-- 開啟mvc注解驅(qū)動 -->
<mvc:annotation-driven>
    <mvc:message-converters>
        <!-- 處理響應中文內(nèi)容亂碼 -->
        <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)如果還沒有結果,看是否是自己的多層路徑的問題,有時候創(chuàng)建文件時候例如com.example.mapper不是三層路徑,可以展開看一下。

SSM整合

SSM整合配置

到此這篇關于Java SSM配置文件案例詳解的文章就介紹到這了,更多相關Java SSM配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Spring Boot配置動態(tài)更新問題

    Spring Boot配置動態(tài)更新問題

    這篇文章主要介紹了Spring Boot配置動態(tài)更新問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java實現(xiàn)字符串倒序輸出的常用方法小結

    Java實現(xiàn)字符串倒序輸出的常用方法小結

    這篇文章主要介紹了Java實現(xiàn)字符串倒序輸出的常用方法,通過三個實例從不同角度實現(xiàn)該功能,有不錯的借鑒價值,需要的朋友可以參考下
    2014-09-09
  • Fluent Mybatis 批量更新的使用

    Fluent Mybatis 批量更新的使用

    本文主要介紹了Fluent Mybatis 批量更新的使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 關于JSON解析中獲取不存在的key問題

    關于JSON解析中獲取不存在的key問題

    這篇文章主要介紹了關于JSON解析中獲取不存在的key問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Java接口RandomAccess全面了解

    Java接口RandomAccess全面了解

    下面小編就為大家?guī)硪黄狫ava接口RandomAccess全面了解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • Struts2修改上傳文件大小限制方法解析

    Struts2修改上傳文件大小限制方法解析

    這篇文章主要介紹了Struts2修改上傳文件大小限制的相關內(nèi)容,包括決定Struts2上傳文件大小的因素,具有一定參考價值,需要的朋友可以了解下。
    2017-09-09
  • java Mail郵件接收工具類

    java Mail郵件接收工具類

    這篇文章主要介紹了java Mail郵件接收工具類,本文直接給出類實現(xiàn)代碼和使用示例,需要的朋友可以參考下
    2015-02-02
  • spring?boot?Mybatis?攔截器實現(xiàn)拼接sql和修改的代碼詳解

    spring?boot?Mybatis?攔截器實現(xiàn)拼接sql和修改的代碼詳解

    這篇文章主要介紹了spring?boot?Mybatis?攔截器實現(xiàn)拼接sql和修改,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • Struts2開發(fā)環(huán)境搭建 附簡單登錄功能實例

    Struts2開發(fā)環(huán)境搭建 附簡單登錄功能實例

    這篇文章主要介紹了Struts2開發(fā)環(huán)境搭建,為大家分享一個簡單登錄功能實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 再談java回調(diào)函數(shù)

    再談java回調(diào)函數(shù)

    個人對于回調(diào)函數(shù)的理解就是回調(diào)函數(shù)就是回頭再調(diào)用的函數(shù),哈哈,下面我們來詳細探討下回調(diào)函數(shù)。
    2015-07-07

最新評論