SSM整合
Spring - Mybatis:将Mybatis的SqlSessionFactory交给Spring
Spring - SpringMVC:将Spring – SpringMVC各自配置一遍
步骤
1. jar包
2. 类 ( Student ) —— 表( student)
public class Student {
private int stuNo;
private String stuName;
private int stuAge;
//构造方法,Setter and Getter
}
3. 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">
<!-- 指定初始化Ioc容器(applicationContext.xml)的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4. db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///mp?serverTimezone=GMT
jdbc.username=root
jdbc.password=root
5. spring配置文件 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 https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 依赖注入,给service注入dao -->
<bean id="studentService" class="org.luo.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper" />
</bean>
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 加载mapper.xml路径 -->
<property name="mapperLocations" value="classpath:org/luo/mapper/StudentMapper.xml" />
</bean>
<!-- 将Mybatis的SqlSessionFactory交给Spring -->
<bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.luo.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
6. springmvc.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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 将控制器所在包加入IOC容器 -->
<context:component-scan base-package="org.luo.controller"></context:component-scan>
<!-- 配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- SpringMVC基础配置、标配-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
7. StudentMapper.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="org.luo.mapper.StudentMapper">
<select id="queryStuByNo" parameterType="int" resultType="org.luo.entity.Student">
select * from student where stuNo = #{stuNo}
</select>
</mapper>
8. StudentMapper.java
public interface StudentMapper {
Student queryStuByNo(int stuNo);
}
9. StudentService.java
public interface StudentService {
Student queryStuByNo(int stuNo);
}
10. StudentServiceImpl
public class StudentServiceImpl implements StudentService {
//service依赖于mapper
private StudentMapper studentMapper;
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
@Override
public Student queryStuByNo(int stuNo) {
return studentMapper.queryStuByNo(stuNo);
}
}
11. StudentConttoller.java
package org.luo.controller;
import org.luo.entity.Student;
import org.luo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
/*
* Created by 洛,Date: 2020/10/5,Time: 20:29
*/
@RequestMapping("controller")
@Controller
public class StudentController {
//控制器依赖于service
@Autowired
@Qualifier("studentService")
private StudentService studentService;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
@RequestMapping("queryStuByNo/{stuno}")
public String queryStuByNo(@PathVariable("stuno") Integer stuNo, Map<String,Object> map) {
Student student = studentService.queryStuByNo(stuNo);
map.put("student",student);
return "result";
}
}
11. 前台
index.jsp
<a href="controller/queryStuByNo/1">查询1号学生</a>
-------------------------
views/result.jsp
${requestScope.student.stuNo} -
${requestScope.student.stuName} -
${requestScope.student.stuAge} -