- ssm:基本是整合spring+springmvc整合mybatis
- spring:主要构成是ioc和di注入,也就是控制反转和值的注入
- springmvc:一个概念模型视图和控制
- mybatis:操作数据库的利器
ssm整合主要是配置文件的整理,主要讲解配置文件
环境:idea 2022,
mysql 8.0
maven 3.8.x
首先添加依赖 maven
<!--mybatis整合spring 依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- mysql依赖,版本可选 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!-- springmvc依赖 包含了spring依赖不必再添加spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--spring 链接依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<!-- 数据库链接依赖-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
上面是需要整合的所有依赖
然后添加mybatis主配置文件
放在resource目录下:
mybatis配置文件的所有东西已经放在spring配置文件里面这个目录可以不加
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>
<typeAliases>
<!-- 起别名操作-->
<package name="com.hqyj.hyx.pojo"/>
</typeAliases>
</configuration>
接下来是spring配置文件,同样也在resource目录下
<?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注解扫描 并忽略controller-->
<context:component-scan base-package="com.hqyj.hyx">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 导入jdbc配置文件-->
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<!-- 配置数据库的参数-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 配置mybatis相关的配置 mybatis所有配置都整合到这-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations">
<list>
<!-- 这是配置mapper的映射文件位置 , 根路径是resouce目录-->
<value>classpath:mapper/UserDao.xml</value>
<value>classpath:mapper/ProviderDao.xml</value>
<value>classpath:mapper/BillDao.xml</value>
<value>classpath:mapper/CustomerDao.xml</value>
</list>
</property>
<!-- 引入mybatis主配置文件,如果配置文件已删除没必要添加这个-->
<property name="configLocation" value="classpath:mybatis.xml"/>
<!--mybatis分页插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor"/>
</array>
</property>
</bean>
<!-- mybatis 的mapper层配置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
<property name="basePackage" value="com.hqyj.hyx.dao"/>
</bean>
</beans>
大家看好resources目录的结构
jabc.properties文件如下
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3307/数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username=root
password=333333 #密码
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.hqyj.hyx.controller"/>
<mvc:annotation-driven/>
<!--这是放行静态资源的配置,如果没有目录会报错不用管-->
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/static/**" location="/static/"/>
<mvc:resources mapping="/template/**" location="/template/"/>
<!--这是controller 视图解析的配置 如果controller 返回index 那就再/WEB-INF/jsp/下找index.jsp-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5000000"/>
</bean>
</beans>
ssm肯定涉及前端页面所以需要在web.xml 做点配置
<?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">
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
<!-- 加载spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.hqyj.hyx.config.MyAppContextListener</listener-class>
</listener>
<!--所有的非jsp请求交给dispatcherServlet处理-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--处理编码格式,所有的请求处理格式为utf-8-->
<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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>filterController</filter-name>
<filter-class>com.hqyj.hyx.controller.FilterController</filter-class>
</filter>
<filter-mapping>
<filter-name>filterController</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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.hqyj.hyx.dao.UserDao">
</mapper>
- 到这里ssm整合结束了,来一个案例测试一下环境
这是一个controller层代码,运行成功,成功访问
@RequestMapping("/findAllUser")
public HashMap<String, Object> findAllUser(HttpServletRequest request,User user,Integer id,String userName){
return userService.findAllUser(request,user,id,userName);
}
service代码
@Override
public HashMap<String, Object> findAllUser(HttpServletRequest request,User user,Integer id,String userName) {
HashMap<String, Object> map = new HashMap<>();
if(id != null || userName != null){
user.setId(id);
user.setUserName(userName);
}
PageHelper.startPage(user.getTotal(),user.getPageSize());
List<User> users;
if(user.getId() != null || user.getUserName() != null || !"".equals(user.getUserName())){
users = userDao.fuzzyQuery(user.getId(), user.getUserName());
System.out.println("模糊查询!");
}else {
users= userDao.findAllUser();
}
PageInfo<User> pageInfo = new PageInfo<>(users);
//分页数据
map.put("users",pageInfo.getList());
//总页数
map.put("pages", pageInfo.getPages());
//总数居量
map.put("total",pageInfo.getTotal());
return map;
}
Mapper层
List<User> findAllUser();