一个基于SB_MYBATIS_JSP的CRUD演示项目

文章目录

一个基于sb+mybatis+jsp的CRUD演示项目-员工管理系统

  • springboot
    • spring–tomcat
    • springmvc–controller–Servlet
    • 组件(java对象)+容器spring
    • 约定大于配置,配置于编码
  • mybatis-dao
    • jdbc—entity–dao–service–controller
    • mybatis
      • dao interface
      • dao mapper—xml
    • mybatis-plus—MP
  • views
    • html5.div+css3+javascript
    • jquery
    • vue3,raect,angular2.0
    • thymeleaf—html
    • freemarker
    • jsp—el—jstl

01 项目坐标及pom文件

01.1 创建项目

一个基于SB_MYBATIS_JSP的CRUD演示项目
一个基于SB_MYBATIS_JSP的CRUD演示项目

一个基于SB_MYBATIS_JSP的CRUD演示项目

查看生成的项目结构

一个基于SB_MYBATIS_JSP的CRUD演示项目

01.2 调整pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<!-- sb父包,注意版本号 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.5</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<!-- 本项目的坐标 -->
	<groupId>com.zfh</groupId>
	<artifactId>ems</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>sb06-emps</name>
	<description>员工管理系统演示CRUD操作</description>
	<!-- 版本管理 -->
	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>
	
	<!-- 所有依赖 -->
	<dependencies>
		<!-- springmvc部分 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 开发时,自动部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<!-- mysql驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>druid</artifactId>
		    <version>1.1.20</version>
		</dependency>

		<!-- 编译前自动生成工具getter/setter etc -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<!-- 单元测试工具 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- tomcat 的支持. -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- 支持jsp -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>
		<!-- 支持servlet -->
		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>javax.servlet-api</artifactId>
		    <version>3.1.0</version>
		    <scope>provided</scope>
		</dependency>		
		<!-- 支持jstl -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<!-- mybatis -->
		<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
		<dependency>
		    <groupId>org.mybatis.spring.boot</groupId>
		    <artifactId>mybatis-spring-boot-starter</artifactId>
		    <version>2.1.3</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

01.3 搭建结构

一个基于SB_MYBATIS_JSP的CRUD演示项目

02 项目配置文件

# server config
server.port=9527
server.servlet.context-path=/emps
server.servlet.jsp.init-parameters.development=true

# app view
spring.application.name=emps
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

# app db
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root

# mybatis
mybatis.mapper-locations=classpath:com/zfh/mapper/*.xml
mybatis.type-aliases-package=com.zfh.entity

# log
logging.level.root=info
logging.level.root.com.zfh.dao=debug

一个基于SB_MYBATIS_JSP的CRUD演示项目

一个基于SB_MYBATIS_JSP的CRUD演示项目

一个基于SB_MYBATIS_JSP的CRUD演示项目

03 项目结构

一个基于SB_MYBATIS_JSP的CRUD演示项目

一个基于SB_MYBATIS_JSP的CRUD演示项目

03.1 数据库和数据

/*
Navicat MySQL Data Transfer

Source Server         : MySQL57
Source Server Version : 50718
Source Host           : localhost:3306
Source Database       : testdb

Target Server Type    : MYSQL
Target Server Version : 50718
File Encoding         : 65001

Date: 2021-05-23 08:00:37
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for account
-- ----------------------------
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号',
  `name` varchar(255) DEFAULT NULL COMMENT '账号',
  `money` float DEFAULT NULL COMMENT '金额',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of account
-- ----------------------------
INSERT INTO `account` VALUES ('1', 'peter123', '2900');
INSERT INTO `account` VALUES ('2', 'merry456', '3000');
INSERT INTO `account` VALUES ('3', 'tom', '100');
INSERT INTO `account` VALUES ('4', 'mike迈克', '1000');
INSERT INTO `account` VALUES ('5', 'cock公鸡先生', '800');
INSERT INTO `account` VALUES ('6', 'netty', '1000');
INSERT INTO `account` VALUES ('10', 'underwood', '4000');
INSERT INTO `account` VALUES ('11', 'wetty', '500');

-- ----------------------------
-- Table structure for emp
-- ----------------------------
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
  `empno` int(11) NOT NULL AUTO_INCREMENT,
  `ename` char(50) NOT NULL,
  `job` char(100) DEFAULT NULL,
  `hiredate` datetime DEFAULT NULL,
  `sal` float DEFAULT NULL,
  `comm` float DEFAULT NULL,
  PRIMARY KEY (`empno`)
) ENGINE=InnoDB AUTO_INCREMENT=22222223 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of emp
-- ----------------------------
INSERT INTO `emp` VALUES ('1001', '周大明', '总总钻风', '2021-05-20 00:00:00', '25000', '35000');
INSERT INTO `emp` VALUES ('1002', '周小明', '总钻风', '2021-05-12 00:00:00', '5000', '5000');
INSERT INTO `emp` VALUES ('1004', '周小明', '总钻风', '2021-05-12 00:00:00', '2000', '3000');
INSERT INTO `emp` VALUES ('1008', '郑框架', '项目经理', '2019-10-10 00:00:00', '3000', '2000');
INSERT INTO `emp` VALUES ('1009', '郑前端', '前端工程师', '2020-11-11 00:00:00', '1000', '1000');
INSERT INTO `emp` VALUES ('1023', '*', '少帅', '1920-10-10 00:00:00', '100000', '200000');
INSERT INTO `emp` VALUES ('1024', '张学友', '文艺部', '2019-11-11 00:00:00', '20000', '20000');
INSERT INTO `emp` VALUES ('2002', '张员工', 'CTO司机', '2020-06-06 00:00:00', '2000', '3000');
INSERT INTO `emp` VALUES ('11111111', '周星星', '小卧底', '2020-09-01 00:00:00', '1000', '2000');
INSERT INTO `emp` VALUES ('22222222', '吴达达', '总钻风', '2020-09-01 00:00:00', '2000', '4000');

-- ----------------------------
-- Table structure for news
-- ----------------------------
DROP TABLE IF EXISTS `news`;
CREATE TABLE `news` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '新闻编号',
  `title` varchar(50) NOT NULL COMMENT '新闻标题',
  `content` text COMMENT '新闻正文',
  `userid` int(11) NOT NULL COMMENT '发布人id',
  `pubtime` datetime NOT NULL COMMENT '发布时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='新闻表';

-- ----------------------------
-- Records of news
-- ----------------------------

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `gender` varchar(2) DEFAULT NULL,
  `gradeClass` varchar(255) DEFAULT NULL,
  `birth` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `cellphone` char(11) DEFAULT NULL,
  `qq` varchar(255) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('2019001001', '张学霸', '女', '18级', '1999-10-01 19:03:06', '13812341234', '1234567890', ' 成都市学府路24号1-1-1');

-- ----------------------------
-- Table structure for tb_emp
-- ----------------------------
DROP TABLE IF EXISTS `tb_emp`;
CREATE TABLE `tb_emp` (
  `id` varchar(50) NOT NULL,
  `name` varchar(50) NOT NULL,
  `salary` double DEFAULT NULL,
  `age` int(3) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of tb_emp
-- ----------------------------
INSERT INTO `tb_emp` VALUES ('a00001', '关羽', '2000', '30');
INSERT INTO `tb_emp` VALUES ('a00002', '张飞', '2100', '31');
INSERT INTO `tb_emp` VALUES ('a00004', '赵云', '3000', '31');

-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
  `id` varchar(50) NOT NULL,
  `username` varchar(50) NOT NULL,
  `realname` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `gender` char(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('6b448c7c-e8d2-460b-8ffd-10754e6d143a', 'jtzheng', '郑洁姚', '123456', '女');
INSERT INTO `tb_user` VALUES ('d137cc7d-9a04-4fba-ab1c-a75a3041c12d', 'fhzheng', '郑丰华', '123456', '男');

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户编号',
  `username` varchar(20) NOT NULL COMMENT '用户名称',
  `passowrd` varchar(18) NOT NULL COMMENT '用户密码',
  `sex` enum('0','1') NOT NULL COMMENT '性别,0男1女',
  `profession` enum('0','1','2') DEFAULT NULL COMMENT '职业,0学生1老师2工人',
  `favourite` enum('0','1','2') DEFAULT NULL COMMENT '爱好,0电脑网络1影视娱乐2棋牌娱乐',
  `note` varchar(255) DEFAULT NULL COMMENT '个人说明',
  `type` enum('0','1') NOT NULL DEFAULT '0' COMMENT '用户类型,0普通用户1管理员,默认为0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';

-- ----------------------------
-- Records of user
-- ----------------------------

-- ----------------------------
-- Table structure for userinfo
-- ----------------------------
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo` (
  `username` varchar(10) NOT NULL COMMENT '用户名',
  `password` varchar(20) DEFAULT NULL,
  `sex` enum('男','女') NOT NULL COMMENT '性别',
  `note` varchar(255) DEFAULT NULL COMMENT '说明',
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';

-- ----------------------------
-- Records of userinfo
-- ----------------------------
INSERT INTO `userinfo` VALUES ('admin', 'admin', '男', '系统管理员');
INSERT INTO `userinfo` VALUES ('admin1', 'admin1', '女', '系统管理员1');
INSERT INTO `userinfo` VALUES ('admin2', 'admin2', '男', '系统管理员2');
INSERT INTO `userinfo` VALUES ('sa', 'sa', '男', '超级用户');

04 入口文件,即启动文件和实体层

04.1 入口文件

package com.zfh;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.zfh.dao")
public class Sb06EmpsApplication {

	public static void main(String[] args) {
		SpringApplication.run(Sb06EmpsApplication.class, args);
	}

}

一个基于SB_MYBATIS_JSP的CRUD演示项目

04.2 实体层-用户实体

package com.zfh.entity;

import java.io.Serializable;

public class User implements Serializable {

	private String id;
	private String username;
	private String realname;
	private String password;
	private String gender;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getRealname() {
		return realname;
	}
	public void setRealname(String realname) {
		this.realname = realname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", realname=" + realname + ", password=" + password
				+ ", gender=" + gender + "]";
	}
	public User(String id, String username, String realname, String password, String gender) {
		super();
		this.id = id;
		this.username = username;
		this.realname = realname;
		this.password = password;
		this.gender = gender;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}


## 04.3 实体层-员工实体

```java
package com.zfh.entity;

import java.io.Serializable;

public class Emp implements Serializable {

	private String id;
	private String name;
	private Double salary;
	private Integer age;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getSalary() {
		return salary;
	}
	public void setSalary(Double salary) {
		this.salary = salary;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Emp [id=" + id + ", name=" + name + ", salary=" + salary + ", age=" + age + "]";
	}
	public Emp(String id, String name, Double salary, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
		this.age = age;
	}
	public Emp() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}

05 控制器

05.1 测试控制器

package com.zfh.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("test")
public class TestController {

	
	@RequestMapping("hello")
	@ResponseBody
	public String sayHello() {
		System.out.println("Client request test/hello contrller...");
		return "hello,Springboot";
	}
	
	@RequestMapping("index")
	public String goIndex() {
		System.out.println("Client request test/index contrller...");
		return "index";
	}
}

05.2 用户控制器

package com.zfh.controller;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zfh.entity.User;
import com.zfh.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;

	@RequestMapping("/regist")
	public String userRegist() {
		return "/user/regist";
	}
	
	@RequestMapping("/login")
	public String userLogin() {
		return "/user/login";
	}
	
	@RequestMapping("/regist.do")
	public String userRegistAction(User user) {
		String id = UUID.randomUUID().toString();
		user.setId(id);
		System.out.println(user);
		try {
			userService.save(user);
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/user/regist";
		}
		return "redirect:/user/login";
	}
	
	@RequestMapping("/login.do")
	public String userLoginAction(String username, String password) {
		try {
			User user = userService.findUserByUsernameAndPassword(username,password);
			if (user != null) {
				return "redirect:/emp/main";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/user/login";
		}
		return "redirect:/user/login";
	}
	
	
}

05.3 员工管理控制器

package com.zfh.controller;

import java.util.List;

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 com.zfh.entity.Emp;
import com.zfh.service.EmpService;

@Controller
@RequestMapping("/emp")
public class EmpController {
	
	@Autowired
	private EmpService empService;

	@RequestMapping("/main")
	public String empMain(Model m) {
		List<Emp> emps = empService.findAll();
		m.addAttribute("emps", emps);
		return "/emp/main";
	}
	
	@RequestMapping("/add")
	public String empAdd() {
		return "/emp/add";
	}
	
	@RequestMapping("/edit")
	public String empEdit(String id,Model m) {
		Emp emp = empService.findEmpById(id);
//		System.out.println("emp id:"+id);
		m.addAttribute("emp", emp);
		return "/emp/edit";
	}
	
	@RequestMapping("/add.do")
	public String empAddAction(Emp emp) {
		try {
			empService.save(emp);
			return "redirect:/emp/main";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/emp/main";
	}
	
	@RequestMapping("/edit.do")
	public String empEditAction(Emp emp) {
		try {
			empService.alter(emp);
			return "redirect:/emp/main";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/emp/main";
	}
	
	@RequestMapping("/del")
	public String empDelAction(String id) {
		try {
			empService.remove(id);
			return "redirect:/emp/main";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/emp/main";
	}
	
}

06 业务层

06.1 用户管理业务接口

package com.zfh.service;


import com.zfh.entity.User;


public interface UserService {

	void save(User user) throws Exception;

	User findUserByUsernameAndPassword(String username, String password);
}

06.2 用户管理业务接口实现

package com.zfh.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zfh.dao.UserDAO;
import com.zfh.entity.User;

@Service
@Transactional
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDAO userDAO;

	@Override
	public void save(User user) throws Exception {
		userDAO.insertObj(user);
	}

	@Override
	public User findUserByUsernameAndPassword(String username, String password) {
		return userDAO.selectObjByNameAndPwd(username,password);
	}

}

06.3 员工管理业务接口

package com.zfh.service;

import java.util.List;

import com.zfh.entity.Emp;

public interface EmpService {

	List<Emp> findAll();

	void save(Emp emp);

	void alter(Emp emp);

	Emp findEmpById(String id);

	void remove(String id);

}

06.4 员工管理业务接口实现

package com.zfh.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zfh.dao.EmpDAO;
import com.zfh.entity.Emp;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {
	
	@Autowired
	private EmpDAO empDAO;

	@Override
	public List<Emp> findAll() {
		return empDAO.selectAllObjs();
	}

	@Override
	public void save(Emp emp) {
		empDAO.insertObj(emp);
	}

	@Override
	public void alter(Emp emp) {
		empDAO.updateObj(emp);
	}

	@Override
	public Emp findEmpById(String id) {
		return empDAO.selectObjById(id);
	}

	@Override
	public void remove(String id) {
		empDAO.deleteObj(id);		
	}

}

07 数据访问层接口

07.1 数据访问接口泛型

package com.zfh.dao;

import java.util.List;

public interface BaseDAO<T,K> {

	void insertObj(T t);
	void updateObj(T t);
	void deleteObj(K id);
	T selectObjById(K id);
	List<T> selectAllObjs();
	Long getTotalObjsCount();
	List<T> selectObjsByPage(Integer start,Integer size);
}

07.2 用户dao

package com.zfh.dao;

import com.zfh.entity.User;

public interface UserDAO extends BaseDAO<User, String> {

	User selectObjByNameAndPwd(String username, String password);

}

07.3 员工dao

package com.zfh.dao;

import com.zfh.entity.Emp;

public interface EmpDAO extends BaseDAO<Emp, String> {

}

08 数据访问层mybatis的mapper配置

08.1 用户mapper

<?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="com.zfh.dao.UserDAO" >

	<insert id="insertObj" parameterType="com.zfh.entity.User">
		insert into tb_user(id,username,realname,password,gender) 
		values(#{id},#{username},#{realname},#{password},#{gender})
	</insert>
	
	<select id="selectObjById" parameterType="String" resultType="com.zfh.entity.User">
		select 
			id,username,realname,password,gender
		from
			tb_user
		where
			id = #{id}
	</select>
	
	<select id="selectObjByNameAndPwd" resultType="com.zfh.entity.User">
		select 
			id,username,realname,password,gender
		from
			tb_user
		where
			username = #{username} and password=#{password}
	</select>
	
	
	
</mapper>

08.2 员工mapper

<?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="com.zfh.dao.EmpDAO" >

	<insert id="insertObj">
		insert into tb_emp(id,name,salary,age)
		values(#{id},#{name},#{salary},#{age})
	</insert>
	
	<delete id="deleteObj">
		delete from tb_emp
		where id=#{id}
	</delete>
	
	<update id="updateObj">
		update tb_emp 
			set name=#{name},salary=#{salary},age=#{age}
		where
			id=#{id}
	</update>
	
	<select id="selectAllObjs" resultType="com.zfh.entity.Emp">
		select 
			id,name,salary,age
		from tb_emp
	</select>
	
	<select id="selectObjById" parameterType="String" resultType="com.zfh.entity.Emp">
		select 
			id,name,salary,age
		from tb_emp
		where
			id = #{id}
	</select>
</mapper>

09 视图部分

09.1 默认首页index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test page</title>
</head>
<body>
webcome to springboot project, this is a jsp test page.
</body>
</html>

09.2 员工信息

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>员工管理首页</title>
<script type="text/javascript">
function del(id){
	
	if(confirm('是否要删除id为'+id+'的员工 ?')){
		location.href="${pageContext.request.contextPath}/emp/del?id="+id;
	}	
}

</script>

</head>
<body>
<h2>员工信息</h2><hr>
<table border="1">
	<thead>
		<tr>
			<th>序号</th>
			<th>员工ID</th>
			<th>员工姓名</th>
			<th>员工工资</th>
			<th>员工年龄</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody>
		<tr><td colspan="6"><a href="${pageContext.request.contextPath}/emp/add">添加员工</a> &nbsp;&nbsp;&nbsp;&nbsp;</td></tr>
		<c:if test="${empty emps}">
		<tr><td colspan="6">还没有员工数据!!!</td></tr>
		</c:if>
		<c:if test="${not empty emps}">
		<c:forEach items="${emps}" varStatus="status" var="emp">
			<tr>
				<td>${status.count}</td>
				<td>${emp.id}</td>
				<td>${emp.name}</td>
				<td>${emp.salary}</td>
				<td>${emp.age}</td>
				<td>
					<a href="${pageContext.request.contextPath}/emp/edit?id=${emp.id}">编辑</a> &nbsp;&nbsp;
					<a href="javascript: del('${emp.id}')">删除</a>
				</td>
			</tr>
		</c:forEach>
		</c:if>
	</tbody>
</table>
</body>
</html>

09.3 添加员工

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加员工</title>
</head>
<body>
<h2>添加员工</h2>
<hr>
<form action="${pageContext.request.contextPath}/emp/add.do" method="post">
员工编号:<input type="text" name="id" /><br>
员工姓名:<input type="text" name="name" /><br>
员工工资:<input type="text" name="salary" /><br>
员工年龄:<input type="text" name="age" /><br>
	<input type="submit" value="添加" />
	<input type="reset" value="重置" />
</form>
</body>
</html>

09.4 编辑员工

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>编辑员工</title>
</head>
<body>
<h2>编辑员工</h2>
<hr>
<form action="${pageContext.request.contextPath}/emp/edit.do" method="post">
员工编号:${emp.id}<input type="hidden" name="id" value="${emp.id}" /><br>
员工姓名:<input type="text" name="name"  value="${emp.name}" /><br>
员工工资:<input type="text" name="salary"  value="${emp.salary}" /><br>
员工年龄:<input type="text" name="age"  value="${emp.age}" /><br>
	<input type="submit" value="修改" />
	<input type="reset" value="重置" />
</form>
</body>
</html>

09.5 用户登录

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>user login</title>
</head>
<body>
<h2>用户登录</h2>
<hr>
<form action="${pageContext.request.contextPath}/user/login.do" method="post">
用户名:<input type="text" name="username" /><br>
用户密码:<input type="text" name="password" /><br>
	<input type="submit" value="登录" />
	<input type="reset" value="重置" />
</form>
</body>
</html>

09.6 用户注册

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>user regist</title>
</head>
<body>
<h2>用户注册</h2>
<hr>
<form action="${pageContext.request.contextPath}/user/regist.do" method="post">
用户名:<input type="text" name="username" /><br>
用户真名:<input type="text" name="realname" /><br>
用户密码:<input type="text" name="password" /><br>
用户性别:<input type="radio" name="gender" value="男" />男
	<input type="radio" name="gender" value="女" checked/>女<br>
	<input type="submit" value="注册" />
	<input type="reset" value="重置" />
</form>
</body>
</html>
上一篇:springBoot完成简单的员工CRUD操作,thymeleaf的练习


下一篇:项目开发笔记——传智健康项目二:简单的CRUD