日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

使用Spring MVC,Mybatis框架等创建Java Web项目时各种前期准备的配置文件内容

發(fā)布時(shí)間:2023/12/13 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Spring MVC,Mybatis框架等创建Java Web项目时各种前期准备的配置文件内容 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、pom.xml

首先,pom.xml文件,里面包含各種maven的依賴,代碼如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.suixue.test</groupId><artifactId>maven-demo-web</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>maven-demo-web Maven Webapp</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring.version>4.1.4.RELEASE</spring.version><jackson.version>2.5.0</jackson.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</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-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version><scope>test</scope></dependency><!-- mybatis 包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.2.8</version></dependency><!--mybatis spring 插件 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.2.2</version></dependency><!-- mysql連接 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.34</version></dependency><!-- 數(shù)據(jù)源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.12</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.4</version></dependency><!-- log4j --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- servlet --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>3.0-alpha-1</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- json --><dependency><groupId>org.codehaus.jackson</groupId><artifactId>jackson-mapper-asl</artifactId><version>1.9.13</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><!-- 文件上傳 --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.2.2</version></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>4.0.1</version> </dependency></dependencies><build><finalName>system</finalName></build><repositories><repository><id>sonatype-nexus-snapshots</id><name>Sonatype Nexus Snapshots</name><url>http://oss.sonatype.org/content/repositories/snapshots</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories> </project> pom.xml

2、web.xml

完成第一步后,系統(tǒng)通常會(huì)自動(dòng)下載相應(yīng)的依賴包,然后,編寫web.xml中的內(nèi)容,代碼如下:

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>Archetype Created Web Application</display-name><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value> </context-param><!-- 字符集 過濾器 --><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><listener-class>org.springframework.web.context.ContextCleanupListener</listener-class></listener><servlet><servlet-name>mvc-dispather</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 改變默認(rèn)的上下文配置 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:mvc-dispather-servlet.xml,classpath:spring-mybatis.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>mvc-dispather</servlet-name><!-- 攔截所有請求 --><url-pattern>/</url-pattern></servlet-mapping><!-- 配置session超時(shí)時(shí)間為2小時(shí) --><session-config><session-timeout>120</session-timeout></session-config> </web-app> web.xml

在web.xml文件中,紅線圈出的幾個(gè)文件具體的配置將在后面給出。

3、resources資源目錄下各文件的配置

(1)spring.xml

也是在web.xml文件中所涉及到的配置文件,下面給出相應(yīng)代碼

?

<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"><!--引入配置屬性文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:/config.properties" /> <context:property-placeholder ignore-unresolvable="true" location="classpath:/mysite.properties" /> <!--自動(dòng)掃描含有@Service將其注入為bean --> <context:component-scan base-package="com.suixue.*.service" /> </beans> spring.xml

?

此處的兩種配置文件,將在下面詳細(xì)給出。

--》config.properties

#mysql version database druid setting validationQuery=SELECT 1 jdbc.jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/movie_management?useUnicode=true&characterEncoding=utf-8 jdbc.username=suixue jdbc.password=123456

注意更換此文件中的數(shù)據(jù)庫名稱以及username和password

--》mysite.properties

adminPath=system/a/

只有此一行代碼,此處為瀏覽器訪問頁面時(shí)url的前綴,比如localhost:8080/system/a/login

(2)spring-mybatis.xml

同時(shí)是web.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:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd "><!-- 配置數(shù)據(jù)源 使用的是Druid數(shù)據(jù)源 --><bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close"><property name="url" value="jdbc:mysql://localhost:3306/movie_management" /><property name="username" value="suixue" /><property name="password" value="123456" /><!-- 初始化連接大小 --><property name="initialSize" value="0" /><!-- 連接池最大使用連接數(shù)量 --><property name="maxActive" value="20" /><!-- 連接池最小空閑 --><property name="minIdle" value="0" /><!-- 獲取連接最大等待時(shí)間 --><property name="maxWait" value="60000" /><property name="poolPreparedStatements" value="true" /><property name="maxPoolPreparedStatementPerConnectionSize"value="33" /><!-- 用來檢測有效sql --><property name="validationQuery" value="${validationQuery}" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><property name="testWhileIdle" value="true" /><!-- 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 --><property name="minEvictableIdleTimeMillis" value="25200000" /><!-- 打開removeAbandoned功能 --><property name="removeAbandoned" value="true" /><!-- 1800秒,也就是30分鐘 --><property name="removeAbandonedTimeout" value="1800" /><!-- 關(guān)閉abanded連接時(shí)輸出錯(cuò)誤日志 --><property name="logAbandoned" value="true" /><!-- 監(jiān)控?cái)?shù)據(jù)庫 --><property name="filters" value="mergeStat" /></bean><!-- myBatis文件 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自動(dòng)掃描entity目錄, 省掉Configuration.xml里的手工配置 --><property name="mapperLocations" value="classpath:com/suixue/mapper/*.xml" /><property name="plugins"><array><bean class="com.github.pagehelper.PageHelper"><property name="properties"><value>dialect=mysqlrowBoundsWithCount=trueoffsetAsPageNum=true</value></property></bean></array></property></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.suixue.**.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /></bean><!-- 配置事務(wù)管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 注解方式配置事物 --><!-- <tx:annotation-driven transaction-manager="transactionManager" /> --><!-- 攔截器方式配置事物 --><tx:advice id="transactionAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="get*" propagation="SUPPORTS" read-only="true" /><tx:method name="find*" propagation="SUPPORTS" read-only="true" /><tx:method name="select*" propagation="SUPPORTS" read-only="true" /></tx:attributes></tx:advice><!-- Spring aop事務(wù)管理 --><aop:config><aop:pointcut id="transactionPointcut" expression="execution(* com..*service.*(..))" /><aop:advisor pointcut-ref="transactionPointcut"advice-ref="transactionAdvice" /></aop:config></beans> spring-mybatis

應(yīng)注意此處應(yīng)改成自己的數(shù)據(jù)庫名稱以及登錄名和密碼

此處路徑應(yīng)改成自己相應(yīng)的mapper路徑,匹配其下面所有的xml文件,這里所說的xml文件中,包含的是sql語句,類似于下面的內(nèi)容

另外,spring-mybatis.xml文件中下面紅色的部分應(yīng)對應(yīng)自己項(xiàng)目中的dao層接口,以使得上面剛剛提到的mapper中的xml(包含sql語句)與dao層下的文件相映射起來。

此例子中com.suixue.**.dao包下面包含java文件,該java文件不是一個(gè)類class,而是一個(gè)接口。

下面給出我一個(gè)項(xiàng)目中的對應(yīng)關(guān)系,方便理解。

另外,還需要注意的是

此處限制了涉及數(shù)據(jù)庫增刪改查操作的時(shí)候方法名的命名限制。

?此處應(yīng)對應(yīng)好自己項(xiàng)目中的service方法,有固定語法,不了解的地方請自己查閱相關(guān)資料。

?

這是我的service文件的結(jié)構(gòu)信息。

(3)mvc-dispather-servlet.xml

同樣是在web.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:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 激活標(biāo)注 --><context:annotation-config></context:annotation-config><!-- DispatherContext上下文,只搜索標(biāo)注了@Controller的類 --><context:component-scan base-package="com.suixue.*"></context:component-scan><mvc:annotation-driven /><!-- 啟用基于mvc的HandlerMapping --><mvc:default-servlet-handler /><mvc:interceptors><mvc:interceptor><!-- 所有文件夾及其子文件夾 --><mvc:mapping path="/system/*" /><bean id="loginInterceptor" class=" com.suixue.system.common.LoginInterceptor"></bean></mvc:interceptor></mvc:interceptors><!-- 避免IE執(zhí)行AJAX時(shí),返回JSON出現(xiàn)下載文件 --><bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value></list></property></bean><!-- 啟動(dòng)Spring MVC的注解功能,完成請求和注解POJO的映射 --><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><!-- json轉(zhuǎn)換器 --><ref bean="mappingJacksonHttpMessageConverter" /></list></property></bean><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView"></property><property name="prefix" value="/WEB-INF/jsps/" /><property name="suffix" value=".jsp" /></bean> </beans> mvc-dispather-servlet.xml

注意紅色區(qū)域部分,做出相應(yīng)更改,其中最后的bean配置中的class,改成自己對應(yīng)的bean即可,下面給出我的LoginInterceptor.java文件的具體代碼

package com.suixue.system.common;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import com.suixue.system.user.entity.User;public class LoginInterceptor extends HandlerInterceptorAdapter{private static final String[] LOGIN_URL = {"/login.form","login"};@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {request.setCharacterEncoding("UTF-8");String url = request.getRequestURI();boolean flag = false;for(String s : LOGIN_URL){if(url.contains(s)){flag = true;break;}}if(!flag){User user = (User) request.getSession().getAttribute("user");if(user == null){String context = request.getContextPath();response.sendRedirect(context + "/a/login");return false;}}return true;}} LoginInterceptor

此處設(shè)置前綴和后綴,根據(jù)需要可以自己更改。

?

?

當(dāng)當(dāng)當(dāng),至此,所有的基本配置到底就結(jié)束啦,若是有不足的地方,歡迎大家多多指正。

?

轉(zhuǎn)載于:https://www.cnblogs.com/suixue/p/5711151.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的使用Spring MVC,Mybatis框架等创建Java Web项目时各种前期准备的配置文件内容的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。