标题 SSM 搭建笔记
1.ssm的组成部分,spring +springmvc +mybatis
2.本教程使用环境为java8 idea以及maven
3.开始教程:
1使用idea创建maven web项目。 注意!一定要选择自己配置好的阿里云maven,否则会创建失败
2.在控制台看到build success视为创建成功,如果创建失败请参考第一条,第一次创建是加载速度较慢请耐心等待。
3.在创建完成后,找到pom.xml(项目依赖管理文件)在dependwencies中写入以下代码:
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- c3p0 数据库连接池 start -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- c3p0 数据库连接池 end -->
<!-- jsp-servlet支持 start -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- jsp-servlet支持 end -->
<!-- 标签库 start -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.1</version>
</dependency>
<!-- 标签库 end -->
<!-- apache的commons系列包 start -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<!-- apache的commons系列包 end -->
<!-- apache 工具包 start -->
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>
<!-- apache 工具包 end -->
<!-- spring start -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</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-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</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-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-websocket</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</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-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring start -->
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- mybatis start -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.0.1</version>
</dependency>
<!-- mybatis end -->
<!--日志管理 start-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>
<!--日志管理 end-->
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<!--getter setter 注解 start-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<!--getter setter 注解 end-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<!--json start-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.verson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.verson}</version>
</dependency>
<!--json end-->
4.在properties中填入如下代码:
<spring.version>4.0.6.RELEASE</spring.version>
<jackson.verson>2.8.7</jackson.verson>
5.在main文件夹下创建建Java(蓝色)源文件夹和resource(灰色带三杠)资源文件夹
6.在java源文件夹创建如下包:
*cn
*kgc
*ssm
*common(存放项目公共文件,比如统一返回类)
*controller(项目中的控制层)
*dao(项目的数据访问层)
*service(项目的业务逻辑层接口)
*impl(项目的业务逻辑层接口的实现类)
*util(项目中的工具类,比如分页工具类以及日期转换类 )
*vo(view-object视图对象,当页面的展示内容和实体类不符时使用vo类)
*entity(实体类,主要负责java中的属性和数据库字段一一对应 )
7.在resources文件夹下创建如下文件:
1.mapper(sql映射文件的集散地,这里的xml文件要和dao层中的接口一一对应) 2.appliacationContext.xml(spring配置文件,这里主要配置项目的数据源,会话工厂之类的)
3.applicationContext-mvc.xml(springmvc配置文件,这里主要配置mvc方向相关的)
4.db.properties(这里主要存放数据库相关的资源配置问题)
5.logback。xml(这里主要配置日志相关信息)
8.打开web。xml更换其头部信息 更换3.0是因为2.3不支持EL表达式的执行
<?xml version="1.0" encoding="UTF-8"?>9.在web.xml中写入代码
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--todo 上下文初始化,配置spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!--todo 配置spring mvc-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-mvc.xml</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>
10.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”
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">
<!-- todo 开启注解扫描-->
<context:component-scan base-package="cn.kgc"/>
<context:property-placeholder location="classpath*:db.properties"/>
<!--todo 配置数据源-->
<bean class="com.mchange.v2.c3p0.DriverManagerDataSource" id="dataSource">
<property name="driverClass" value="${jdbc.Driver}"/>
<property name="password" value="${jdbc.pwd}"/>
<property name="user" value="${jdbc.userName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
</bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"/>
<property name="typeAliasesPackage" value="cn.kgc.ssm.entity"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.kgc.ssm.dao"/>
</bean>
<!--todo -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- todo 由于总金额次联席没有涉及到增删改 所以不写声明式事务-->
11.applicationCntext-mvc 配置内容
<?xml version="1.0" encoding="UTF-8"?><!--todo 开启注解扫描-->
<context:component-scan base-package="cn.kgc"/>
<!--todo 配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:annotation-driven conversion-service="conversionService2"/>
<mvc:resources mapping="/statics/**" location="/statics/"/>
<bean id="conversionService2" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="cn.kgc.ssm.util.DataConverter"/>
</set>
</property>
</bean>
<!--配置文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="44554"/>
<property name="maxInMemorySize" value="54545"/>
</bean>
<!--todo 异常处理-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"/>
<property name="exceptionAttribute" value="e"/>
</bean>
4.项目中注解介绍
1.@service—主要使用在service层的实现类上用来标注当前层为service
2.@Controller–主要使用早controller层,标注当前类为控制层
3.@RequestMapping–此注解可用在类上也可用在方法上,作用是被标注的方法可用在处理网络请求。
4.ResponseBody–主要用在方法上,用来告诉前端控制器不走视图解析器,直接将数据返回给前端
5.@RequestParam-用来标注控制层参数,如果前端传递的参数名与控制层参数名不一致可用此注解,或者参数要求必填也可用此注解来强调
6.@pathVariable–用来从路径获取参数
7.@Slf4j—用在类上,当前类全类可使用log日志记录工具
8.@Resource–用来注入数据类型
9.@Param–当mybatis dao 层参数超过两个时使用此注解来区分参数