项目地址:
https://github.com/wannengdek/ssm_surd
SSM即Spring+springMvc+Mybatis的整合框架
Spring
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
mybatis
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
页面发送请求给控制器,控制器调用业务层处理逻辑,逻辑层向持久层发送请求,持久层与数据库交互,后将结果返回给业务层,业务层将处理逻辑发送给控制器,控制器再调用视图展现数据。
(来自百度百科)
开发一个ssm框架编写的增删改查项目.
1.项目搭建
创建一个maven工程
引入项目依赖的jar包
• spring
• springmvc
• mybatis
• 数据库连接池,驱动包
• 其他(jstl,servlet-api,junit)
引入bootstrap前端框架
编写ssm整合的关键配置文件
• web.xml,spring,springmvc,mybatis,使用mybatis的逆向工程生成对应的bean以
及mapper
测试mapper
创建maven工程便不在此赘述.下面给出pom.xml的配置
https://blog.csdn.net/qq_41852212/article/details/89601837 (pom.xml配置)
引入bootstrap前端框架
下载bootsrap
将解压下的文件放到项目中的webapp下即可
在使用过程中参照官方的文档提示就行了
编写ssm整合的关键配置文件
• web.xml,spring,springmvc,mybatis,
所用的配置文件均在这篇博客里
https://blog.csdn.net/qq_41852212/article/details/89601837
使用mybatis的逆向工程生成对应的bean以及mapper
使用junit进行单元测试
package com.dk.crud.test;
import java.io.IOException;
import java.util.UUID;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.dk.crud.bean.Department;
import com.dk.crud.bean.Employee;
import com.dk.crud.dao.DepartmentMapper;
import com.dk.crud.dao.EmployeeMapper;
@RunWith(SpringJUnit4ClassRunner.class )
@ContextConfiguration(value="classpath:applicationContext.xml")
public class MapperTest
{
@Autowired
DepartmentMapper departmentMapper;
@Autowired
EmployeeMapper employeeMapper;
@Autowired
SqlSession sqlSession;
/**
* 测试 Depmapper
* @throws IOException
*/
@Test
public void test1() throws IOException
{
// departmentMapper.insertSelective(new Department(null,"产品部"));
// departmentMapper.insertSelective(new Department(null,"测试部"));
//单个员工插入
employeeMapper.insertSelective(new Employee(null,"tom","M","jery12@qq.com",2));
System.out.println("插入成功");
//批量插入员工 使用可以批量执行的sqlSession
// EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
// System.out.println(1);
// System.out.println(departmentMapper);
// for(int i = 0;i<100;i++){
// String uid = UUID.randomUUID().toString().substring(0,5)+i;
// mapper.insertSelective(new Employee(null,uid, "M", uid+"@atguigu.com", 1));
// }
// System.out.println("批量完成");
}
}
数据能进到数据库中,即配置ssm成功.可以进行开发