spring项⽬篇2----编写配置⽂件
我们接下来开始编写我们的权限系统,以便进⼀步熟悉SSM,接下来按照第⼀篇起⼀个maven项⽬,项⽬简介如下,接下来看⼀下配置⽂件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"java.sun/dtd/web-app_2_3.dtd" >
<web-app xmlns="java.sun/xml/ns/javaee"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="java.sun/xml/ns/javaee
java.sun/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="false">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--配置前端控制器-->
<servlet>
<servlet-name>SpringMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置初始化参数-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!--加载的主配置⽂件-->
<param-value>l</param-value>
</init-param>
<!--设置项⽬启动时加载框架,如果不设置,就会在第⼀次发请求是加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--设置拦截路径-->
<servlet-mapping>
<servlet-name>SpringMvc</servlet-name>
<!--拦截所有-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置编码过滤器-->
<filter>
<filter-name>CharacterEncoding</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>
</filter>
<!--将所有路径进⾏解码-->
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
接下来看⼀下spring配置⽂件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd /schema/context /schema/context/spring-context.xsd"> <!--注解扫描,这是扫描的包路径-->
<context:component-scan base-package="com.yang"/>
<!--导⼊mybatis的配置-->
<import resource="classpath:application-mybatis"/>
<!--导⼊springMvc配置-->
<import resource="l"/>
</beans>
数据库属性⽂件
jdbc.sql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/promission?characterEncoding=utf-8
jdbc.username=root
jdbc.password=mysql
mybatis的配置⽂件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/aop
/schema/aop/spring-aop.xsd
/schema/tx
/schema/tx/spring-tx.xsd
">
<!--加载数据库属性⽂件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置德鲁伊连接吃-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="initialSize" value="5"/>
<!--属性⽂件中的名称不能跟property的name⼀样,因此⼀般这个属性⽂件名称前⾯会加上jdbc-->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--设置数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启注解事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--配置Mybatis⼯⼚-->
<bean id="sqlSessionFactoryBean" class="batis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--核⼼配置⽂件位置-->
<property name="configLocation" value="l"/>
<!--配置mapper映射⽂件的路径-->
<property name="mapperLocations" value="classpath:com.yang.mapper"/>
</bean>
<!--配置Mapper接⼝扫描-->
<bean class="batis.spring.mapper.MapperScannerConfigurer">
<!--设置mapper接⼝包的位置-->
<property name="basePackage" value="com.yang.mapper"/>
</bean>
</beans>
springMvc配置⽂件
<?xml version="1.0" encoding="UTF-8" ?>
bigdecimal格式化两位小数
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/mvc
/schema/mvc/spring-mvc-4.0.xsd
">
<!--主要就是为了Spring MVC来⽤的,提供Controller请求转发,json⾃动转换等功能-->
<mvc:annotation-driven />
<!--静态资源处理-->
<mvc:default-servlet-handler />
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置,先注释,到时候写的时候在进⾏配置-->
<!-- <mvc:interceptors>-->
<!-- <mvc:interceptor>-->
<!-- <mvc:mapping path="/*/" />-->
<!-- </mvc:interceptor>-->
<!-- </mvc:interceptors>-->
<!--配置⽂件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipartmons.CommonsMultipartResolver">
<!--设置上传最⼤⽂件⼤⼩-->
<property name="maxUploadSize">
<value>10240000</value>
</property>
</bean>
</beans>
sqlMapConfig
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--设置sql语句输出到终端-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<!-- 批量别名定义,扫描整个包下的类,别名为类名(⼤⼩写不敏感) -->
<package name="com.yang.domain"/>
</typeAliases>
<plugins>
<!--配置分页插件-->
<plugin interceptor="com.github.pagehelper.PageHelper"/>
</plugins>
</configuration>
最后看⼀下可以根据数据库帮助我们⽣成mapper接⼝以及xml⽂件的插件的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN"
"/dtd/mybatis-generator-config_1_0.dtd">
<!-- 配置⽣成器 -->
<generatorConfiguration>
<!--targetRuntime有两个参数⼀个是MyBatis3Simple只⽣成简单的增删改查,另外⼀个会⽣成很多操作,
但那些基本都是我们⾃⼰编写,⼀次⼀般只使⽤MyBatis3Simple-->
<context id="mysql" defaultModelType="hierarchical" targetRuntime="MyBatis3Simple">
<!-- ⾃动识别数据库关键字,
默认false,如果设置为true,
根据SqlReservedWords中定义的关键字列表;
⼀般保留默认值,
遇到数据库关键字(Java关键字),
使⽤columnOverride覆盖 -->
<property name="autoDelimitKeywords" value="false"/>
<!-- ⽣成的Java⽂件的编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 格式化java代码 -->
<property name="javaFormatter" value="ator.api.dom.DefaultJavaFormatter"/>
<!-- 格式化XML代码 -->
<property name="xmlFormatter" value="ator.api.dom.DefaultXmlFormatter"/>
<!-- beginningDelimiter和endingDelimiter:指明数据库的⽤于标记数据库对象名的符号,
⽐如ORACLE就是双引号,MYSQL默认是`反引号; -->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!--阻⽌⽣成⽇期和注释-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库连接信息 -->
<jdbcConnection driverClass="sql.jdbc.Driver" connectionURL="jdbc:mysql:///promission" userId="root" password="mysql"/> <!-- java类型处理器⽤于处理数据库中的类型到Java中的类型,
默认使⽤JavaTypeResolverDefaultImpl;
注意⼀点,默认会先尝试使⽤Integer,Long,Short等来对应DECIMAL和NUMERIC数据类型; -->
<javaTypeResolver type="pes.JavaTypeResolverDefaultImpl">
<!-- true:使⽤BigDecimal对应DECIMAL和 NUMERIC数据类型 false:默认,
scale>0;length>18:使⽤BigDecimal;
scale=0;length[10,18]:使⽤Long;
scale=0;length[5,9]:使⽤Integer;
scale=0;length<5:使⽤Short; -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--
配置domain⽣成策略
targetProject:把⾃动⽣成的domain放在哪个⼯程⾥⾯
targetPackage:哪个包下
-->
<javaModelGenerator targetPackage="com.yang.domain" targetProject="src/main/java">
<!-- for MyBatis3/MyBatis3Simple
⾃动为每⼀个⽣成的类创建⼀个构造⽅法,
构造⽅法包含了所有的field;⽽不是使⽤setter; -->
<property name="constructorBased" value="false"/>
<!-- for MyBatis3 / MyBatis3Simple
是否创建⼀个不可变的类,如果为true,
那么MBG会创建⼀个没有setter⽅法的类,
取⽽代之的是类似constructorBased的类 -->
<property name="immutable" value="false"/>
<!-- 设置是否在getter⽅法中,
对String类型字段调⽤trim()⽅法 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--
配置l的⽣成策略
targetPackage:把⾃动⽣成的mapper放在哪个⼯程⾥⾯
targetProject:哪个包下
-->
<sqlMapGenerator targetPackage="com.yang.mapper"
targetProject="src/main/java">
<!-- 在targetPackage的基础上,
根据数据库的schema再⽣成⼀层package,
最终⽣成的类放在这个package下,默认为false -->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--mapper接⼝⽣成策略-->
<javaClientGenerator targetPackage="com.yang.mapper" type="XMLMAPPER" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--指定⽣成那个表-->
<table tableName="user"></table>
</context>
</generatorConfiguration>
⽂件结尾,我们看⼀下maven的项⽬依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yang</groupId>
<artifactId>promissionPro</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>promissionPro Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>ample</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mavenpiler.source>1.8</mavenpiler.source>
<mavenpiler.target>1.8</mavenpiler.target>
<!--定义版本号 ${org.springframework.version}-->
<org.springframework.version>5.2.3.RELEASE</org.springframework.version>
&batis.version>3.5.3</batis.version>
</properties>
<dependencies>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--lombok-->
<!--Lombok能通过注解的⽅式,在编译时⾃动为属性⽣成构造器、getter/setter、equals、hashcode、toString⽅法。--> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<!-- servlet-api -->
<!--⽤到HttpServletRequest和HttpServletResponse等对象,这些对象都是要靠这个jar包才能使⽤的-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- druid -->
<!--阿⾥云德鲁伊连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!-- common-lang -->
<!--commons-lang主要⽬的是提供更多的⼯具类,其实也就是⽅便我们的开发--> <dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!--spring 框架-->
<!--spring-test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<!--spring-core-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring-context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring-context-support-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring-expression-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring-tx-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring-web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring-aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.5</version>
</dependency>
<!--aspectj weaver-->
<dependency>
<groupId>org.aspectj</groupId>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论