整合ssm(spring+springmvc+mybatis)框架
1、创建一个名为ssm的maven项目
2、连接mysql数据库以一张表为例
3、创建实体类与dao层、service层、controller层
4、导入maven依赖并且设置资源过滤
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>org.kuang</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!--德鲁伊连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.2</version>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!--jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<!--maven资源过滤-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
5、编写dao层接口与mapper.xml映射文件(接口类名与映射文件名应该相同,否则回报错)、service层
dao层()
studentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yang.dao.StudentDao">
<resultMap id="stu" type="com.yang.pojo.Student">
<id property="id" column="stuId"/>
<result property="name" column="stuName"/>
<result property="age" column="age"/>
<result property="classId" column="classId"/>
</resultMap>
<select id="queryStudentById" resultMap="stu">
select * from student where id=#{id};
</select>
<select id="queryAllStudent" resultMap="stu">
select * from student;
</select>
<delete id="deleteStudent" >
delete from student where id=#{id}
</delete>
<update id="updateStudent" parameterType="student" >
update student set stuName=#{name},age=#{age},classId=#{classId} where stuId=#{id};
</update>
<insert id="addStudent" parameterType="student">
insert into student(stuName,age,classId) values (#{name},#{age},#{classId})
</insert>
</mapper>
如果没有特殊的业务要求dao层接口与service层接口方法相同
serviceImpl
package com.yang.service;
import com.yang.dao.StudentDao;
import com.yang.pojo.Student;
import java.util.List;
/**
* @author hang yang
* @create 2021-10-09 21:46
*/
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
//实现get方法方便spring管理
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public Student queryStudentById(Integer id) {
return studentDao.queryStudentById(id);
}
public List<Student> queryAllStudent() {
return studentDao.queryAllStudent();
}
public int deleteStudent(Integer id) {
return studentDao.deleteStudent(id);
}
public int updateStudent(Student student) {
return studentDao.updateStudent(student);
}
public int addStudent(Student student) {
return studentDao.addStudent(student);
}
}
database.propertise
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123
jdbc.url=jdbc:mysql://localhost:3306/library?useSSL=true&useUnicode=true&characterEncoding=utf8
mybatis.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>
<typeAliases>
<package name="com.yang.pojo"/>
</typeAliases>
<mappers>
<mapper resource="com/yang/dao/studentMapper.xml"></mapper>
</mappers>
</configuration>
配置spring整合mybatis
spring-dao.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1、关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!--2、配置数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="password" value="${jdbc.password}"/>
<property name="username" value="${jdbc.username}"/>
</bean>
<!--3、配置sqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--配置mybatis.xml全局配置文件-->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!--4、配置扫描dao层包,动态实现dao接口注入到spring中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yang.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
spring-service.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
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描service相关的包-->
<context:component-scan base-package="com.yang.service"/>
<bean class="com.yang.service.StudentServiceImpl" id="StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
<!--配置事务管理-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
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">
<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:applicationContext.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>
<!--fileEncoding-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--配置springMvc-->
<!--1、开启SpringMvc注解驱动-->
<mvc:annotation-driven/>
<!--2、静态资源默认servlet配置-->
<mvc:default-servlet-handler/>
<!--3、配置jsp 显示ViewResolver视图解析器 InternalResourceViewResolver-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--扫描controller层-->
<context:component-scan base-package="com.yang.controller"/>
</beans>
测试类
package com.yang.dao;
import com.yang.pojo.Student;
import com.yang.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* @author hang yang
* @create 2021-10-09 21:43
*/
public class StudentDao {
public static StudentService getStudentService(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
return context.getBean("StudentServiceImpl",StudentService.class);
}
@Test
public void queryAllStudent(){
List<Student> students = getStudentService().queryAllStudent();
System.out.println(students);
}
@Test
public void queryStudentById(){
Student student = getStudentService().queryStudentById(1);
System.out.println(student);
}
@Test
public void addStudent(){
getStudentService().addStudent(new Student(null,"yang",23,1));
}
@Test
public void updateStudent(){
getStudentService().updateStudent(new Student(6,"hang",21,2));
}
@Test
public void deleteStudent(){
getStudentService().deleteStudent(6);
}
}
测试成功对数据库的操作成功,可以开始对controller层的代码编写
StudentController
package com.yang.controller;
import com.yang.pojo.Student;
import com.yang.service.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* @author hang yang
* @create 2021-10-10 10:15
*/
@Controller
public class StudentController {
//自动装配StudentServiceImpl
@Autowired
private StudentServiceImpl studentService;
@RequestMapping("/students")
public String queryAllStudent(Model model){
List<Student> students = studentService.queryAllStudent();
model.addAttribute("students",students);
return "studentlist";
}
}
index.jsp
<%--
Created by IntelliJ IDEA.
User: YANG HANG
Date: 2021/10/9
Time: 21:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h3><a href="${pageContext.request.contextPath}/students">跳</a></h3>
</body>
</html>
studentlist.jsp
<%--
Created by IntelliJ IDEA.
User: YANG HANG
Date: 2021/10/10
Time: 10:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${students}
</body>
</html>
页面效果
总结:到此简单的ssm的框架整合就到此结束了,说一下遇到的坑
1、遇到数据库字段名与实体类属性名不一致时可以采用resultType解决,在resultType中主键采用id,其他的采用properties。
2、dao层中的接口类名应该与mapper.xml文件名应该相同
3、其实ssm框架对应的是Java中的三层结构。mybatis对应的是数据持久层(dao),spring对应的是业务层(service),springmvc对应的是控制层与视图层
4、希望以后对ssm框架有更深的理解进行补充。