🥙三、项目搭建
🥪3.1、pom文件导入
我们先创建一个Maven项目,默认创建一个Maven是必须熟稔于心的,还不会的话必须学到闭着眼睛都可以创建,接下来我们来导入一些依赖,这些依赖基本上是我们整个项目都会用得到的依赖。
<properties> <spring.version>5.0.8.RELEASE</spring.version> <shiro.version>1.5.2</shiro.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!--shiro 核心--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${shiro.version}</version> </dependency> <!--shiro 的 Web 模块--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>${shiro.version}</version> </dependency> <!--shiro 和 Spring 集成--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> <!--shiro 底层使用的 ehcache 缓存--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>${shiro.version}</version> </dependency> <!--shiro 依赖的日志包--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!--Freemarker 的 shiro 标签库--> <dependency> <groupId>net.mingsoft</groupId> <artifactId>shiro-freemarker-tags</artifactId> <version>1.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.19</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <!-- file upload part --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</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.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- 将freemarker等第三方库整合进Spring应用上下文的依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.22</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-spec</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.6.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>8080</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> <server>tomcat7</server> </configuration> </plugin> </plugins> </build> 复制代码
🥪3.2、SSM项目搭建
学过SSM项目的朋友们都知道,SSM项目最繁琐的地方在于他的配置文件,各种各样花里胡哨的配置文件真的很难记住,所以我的建议是准备一份通用的模板,需要用到的时候可以随时拿出来(虽然可能目前大部分的企业都不会用SSM来搭建,但是可能会有老项目)。我这边给出一份通用的,不需要你记住,但是看到需要马上反映出来,他这行配置配置的是啥。
🌯3.2.1、db.properties
db.properties
配置文件用于书写我们的数据库的四要素,我们将这些配置抽取到一个配置文件中,方便我们修改。
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///ssm_carbusiness?characterEncoding=utf-8&useSSL=false jdbc.username=root jdbc.password=1101121833 复制代码
🍗3.2.2、spring.xml
spring.xml
是spring的核心配置文件,他配置的主要是关联数据库文件、Bean的一些配置。
<?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:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 关联 db.properties 文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置 DataSource bean --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置 SqlSessionFactory bean --> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="cn.linstudy.domain"/> <property name="mapperLocations" value="classpath:cn/linstudy/mapper/*.xml"/> <!-- 注意其他配置 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置参数,一行配置一个,下面配的是合理化分页 --> <value> pageSizeZero=true reasonable=true </value> </property> </bean> </array> </property> </bean> <!-- 配置 SqlSessionFactory bean --> <!-- 关联主配置文件 目前可以不配置--> <!-- 配置别名 若不用别名,可以不配置 --> <!-- 配置数据源--> <!-- 关联 Mapper XML 可以不配置, 前提编译 Mapper 接口字节码文件与 Mapper XML 文件在一起,文件名也一样 --> <!-- 配置 Mapper 接口的实现类对象 --> <!-- 指定 Mapper 接口所在包 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.linstudy.mapper"/> </bean> <!-- 配置 IoC DI 注解解析器 , 让 Spring 帮我们创建业务接口的实现类对象, 完成字段注入 --> <context:component-scan base-package="cn.linstudy.service"/> <!-- 配置事务管理器 WHAT--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置增强, WHEN, 并关联上面 WHAT--> <tx:advice id="interceptor" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get" read-only="true"/> <tx:method name="select" read-only="true"/> <tx:method name="list" read-only="true"/> <tx:method name="query" read-only="true"/> <tx:method name="count" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置 AOP --> <aop:config> <aop:pointcut id="txPoint" expression="execution(* cn.linstudy.service.impl.*ServiceImpl.*(..))"/> <aop:advisor advice-ref="interceptor" pointcut-ref="txPoint"/> </aop:config> <!-- WHERE --> <!-- 关联 WHERE WHEN--> <!--开启shiro注解扫描--> <aop:config/> <!-- Pointcut advisor通知器, 会匹配所有加了shiro权限注解的方法 --> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean> </beans> 复制代码
🥩3.2.3、spring-mvc.xml
spring-mvc.xml
是springmvc的核心配置文件,主要配置的是视图解析器和Freemarker的一些配置,如果不需要的话可以把Freemarker的配置删除即可。
<?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"> <!-- IoC DI 注解解析器 配置扫描控制器 说人话,让 Spring 帮我们创建控制器对象, 并注入 --> <context:component-scan base-package="cn.linstudy"/> <!-- 配置 MVC 注解解析器, 时间注解, JSON 注解 --> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <mvc:interceptors> <mvc:interceptor> <!--静态资源和动态资源同时拦截--> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/getImage"/> <mvc:exclude-mapping path="/empLogin"/> <mvc:exclude-mapping path="/getEmailCode"/> <mvc:exclude-mapping path="/checkEmail"/> <mvc:exclude-mapping path="/checkUsername"/> <mvc:exclude-mapping path="/error"/> <mvc:exclude-mapping path="/checkEmailIsHave"/> <mvc:exclude-mapping path="/fronted/**"/> <bean class="cn.linstudy.interceptor.CheckLoginInterceptor"/> </mvc:interceptor> </mvc:interceptors> <mvc:default-servlet-handler/> <!-- 注册 FreeMarker 配置类 --> <bean class="cn.linstudy.shiro.ShiroFreeMarkerConfig"> <!-- 配置 FreeMarker 的文件编码 --> <property name="defaultEncoding" value="UTF-8"/> <!-- 配置 FreeMarker 寻找模板的路径 --> <property name="templateLoaderPath" value="/WEB-INF/views/"/> <property name="freemarkerSettings"> <props> <!-- 兼容模式 ,配了后不需要另外处理空值问题,时间格式除外 --> <prop key="classic_compatible">true</prop> <!-- 数字格式化 , 不会有,字符串的 --> <prop key="number_format">0.##</prop> </props> </property> </bean> <!-- 注册 FreeMarker 视图解析器 --> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <!-- 是否把session中的attribute复制到模板的属性集中,可以使用FreeMarker的表达式来访问并显示--> <property name="exposeSessionAttributes" value="true"/> <!-- 配置逻辑视图自动添加的后缀名 --> <property name="suffix" value=".ftl"/> <!-- 配置响应头中 Content-Type 的指 --> <property name="contentType" value="text/html;charset=UTF-8"/> </bean> <!--文件上传解析器 id必须是multipartResolver--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--最大上传文件大小10M--> <property name="maxUploadSize" value="#{1024*1024*10}"/> </bean> <!-- 引入 spring.xml --> <import resource="classpath:spring.xml"/> <!-- 引入shiro的配置文件--> <import resource="classpath:shiro.xml"/> </beans> 复制代码
🍖3.2.4、mybatis-config.xml
mybatis-config.xml
这个配置文件是一个全局配置文件,这里面是对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> </configuration> 复制代码
🥟3.2.5、log4j.properties
log4j.properties
配置文件主要是配置我们日志相关的信息,让他更合理的显示在控制台,方便我们查看和排错。
# Global logging configuration log4j.rootLogger=ERROR, stdout log4j.logger.cn=TRACE # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
🍠3.2.5、总结
到这里,我们的SSM项目就彻底搭建完成了,正式开启我们写项目