SSM整合
SSM整合
SSM整合的实现:查询studends表中所有用户信息
整合思路
第一步:整合dao层
mybatis和spring整合,通过spring管理mapper接口。
使用mapper的扫描器自动扫描mapper接口在spring中进行注册。
第二步:整合service层
通过spring管理service接口。
使用配置方式将service接口配置在spring配置文件中。
实现事务控制。
第三步:整合springMvc
由于springmvc是spring的模块,不需要整合。
数据准备
创建表student
| students | CREATE TABLE `students` (
`id` int(11) NOT NULL,
`name` varchar(10) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(5) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
表中添加数据
+----+------+------+------+
| id | name | age | sex |
+----+------+------+------+
| 1 | zs | 15 | nan |
| 2 | yk | 20 | NULL |
| 3 | 'dd' | 18 | nv |
| 4 | hmm | 27 | nan |
| 5 | yk | 25 | nan |
| 6 | jzx | 12 | NULL |
| 7 | zz | 15 | nan |
| 8 | tt | 30 | nv |
+----+------+------+------+
依赖Jar包
依赖jar中需要包含spring的核心jar mysql的驱动jar mybatis的相关jar spring和mybatis的整合jar
<!--mybatis和spring整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
全部的依赖
开发dao层
mybatis-config.xml
在resources目录下创建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>
<!-- 全局setting配置,根据需要添加 --> <!-- 配置mapper
由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。
但是必须遵循:mapper.xml和mapper.java文件同名且在一个目录
-->
<!-- <mappers></mappers> -->
</configuration>
mapper.java接口(StudentMapper.java)
@Repository
public interface StudentMapper {
public List<Student> selectAll();
}
Mapper.xml文件(StudentMapper.xml)
在配置文件目录resources下创建mapper目录,专门用来存储sql的配置文件
<?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.SSMTest.pojo.StudentMapper">
<select id="selectAll" resultType="com.SSMTest.bean.Student">
select * from students
</select>
</mapper>
实体类(Student.java)
public class Student {
private Integer id;
private String name;
private Integer age;
private String sex;
//此处省略setter、getter和重写toString方法
}
spring-core.xml(spring管理容器)
在resources目录下创建spring-core.xml文件,通过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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.SSMTest"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/person?useUnicode=true&characterEncoding=utf-8&useSSL=false"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--
sqlSessionFactory配置
org.mybatis.spring.SqlSessionFactoryBean类来实例化sqlSessionFactory对象
-->
<context:component-scan base-package="com.SSMTest"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--属性注入:数据源-->
<property name="dataSource" ref="dataSource"/>
<!--读取mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--读取mapper.xml文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--动态代理对象-->
<bean class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--注入会话工厂-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<!--注入接口-->
<property name="mapperInterface" value="com.SSMTest.pojo.StudentMapper"/>
</bean>
</beans>
Service层(StudentService.java 调用dao层获取数据)
Service层调用dao层获取数据,而dao层到数据库获取结果进行返回
@Service
public class StudentService {
//注入dao层依赖
@Autowired
private StudentMapper studentMapper;
public List<Student> getAllStudents(){
return studentMapper.selectAll();
}
}
Contrller层(StudentController.java)
Controller调用service层提供的相应功能的方法进行业务逻辑处理
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/studentList")
public ModelAndView studentList(){
List<Student> student = studentService.getAllStudents();
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("student",student);
modelAndView.setViewName("studentList");
return modelAndView;
}
}
前端页面(StudentList.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学生列表</title>
</head>
<body>
<table width="80%" border="1" align="center">
<h1 align="center">用户列表</h1>
<tr>
<td>学生ID</td>
<td>学生姓名</td>
<td>学生年龄</td>
<td>学生性别</td>
</tr>
<c:forEach items="${student }" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.sex}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
springmvc配置(spring-mvc.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!--扫描Controller等web注解-->
<context:component-scan base-package="com.SSMTest.Controller"/>
<!--采用注解方式,加载默认的处理器-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!--URL前缀-->
<property name="prefix" value="WEB-INF/jsp/"/>
<!--URL后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
配置web.xml文件
加载spring容器:Spring和mybatis的配置文件,配置springMVC前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 加载spring容器:Spring和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-core.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springMvc前端控制器配置 -->
<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:spring-mvc.xml</param-value>
</init-param>
<!-- load-on-startup:表示servlet随服务启动; -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--配置URL映射-->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
项目目录结构
查询结果
使用Demo