Java SSM配置文件案例詳解
先對Spring SpringMVC和Mybatis單獨進行配置,最后對三者進行整合配置
Spring
實際使用中,一般會使用注解+xml配置來實現(xiàn)spring功能,其中xml配置對上文進行總結(jié),配置內(nèi)容如下
<?xml version="1.0" encoding="UTF-8"?>
<!--在使用spring 相關(guān)jar包的時候進行配置 每個jar包對應(yīng)一個xmlns和schemaLocation路徑-->
<!--格式基本相關(guān) 只要修改相關(guān)的關(guān)鍵字-->
<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)建事務(wù)管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入數(shù)據(jù)源-->
<property name="dataSource" ref="dataSource"></property>
<!--開啟事務(wù)注解-->
<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的初始化時間提前到服務(wù)器啟動時
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!--
設(shè)置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
若只設(shè)置該標簽,則只能訪問靜態(tài)資源,其他請求則無法訪問
此時必須設(shè)置<mvc:annotation-driven/>解決問題
-->
<mvc:default-servlet-handler/>
<!-- 開啟mvc注解驅(qū)動 -->
<mvc:annotation-driven>
<mvc:message-converters>
<!-- 處理響應(yīng)中文內(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&useUnicode=true&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)如果還沒有結(jié)果,看是否是自己的多層路徑的問題,有時候創(chuàng)建文件時候例如com.example.mapper不是三層路徑,可以展開看一下。
SSM整合
到此這篇關(guān)于Java SSM配置文件案例詳解的文章就介紹到這了,更多相關(guān)Java SSM配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)字符串倒序輸出的常用方法小結(jié)
這篇文章主要介紹了Java實現(xiàn)字符串倒序輸出的常用方法,通過三個實例從不同角度實現(xiàn)該功能,有不錯的借鑒價值,需要的朋友可以參考下2014-09-09
spring?boot?Mybatis?攔截器實現(xiàn)拼接sql和修改的代碼詳解
這篇文章主要介紹了spring?boot?Mybatis?攔截器實現(xiàn)拼接sql和修改,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
Struts2開發(fā)環(huán)境搭建 附簡單登錄功能實例
這篇文章主要介紹了Struts2開發(fā)環(huán)境搭建,為大家分享一個簡單登錄功能實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11

