目录
Springboot整合Mybatis web小项目
1.在pom.xml导入相关的依赖.
2.建立数据库
3.编写实体类Entity
4.配置application.properties
5.Service层
6.Controller层
7.页面实现的效果
Mybatis相关的依赖
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.1
Mysql驱动相关的依赖
mysql
mysql-connector-java
5.1.47
runtime
Druid 连接池相关的依赖
com.alibaba
druid-spring-boot-starter
1.1.17
PageHelper 分页插件
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.13
建立部门表department
CREATE TABLE `department` (
`dept_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`dept_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '部门名称',
PRIMARY KEY (`dept_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1000 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '部门表' ROW_FORMAT = Dynamic;
建立员工表:Employee
CREATE TABLE `employee` (
`emp_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`emp_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '员工名',
`dept_id` int(11) NOT NULL COMMENT '关联部门表主键',
`gender` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '性别',
`email` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '邮箱',
`phone` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '手机号',
`hire_date` datetime(0) DEFAULT NULL COMMENT '入职时间',
PRIMARY KEY (`emp_id`) USING BTREE,
INDEX `dept_id`(`dept_id`) USING BTREE,
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`dept_id`) REFERENCES `department` (`dept_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 1008 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '员工表' ROW_FORMAT = Dynamic;
1.Department.java
public class Department {
private Integer deptId;
private String deptName;
private List employees;
public List getEmployees() {
return employees;
}
public void setEmployees(List employees) {
this.employees = employees;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString() {
return "Department{" +
"deptId=" + deptId +
", deptName='" + deptName + '\'' +
", employees=" + employees +
'}';
}
}
2.Employee.java
public class Employee {
private Integer empId;
private String empName;
private String gender;
private String email;
private String phone;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date hireDate;
//多对一
private Department department;
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"empId=" + empId +
", empName='" + empName + '\'' +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", hireDate=" + hireDate +
", department=" + department +
'}';
}
}
3.编写Mapper层
1.DepartmentMapper
@Mapper
@Repository
public interface DepartmentMapper {
/**
* 获取部门列表
* @return
*/
List getDepartmentList();
/**
* 根据部门ID查询部门信息
* @param deptId
* @return
*/
Department getDepartmentByDeptId(Integer deptId);
/**
* 根据部门ID查询部门详情
* @param deptId
* @return
*/
Department getDepartmentByDeptId_(Integer deptId);
}
2.EmployeeMapper
@Mapper
public interface EmployeeMapper {
/**
* 添加员工
* @param employee
*/
void addEmployee(Employee employee);
/**
* 删除员工
*/
void deleteEmployeeByEmpId(Integer empId);
/**
* 更新员工
*/
void updateEmployee(Employee employee);
/**
* 查询员工列表
* @return
*/
List getEmployeeList();
/**
* 根据员工ID查询员工信息
* @param empId
* @return
*/
Employee getEmployeeByEmpId(Integer empId);
}
3.配置mapper.xml文件
通过用xml文件注解的形式实现上面的接口
departmentMapper.xml
employeeMapper.xml
INSERT into employee
(emp_name,gender,email,phone,hire_date,dept_id)
VALUES
(#{empName},#{gender},#{email},#{phone},#{hireDate},#{department.deptId})
DELETE FROM employee WHERE emp_id=#{empId}
UPDATE employee
emp_name=#{empName},
gender=#{gender},
email=#{email},
phone=#{phone},
hire_date=#{hireDate},
dept_id=#{department.deptId}
emp_id=#{empId}
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/emp_manager?characterEncoding=utf-8&useSSL=false&unicode=true&serverTimezone=UTC
#mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.snow.entity
mybatis.configuration.map-underscore-to-camel-case=true
#thymeleaf
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
#json时间格式化配置
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
1.departmentService
public interface DeptService {
/**
* 获取部门列表
* @return
*/
List getDepartmentList();
}
2.employeeService
public interface EmpService {
/**
* 添加员工
* @param employee
*/
void addEmployee(Employee employee);
/**
* 删除员工
*/
void deleteEmployeeByEmpId(Integer empId);
/**
* 更新员工
*/
void updateEmployee(Employee employee);
/**
* 查询员工列表
* @return
*/
Page getEmployeeList(int pageNo,int pageSize);
/**
* 根据员工ID查询员工信息
* @param empId
* @return
*/
Employee getEmployeeByEmpId(Integer empId);
}
3.Impl实现上面的两个接口,一般都是这样开发的
1.departmentServiceImpl.java
@Service
@Repository
public class DeptServiceImpl implements DeptService {
@Autowired
private DepartmentMapper departmentMapper;
@Override
public List getDepartmentList() {
return departmentMapper.getDepartmentList();
}
}
2.EmployeeServiceImpl.java
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
public void addEmployee(Employee employee) {
employeeMapper.addEmployee(employee);
}
@Override
public void deleteEmployeeByEmpId(Integer empId) {
employeeMapper.deleteEmployeeByEmpId(empId);
}
@Override
public void updateEmployee(Employee employee) {
employeeMapper.updateEmployee(employee);
}
@Override
public Page getEmployeeList(int pageNo,int pageSize) {
Page page = new Page<>();
PageHelper.startPage(pageNo,pageSize);
List employeeList = employeeMapper.getEmployeeList();
PageInfo info = new PageInfo<>(employeeList);
page.setPage(info.getList());
page.setPageCount(info.getPages());
page.setPageNo(info.getPageNum());
page.setHasNext(info.isHasNextPage());
page.setHasPre(info.isHasPreviousPage());
return page;
}
@Override
public Employee getEmployeeByEmpId(Integer empId) {
return employeeMapper.getEmployeeByEmpId(empId);
}
}
EmpController.java
@Controller
public class EmpController {
@Autowired
private EmpService empService;
@Autowired
private DeptService deptService;
/*获取所有用户*/
@GetMapping("/allemp")
public String allEmpList(Integer pageNo, Integer pageSize, Model model){
if (pageNo==null){
pageNo=1;
}
if (pageSize==null){
pageSize=5;
}
Page employeeList = empService.getEmployeeList(pageNo, pageSize);
System.out.println(employeeList);
model.addAttribute("employees",employeeList);
return "employee_list";
}
/*添加页面*/
@GetMapping("/addEmp")
public String goAddHtml(Model model){
List departmentList = deptService.getDepartmentList();
model.addAttribute("depts",departmentList);
return "employee_add";
}
/*添加用户*/
@PostMapping("/insertEmp")
public String addEmp(Employee employee){
empService.addEmployee(employee);
return "redirect:/allemp";
}
/*删除用户*/
@GetMapping("/delemp/{empId}")
public String delEmpById(@PathVariable("empId") Integer empId){
empService.deleteEmployeeByEmpId(empId);
return "redirect:/allemp";
}
/*更新用户页面*/
@GetMapping("/upup")
public String upEmp(Integer empId,Model model){
List departmentList = deptService.getDepartmentList();
model.addAttribute("departments",departmentList);
Employee employeeByEmpId = empService.getEmployeeByEmpId(empId);
model.addAttribute("emps",employeeByEmpId);
return "employee_update";
}
/*更新员工信息*/
@GetMapping("/upEmp")
public String upupEmp(Employee employee){
empService.updateEmployee(employee);
return "redirect:/allemp";
}
/*获取分页数据*/
@ResponseBody
@GetMapping("/pagedata")
public Page getPageData(Integer pageNo,Integer pageSize){
if (pageNo==null){
pageNo=1;
}
if (pageSize==null){
pageSize=5;
}
return empService.getEmployeeList(pageNo,pageSize);
}
}
在浏览器输入localhost:8080
总结:上面是我用Sspringboot 实现的一个小Demo ,适合新手小白入手,熟悉springboot的使用。