SSM框架开发配置
SSM框架开发,配置太麻烦了,记录一下,方便之后的开发,仅仅只有相关的配置,里面有需要修改的地方, 不能直接用。
ch03-ssm
SpringMVC:视图层,界面层,负责接收请求,显示处理结果
Spring:业务层,管理Service、Dao、工具类对象
Mybatis:持久层,访问数据库
用户发起请求--springMVC接收--spring中的Service对象--mybatis处理数据
SSM整合
两个容器
1,SpringMVC,管理Controller控制器对象的
2,Spring,管理Service、Dao、工具类对象
我们要把使用的对象交给合适的容器创建、管理
两个容器是存在关系的,关系已经确定好了
SpringMVC容器是Spring容器的子容器,类似于java中的继承,子容器可以访问父容器中的对象
子容器中的Controller可以访问父容器中的Service对象
实现步骤
0. 使用数据库myd,表使用stu(id auto_increment, name,age)
1. 创建maven web项目
2. 加入依赖
3. 编写web.xml
1)注册DispatcherServlet
目的:1.创建SpringMVC容器,有了容器才能创建Controller类对象
2. 创建Servlet,接收用户的请求
2)注册Spring的监听器:ContextLoaderListener,
目的:创建Spring的容器对象,才能创建Service、Dao等对象
3)注册字符集过滤器,解决乱码问题
4. 创建包
5. 配置文件
1)springMVC配置文件
2)spring配置文件
3)mybatis配置文件
4)数据库配置文件
6. 写代码,dao接口、mapper映射文件、service接口和实现类、controller、实体类
7. 写jsp页面
1. 依赖引入
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- jsp依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<!-- spring-webmvc依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- spring 核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- spring事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- spring-jdbc依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- jackson依赖 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybaits依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<!-- mybaits 和 spring 集成的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- 阿里数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
<!-- 以下为其他的一些配置 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<!--原有资源路径-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
2. web.xml
1)注册DispatcherServlet
目的:
1.创建SpringMVC容器,有了容器才能创建Controller类对象
2. 创建Servlet,接收用户的请求
2)注册Spring的监听器:ContextLoaderListener,
目的:
创建Spring的容器对象,才能创建Service、Dao等对象
3)注册字符集过滤器,解决乱码问题
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 中央调度器 -->
<servlet>
<servlet-name>myweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myweb</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 注册监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring.xml</param-value>
</context-param>
<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>
</web-app>
3. Mybatis配置
3.1 Mybatis映射文件
<?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.zzs.dao.StudentDao">
<sql id="col"> id, name, email, age</sql>
<select id="selectStudents" resultType="Student">
select <include refid="col"></include> from student
</select>
<insert id="insertStudent">
insert into student( <include refid="col"></include> )
values( #{id},#{name},#{email},#{age} )
</insert>
</mapper>
3.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:控制mybatis全局行为 -->
<settings>
<!-- 设置mybatis输出日志 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 定义resultType返回类型的别名 -->
<typeAliases>
<!-- 该包下 使用类名作为别名 -->
<package name="com.zzs.entity"/>
</typeAliases>
<!-- 配置pageHelper插件 -->
<!--
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
-->
<!-- sql映射文件的位置 -->
<mappers>
<!--<mapper resource="com.zzs.dao.StudentDao.xml"/>-->
<package name="com.zzs.dao"/>
</mappers>
</configuration>
4. Spring配置
4.1 数据库配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/dbname
jdbc.username=root
jdbc.password=password
4.2 Spring配置文件
<?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
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- spring的配置文件:声明service、dao、工具类等对象 -->
<!-- 声明数据库配置文件的位置 -->
<context:property-placeholder location="classpath:conf/jdbc.properties"/>
<!-- 声明数据源,连接数据库 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- SqlSessionFactoryBean创建SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:conf/mybatis.xml" />
</bean>
<!-- 声明mybaits的扫描器,创建dao对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.zzs.dao" />
</bean>
<!-- 声明service的注解@Service所在包名的位置 -->
<context:component-scan base-package="com.zzs.service" />
<!-- 事务配置:注解的配置 或 aspectj的配置 二选一 -->
</beans>
5. SpringMVC
5.1 SpringMVC配置文件
配置组件扫描器和视图解析器
<?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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- springmvc配置文件 -->
<!-- 组件扫描器 -->
<context:component-scan base-package="com.zzs.controller" />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 注解驱动:ajax返回json、静态资源访问等 -->
<mvc:annotation-driven />
<mvc:resources mapping="/resource/**" location="/resource/" />
</beans>