文章目录
- 前言
- 一、SSM框架搭建
- 1、 配置具体流程:
- 二、总结
前言
一、SSM框架搭建
1、 配置具体流程:
文件定义及意义:
配置文件及目录位置
- 1、pom.xml----引入项目jar包-------项目根目录
Pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 这里要自己创建的大项目名称,本人的是com.csh -->
<groupId>com.csh</groupId>
<!-- 这里要自己创建的子项目名称,本人的是CSH -->
<artifactId>CSH</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<!--======================= mybatis整合============================ -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- mybatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.6</version>
</dependency>
<!--======================= spring整合mybatis============================ -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!--======================= spring整合============================ -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!--======================= springmvc============================ -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
</project>
- 2、web.xml----项目入口文件-------WEB-INF文件夹下面
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 这里要自己创建的子项目名称,本人的是CSH -->
<display-name>CSH</display-name>
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<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:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 3、
spring-config.xml
-----spring配置文件-----resources文件夹下(需自己创建),这里需要注意的一点是mapper文件是以包的形式创建的。
Spring-config.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 这里要自己创建的大项目名称,本人的是com.csh -->
<context:component-scan base-package="com.csh">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务通知 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置SessionFactory -->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml" />
<!-- 这里要自己创建的大项目名称,本人的是com.csh -->
<property name="typeAliasesPackage" value="com.csh"></property>
</bean>
<!-- 配置扫描器,将myBatis接口的实现加入到ioc接口的实现中 -->
<!-- 通过注解形式配置 -->
<!-- 这里要自己创建的dao包的名称,本人的是com.csh.dao -->
<mybatis-spring:scan base-package="com.csh.dao"/>
</beans>
- 4、
springmvc-config.xml
-----springmvc配置文件-----resources文件夹下(需自己创建)
Springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 静态资源访问 -->
<mvc:default-servlet-handler />
<mvc:annotation-driven ></mvc:annotation-driven>
<!-- 这里要自己创建的controller包的名称,本人的是com.csh.dao -->
<context:component-scan base-package="com.csh.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 5、
mybatis-config.xml
----mybatis配置文件----resources文件夹下(需自己创建)
Mybatis.config.xml
<?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>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true" />
<setting name="aggressiveLazyLoading" value="false" />
<setting name="cacheEnabled" value="true"/>
<setting name="logImpl" value="LOG4J" />
</settings>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 参数合理化,当该参数设置为 true 时,pageNum<=0 时会查询第一页,
pageNum>pages(超过总数时),会查询最后一页。 -->
<property name="reasonable" value="true"/>
<property name="supportMethodsArguments" value="true"/>
<property name="params" value="pageNum=pageNum;pageSize=pageSize;"/>
</plugin>
</plugins>
</configuration>
- 6、
jdbc.properties
—连接jdbc数据库配置文件-----resources配置文件(需自己创建)
Jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/csh?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
jdbc.user=root
jdbc.password=1234
注:这里我的数据库名称为csh
- 7、
LoginMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.csh.dao.LoginDao">
<!-- 对数据库进行操作 -->
<select id="getUserByNameAndPass" resultType="users">
select * from users
where
username=#{username}
and password=#{password}
</select>
</mapper>
注:以上这文件是本人第一天需要创建配置的,其他文件都是后续才需要用的
包创建作用及位置:
- Step 1、controller包—放置XXController.java类—java下创建package包:com.姓名缩写.controller
- Step 2、service包–放置XXService.java接口文件–java下创建package包:com.姓名缩写.service
- Step 3、service.impl包–放置XXServiceImpl.java类文件–java下创建package包:com.姓名缩写.service.impl
- Step 4、dao包–放置XXDao.java接口文件–java文件夹创建package包:com.姓名缩写.dao
- Step 5、mapper文件夹–放置XXMapper.xml配置文件—resource下创建包mapper包
XXMapper.xml具体写法:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">\
<mapper namespace="com.csh.dao.XXDao">
</mapper>
到这里SSM框架就配置完成了,以上这些就是本人在参与项目培训过程中的第一天笔记
这里是关于SSM框架配置文件一些介绍
- 1、web.xml(入口)
springmvc-servlet (DispatcherServlet),spring-config(ContextLoaderListener)
辅助:编码格式(CharacterEncodingFilter) - 2、springmvc-servlet.xml(springmvc配置文件)
(类的驱动)context:component-scan —注解扫描(只扫描controller)
(方法的驱动)mvc:annotation-driven —springmvc注解驱动(选配,数据类型格式化,数据校验,国际化。。。) - 3、spring-config.xml(spring配置文件)
context:component-scan —注解扫描(不扫描controller)
c3p数据库连接池(ComboPooledDataSource)—jdbc.properties
DataSourceTransactionManager----事务
tx:annotation-driven—事务通知(注解)
SqlSessionFactoryBean----spring配置mybatis信息(数据库dataSource,mybatis而配置文件信息。。。。)
mybatis-spring:scan----将mybatis的xml配置文件加入到spring的ioc容器中 - 4、log4j.properties日志输出(辅助)
- 5、类文件
controller ----@Controller—注入serive(@Autowired)
service(seriviceImpl)------接口的实现类@Service—注入dao(@Autowired)
dao--------dao接口
mapper-----namespace引入dao接口,实现数据的增删改查 - 6、实现流程:
浏览器发送http请求地址url,进入springmvc的controller控制器,找到对应的请求方式@Requestmapping(“url”)-查询信息,结果返回到modelAndView中–通过视图解析器,返回页面。