Maven项目的创建及SpringMVC的配置:https://blog.csdn.net/qq_46062119/article/details/120373122?spm=1001.2014.3001.5501
加载完Maven之后,在pom.xml中里引入所需依赖: 注:由于 Maven 的传递性,我们不必将所有需要的包全部配置依赖,而是配置最顶端的依赖,其他靠传递性导入。
org.springframework
spring-webmvc
5.3.1
ch.qos.logback
logback-classic
1.2.3
javax.servlet
javax.servlet-api
3.1.0
provided
org.thymeleaf
thymeleaf-spring5
3.0.12.RELEASE
org.mybatis
mybatis
3.5.6
mysql
mysql-connector-java
5.1.38
二.web.xml基础配置
打开web.xml,配置编码过滤器及SpringMVC前端控制器:
三.搭建MyBatis框架CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceResponseEncoding true CharacterEncodingFilter /* springMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springMVC.xml 1 springMVC /
- mybatis框架需与数据库关联,所以在搭建框架之前,需先创建数据库:
create database db_springmvc -- 创建tb_employee表 create table tb_employee ( id int(11) primary key AUTO_INCREMENT, -- 主键id eno varchar(11) NULL, -- 员工编号 name varchar(55) NULL, -- 员工姓名 gender varchar(2) NULL, -- 员工性别 age varchar(12) NULL, -- 员工年龄 email varchar(255) NULL -- 员工邮箱 )
- 在resources文件下新建一个properties文件(dbconfig.properties)建立数据库连接:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/db_springmvc?characterEncoding=UTF-8 jdbc.username=root jdbc.password=
- 在resources新建一个xml文件(mybatis-config.xml)配置mybatis:
四.创建所需要的相关文件
①:实体类,属性需与数据库字段对应;
②:控制类,用于方法实现与页面跳转;
③:接口,用于编写与所需实现的SQL相关的方法;
④:服务类,用于实现接口方法;
⑤:工具类,用于获取sqlSession实例;
⑥:SQL映射文件,需与接口全类名相同;
⑦:用于html页面存放,文件名需与SpringMVC中的配置对应
编写mapper接口:
package com.jfeng.springmvc_mybatis.dao;
import com.jfeng.springmvc_mybatis.bean.Employee;
import java.util.List;
public interface EmployeeMapper {
//查询所有员工信息
public List query();
//根据员工id查询员工信息
public Employee getEmpById(Integer id);
//添加员工
public void add(Employee employee);
//修改员工信息
public void edit(Employee employee);
//删除员工
public void del(Integer id);
}
编写SQL映射文件:
select * from tb_employee
编写工具类,获取sqlSession实例:
package com.jfeng.springmvc_mybatis.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
public class DButil {
public SqlSession getopenSession() throws Exception {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSession = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSession.openSession();
return openSession;
}
}
编写service,实现mapper接口方法:
package com.jfeng.springmvc_mybatis.service;
import com.jfeng.springmvc_mybatis.bean.Employee;
import com.jfeng.springmvc_mybatis.dao.EmployeeMapper;
import com.jfeng.springmvc_mybatis.util.DButil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmployeeService extends DButil {
public List selectAll() throws Exception {
SqlSession session = getopenSession();
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
List employees = mapper.query();
return employees;
} finally {
session.close();
}
}
public void del(Integer id) throws Exception {
SqlSession session = getopenSession();
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
mapper.del(id);
session.commit();
} finally {
session.close();
}
}
public void add(Employee employee) throws Exception {
SqlSession session = getopenSession();
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
mapper.add(employee);
session.commit();
} finally {
session.close();
}
}
public void edit(Employee employee) throws Exception {
SqlSession session = getopenSession();
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
mapper.edit(employee);
session.commit();
} finally {
session.close();
}
}
public Employee getEmpById(Integer id) throws Exception {
SqlSession session = getopenSession();
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
return mapper.getEmpById(id);
} finally {
session.close();
}
}
}
新建前台页面:
编写index.html页面:
Title
全部员工信息
编写showAll.html页面:
员工列表
| # | 姓名 | 性别 | 年龄 | 邮箱 | 操作 |
编写addEmps.html页面:
Title
编写updateEmp.xml页面:
Title
编写Controller,实现所需功能,并进行页面跳转:
package com.jfeng.springmvc_mybatis.controller;
import com.jfeng.springmvc_mybatis.bean.Employee;
import com.jfeng.springmvc_mybatis.service.EmployeeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Map;
@Controller
public class EmployeeController {
EmployeeService service = new EmployeeService();
//实现对首页的访问,在请求控制器中创建处理请求的方法
@RequestMapping("/")
public String index() {
return "index";
}
//实现addEmps.html添加员工页面的跳转
@RequestMapping("/insert")
public String insert() {
return "addEmps";
}
//实现查询,并返回到前台页面showAll.html
@RequestMapping("/queryAll")
public String selectAll(Map map) throws Exception {
List emps = service.selectAll();
map.put("getemps", emps);
return "showAll";
}
//实现添加,并重定向到查询方法
@RequestMapping("/addemps")
public String add(Employee employee) throws Exception {
service.add(employee);
return "redirect:/queryAll";
}
//实现删除,并重定向到查询方法
@RequestMapping("/del")
public String del(@RequestParam(value = "id")Integer id) throws Exception {
service.del(id);
return "redirect:/queryAll";
}
//实现根据id查询,并返回到前台页面updateEmp.html
@RequestMapping("/getEmpById")
public String getEmpById(@RequestParam(value = "id")Integer id, Map map) throws Exception {
Employee emp = service.getEmpById(id);
map.put("getEmp", emp);
return "updateEmp";
}
//实现修改,并重定向到查询方法
@RequestMapping("/edit")
public String edit(Employee employee) throws Exception {
service.edit(employee);
return "redirect:/queryAll";
}
}
五.配置服务器
IDEA配置本地Tomcat服务器
六.测试
经测试,增删改查均能实现,并且数据库中也有相应改变。



