CRM 02 配置文件
1. 导入依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<spring.version>5.2.8.RELEASE</spring.version>
</properties>
<dependencies>
<!--springmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<!--connection pool-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--JSTL-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--unit testing-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!--提供对office 文档的读写能力-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
</plugins>
</build>
</project>
2. mybatis的配置
<?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>
<settings>
<!--是否开启延迟加载,默认false-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--SQL日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--开启二级缓存默认是 true-->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
3. spring mvc的配置
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--声明注解驱动-->
<mvc:annotation-driven/>
<!--扫描控制器、异常处理器-->
<context:component-scan base-package="cn.ccut.**.web.*"/>
<!--静态资源管理,当访问以"/static/"开始的路径,则不去匹配RequestMapping,直接到指定的目录下去查询相应资源-->
<mvc:resources mapping="/static/**" location="/static/" cache-period="#{3600*24*10}"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp" />
<property name="suffix" value=".jsp" />
</bean>
<!--
站点入口
/WEB-INF/jsp/login.jsp
-->
<mvc:view-controller path="/" view-name="/login"/>
</beans>
4. applicationContext.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:context="http://www.springframework.org/schema/context"
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.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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--声明组件扫描器-->
<context:component-scan base-package="cn.ccut.**.service" />
<!--导入外部配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源druid-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--声明SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="cn.ccut.crm.mapper"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--声明MyBatis的扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="cn.ccut.**.mapper"/>
</bean>
<!--配置声明式事务-->
<bean id="tranManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置切面-->
<tx:advice id="myAdvice" transaction-manager="tranManager">
<tx:attributes>
<tx:method name="*" rollback-for="java.lang.Exception" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--配置AOP-->
<aop:config>
<!--指定切入点-->
<aop:pointcut id="allService" expression="execution(* *..service..*.*(..))"/>
<!--指定切面+指定切入点-->
<aop:advisor advice-ref="myAdvice" pointcut-ref="allService"/>
</aop:config>
</beans>
5. jdbc.properties
jdbc.url=jdbc:mysql://127.0.0.1:3306/crm
jdbc.username=root
jdbc.password=root
6. 配置页面处理器 // 页面访问到时 *.html,通过此可以转化为 *.jsp
项目中,所有的jsp文件,都存放在WEB-INF下,该目录是一个受保护的目录,其中的资源,不允许通过浏览器的地址栏直接访问,需要在Controller层添加一个RequestMapping进行映射,然后在方法中通过转发的形式来访问页面,但是由于项目中的页面很多,我们不想为每一个页面都提供一个单独的RequestMapping,我们需要对页面访问提供统一处理!思路:凡是页面的访问,后缀名统一使用.html来完成,我们可以添加一个RequestMapping("/**/.html")来匹配所有的页面访问,然后在方法中通过访问的地址,来确定选择哪个视图。
@Controller
public class PagerController {
@RequestMapping("/**/*.html")
public String pager(HttpServletRequest request){
// 获取请求的URI
String url=request.getRequestURI();
// 会走视图解析器
// 前缀 + url.substring(0,url.lastIndexOf("."))+后缀
return url.substring(0,url.lastIndexOf("."));
}
}
不停的思考,就会不停的进步