刷题系统是指一种用于学习和练习编程技能的在线平台,其中包含了大量的编程题目和测试用例,用户可以通过完成这些题目来提高自己的编程能力。刷题系统通常会提供一些辅助功能,如智能评测、代码分享、讨论区等,以帮助用户更好地学习和交流。刷题系统也被广泛应用于编程竞赛和招聘面试等场景。常见的刷题系统包括LeetCode、HackerRank、Codeforces等。
本文主要讲解简单的刷题系统项目
后台系统和前台系统是指网站或应用程序的两个主要组成部分。
因此,后台系统和前台系统是网站或应用程序不可或缺的两个组成部分,它们共同构成了一个完整的网站或应用程序。
1、创建项目:mm
<groupId>com.itheimagroupId>
<artifactId>mmartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
2、在pom.xml文件中添加相关坐标:
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.3version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.1.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.46version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.21version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>2.3.3version>
<scope>providedscope>
dependency>
<dependency>
<groupId>commons-beanutilsgroupId>
<artifactId>commons-beanutilsartifactId>
<version>1.9.4version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.9version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>4.0.1version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>4.0.1version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxml-schemasartifactId>
<version>4.0.1version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>80port>
<path>/path>
configuration>
plugin>
plugins>
build>
删除:web.xml
4、数据库表设计
1、创建实体:Company
public class Company {
private String id;
private String name;
private Date expirationDate;
private String address;
private String licenseId;
private String representative;
private String phone;
private String companySize;
private String industry;
private String remarks;
private Integer state;
private String city;
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 Date getExpirationDate() {
return expirationDate;
}
public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getLicenseId() {
return licenseId;
}
public void setLicenseId(String licenseId) {
this.licenseId = licenseId;
}
public String getRepresentative() {
return representative;
}
public void setRepresentative(String representative) {
this.representative = representative;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCompanySize() {
return companySize;
}
public void setCompanySize(String companySize) {
this.companySize = companySize;
}
public String getIndustry() {
return industry;
}
public void setIndustry(String industry) {
this.industry = industry;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
2、创建dao:CompanyDao
public interface CompanyDao {
int save(Company company);
int delete(Company company);
int update(Company company);
Company findById(String id);
List<Company> findAll();
}
1、创建业务层接口:CompanyService
public interface CompanyService {
/**
* 添加
* @param company
* @return
*/
void save(Company company);
/**
* 删除
* @param company
* @return
*/
void delete(Company company);
/**
* 修改
* @param company
* @return
*/
void update(Company company);
/**
* 查询单个
* @param id 查询的条件(id)
* @return 查询的结果,单个对象
*/
Company findById(String id);
/**
* 查询全部的数据
* @return 全部数据的列表对象
*/
List<Company> findAll();
/**
* 分页查询数据
* @param page 页码
* @param size 每页显示的数据总量
* @return
*/
PageInfo findAll(int page,int size);
}
2、CompanyServiceImpl
public class CompanyServiceImpl implements CompanyService {
@Override
public void save(Company company) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
CompanyDao companyDao = MapperFactory.getMapper(sqlSession,CompanyDao.class);
//id使用UUID的生成策略来获取
String id = UUID.randomUUID().toString();
company.setId(id);
//3.调用Dao层操作
companyDao.save(company);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void delete(Company company) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
CompanyDao companyDao = MapperFactory.getMapper(sqlSession,CompanyDao.class);
//3.调用Dao层操作
companyDao.delete(company);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void update(Company company) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
CompanyDao companyDao = MapperFactory.getMapper(sqlSession,CompanyDao.class);
//3.调用Dao层操作
companyDao.update(company);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public Company findById(String id) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
CompanyDao companyDao = MapperFactory.getMapper(sqlSession,CompanyDao.class);
//3.调用Dao层操作
return companyDao.findById(id);
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public List<Company> findAll() {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
CompanyDao companyDao = MapperFactory.getMapper(sqlSession,CompanyDao.class);
//3.调用Dao层操作
return companyDao.findAll();
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public PageInfo findAll(int page, int size) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
CompanyDao companyDao = MapperFactory.getMapper(sqlSession,CompanyDao.class);
//3.调用Dao层操作
PageHelper.startPage(page,size);
List<Company> all = companyDao.findAll();
PageInfo pageInfo = new PageInfo(all);
return pageInfo;
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
3、创建测试类:CompanyServiceTest
public class CompanyServiceTest {
private static CompanyService companyService = null;
@BeforeClass
public static void init(){
companyService = new CompanyServiceImpl();
}
@Test
public void testSave(){
Company company = new Company();
company.setName("测试数据");
companyService.save(company);
}
@Test
public void testFindAll(){
PageInfo all = companyService.findAll(1, 100);
System.out.println(all);
}
@AfterClass
public static void destory(){
companyService = null;
}
}
1、BaseServlet基类:主要负责业务层实例初始化
package com.itheima.web.controller;
import com.itheima.service.store.*;
import com.itheima.service.store.impl.*;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.ModuleService;
import com.itheima.service.system.RoleService;
import com.itheima.service.system.UserService;
import com.itheima.service.system.impl.DeptServiceImpl;
import com.itheima.service.system.impl.ModuleServiceImpl;
import com.itheima.service.system.impl.RoleServiceImpl;
import com.itheima.service.system.impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class BaseServlet extends HttpServlet {
protected CompanyService companyService;
protected DeptService deptService;
protected UserService userService;
protected CourseService courseService;
protected CatalogService catalogService;
protected QuestionService questionService;
protected QuestionItemService questionItemService;
protected RoleService roleService;
protected ModuleService moduleService;
@Override
public void init() throws ServletException {
companyService = new CompanyServiceImpl();
deptService = new DeptServiceImpl();
userService = new UserServiceImpl();
courseService = new CourseServiceImpl();
catalogService = new CatalogServiceImpl();
questionService = new QuestionServiceImpl();
questionItemService = new QuestionItemServiceImpl();
roleService = new RoleServiceImpl();
moduleService = new ModuleServiceImpl();
}
}
2、CompanyServlet控制器类:主要负责提供接口的各种服务
package com.itheima.web.controller.store;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Company;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
// uri:/store/company?operation=list
@WebServlet("/store/company")
public class CompanyServlet extends BaseServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String operation = request.getParameter("operation");
if("list".equals(operation)){
this.list(request,response);
}else if("toAdd".equals(operation)){
this.toAdd(request,response);
}else if("save".equals(operation)){
this.save(request, response);
}else if("toEdit".equals(operation)){
this.toEdit(request,response);
}else if("edit".equals(operation)){
this.edit(request,response);
}else if("delete".equals(operation)){
this.delete(request,response);
}
}
private void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//进入列表页
//获取数据
int page = 1;
int size = 5;
if(StringUtils.isNotBlank(request.getParameter("page"))){
page = Integer.parseInt(request.getParameter("page"));
}
if(StringUtils.isNotBlank(request.getParameter("size"))){
size = Integer.parseInt(request.getParameter("size"));
}
PageInfo all = companyService.findAll(page, size);
//将数据保存到指定的位置
request.setAttribute("page",all);
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(request,response);
}
private void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/store/company/add.jsp").forward(request,response);
}
private void save(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//将数据获取到,封装成一个对象
Company company = BeanUtil.fillBean(request,Company.class,"yyyy-MM-dd");
//调用业务层接口save
companyService.save(company);
//跳转回到页面list
response.sendRedirect(request.getContextPath()+"/store/company?operation=list");
}
private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//查询要修改的数据findById
String id = request.getParameter("id");
Company company = companyService.findById(id);
//将数据加载到指定区域,供页面获取
request.setAttribute("company",company);
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/store/company/update.jsp").forward(request,response);
}
private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
//将数据获取到,封装成一个对象
Company company = BeanUtil.fillBean(request,Company.class,"yyyy-MM-dd");
//调用业务层接口save
companyService.update(company);
//跳转回到页面list
response.sendRedirect(request.getContextPath()+"/store/company?operation=list");
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
//将数据获取到,封装成一个对象
Company company = BeanUtil.fillBean(request,Company.class);
//调用业务层接口save
companyService.delete(company);
//跳转回到页面list
response.sendRedirect(request.getContextPath()+"/store/company?operation=list");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
1、创建实体:Dept
public class Dept {
private String id;
private String deptName;
private String parentId;
private Integer state;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
}
2、创建dao接口:DeptDao
public interface DeptDao {
int save(Dept dept);
int delete(Dept dept);
int update(Dept dept);
Dept findById(String id);
List<Dept> findAll();
}
1、创建业务层接口:DeptService
public interface DeptService {
/**
* 添加
* @param dept
* @return
*/
void save(Dept dept);
/**
* 删除
* @param dept
* @return
*/
void delete(Dept dept);
/**
* 修改
* @param dept
* @return
*/
void update(Dept dept);
/**
* 查询单个
* @param id 查询的条件(id)
* @return 查询的结果,单个对象
*/
Dept findById(String id);
/**
* 查询全部的数据
* @return 全部数据的列表对象
*/
List<Dept> findAll();
/**
* 分页查询数据
* @param page 页码
* @param size 每页显示的数据总量
* @return
*/
PageInfo findAll(int page, int size);
}
2、创建业务层实现类:DeptServiceImpl
public class DeptServiceImpl implements DeptService {
@Override
public void save(Dept dept) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//id使用UUID的生成策略来获取
String id = UUID.randomUUID().toString();
dept.setId(id);
//3.调用Dao层操作
deptDao.save(dept);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void delete(Dept dept) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//3.调用Dao层操作
deptDao.delete(dept);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void update(Dept dept) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//3.调用Dao层操作
deptDao.update(dept);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public Dept findById(String id) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//3.调用Dao层操作
return deptDao.findById(id);
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public List<Dept> findAll() {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//3.调用Dao层操作
return deptDao.findAll();
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public PageInfo findAll(int page, int size) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//3.调用Dao层操作
PageHelper.startPage(page,size);
List<Dept> all = deptDao.findAll();
PageInfo pageInfo = new PageInfo(all);
return pageInfo;
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
1、创建Servlet:DeptServlet
// uri:/system/dept?operation=list
@WebServlet("/system/dept")
public class DeptServlet extends BaseServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String operation = request.getParameter("operation");
if("list".equals(operation)){
this.list(request,response);
}else if("toAdd".equals(operation)){
this.toAdd(request,response);
}else if("save".equals(operation)){
this.save(request, response);
}else if("toEdit".equals(operation)){
this.toEdit(request,response);
}else if("edit".equals(operation)){
this.edit(request,response);
}else if("delete".equals(operation)){
this.delete(request,response);
}
}
private void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//进入列表页
//获取数据
int page = 1;
int size = 5;
if(StringUtils.isNotBlank(request.getParameter("page"))){
page = Integer.parseInt(request.getParameter("page"));
}
if(StringUtils.isNotBlank(request.getParameter("size"))){
size = Integer.parseInt(request.getParameter("size"));
}
PageInfo all = deptService.findAll(page, size);
//将数据保存到指定的位置
request.setAttribute("page",all);
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/system/dept/list.jsp").forward(request,response);
}
private void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/system/dept/add.jsp").forward(request,response);
}
private void save(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//将数据获取到,封装成一个对象
Dept dept = BeanUtil.fillBean(request,Dept.class,"yyyy-MM-dd");
//调用业务层接口save
// DeptService deptService = new DeptServiceImpl();
deptService.save(dept);
//跳转回到页面list
//list(request,response);
response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
}
private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//查询要修改的数据findById
String id = request.getParameter("id");
// DeptService deptService = new DeptServiceImpl();
Dept dept = deptService.findById(id);
//将数据加载到指定区域,供页面获取
request.setAttribute("dept",dept);
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/system/dept/update.jsp").forward(request,response);
}
private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
//将数据获取到,封装成一个对象
Dept dept = BeanUtil.fillBean(request,Dept.class,"yyyy-MM-dd");
//调用业务层接口save
// DeptService deptService = new DeptServiceImpl();
deptService.update(dept);
//跳转回到页面list
//list(request,response);
response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
//将数据获取到,封装成一个对象
Dept dept = BeanUtil.fillBean(request,Dept.class);
//调用业务层接口save
// DeptService deptService = new DeptServiceImpl();
deptService.delete(dept);
//跳转回到页面list
//list(request,response);
response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
2、在BaseServlet中添加新的代码,如下
public class BaseServlet extends HttpServlet {
protected CompanyService companyService;
protected DeptService deptService;
@Override
public void init() throws ServletException {
companyService = new CompanyServiceImpl();
deptService = new DeptServiceImpl();
}
}
3、实际页面的增删改查
1、创建实体:User
public class User {
private String id;
private String email; //邮箱
private String userName; //姓名
private String password; //密码
private Long state; //状态
private String gender; //性别
private String telephone; //电话
private Date birthday; //出生年月
private Date joinDate; //入职时间
private String deptId; //部门id
private Dept dept;
public Dept getDept() {
return dept;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", email='" + email + '\'' +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", state=" + state +
", gender='" + gender + '\'' +
", telephone='" + telephone + '\'' +
", birthday=" + birthday +
", joinDate=" + joinDate +
", deptId='" + deptId + '\'' +
", dept=" + dept +
'}';
}
//---------get/set方法略
}
2、创建dao接口:UserDao
public interface UserDao {
int save(User user);
int delete(User user);
int update(User user);
User findById(String id);
List<User> findAll();
}
1、创建业务层接口:UserService
public interface UserService {
/**
* 添加
* @param user
* @return
*/
void save(User user);
/**
* 删除
* @param user
* @return
*/
void delete(User user);
/**
* 修改
* @param user
* @return
*/
void update(User user);
/**
* 查询单个
* @param id 查询的条件(id)
* @return 查询的结果,单个对象
*/
User findById(String id);
/**
* 查询全部的数据
* @return 全部数据的列表对象
*/
List<User> findAll();
/**
* 分页查询数据
* @param page 页码
* @param size 每页显示的数据总量
* @return
*/
PageInfo findAll(int page, int size);
}
2、创建业务层实现类:UserServiceImpl
public class UserServiceImpl implements UserService {
@Override
public void save(User user) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
UserDao userDao = MapperFactory.getMapper(sqlSession,UserDao.class);
//id使用UUID的生成策略来获取
String id = UUID.randomUUID().toString();
user.setId(id);
//3.调用Dao层操作
userDao.save(user);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void delete(User user) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
UserDao userDao = MapperFactory.getMapper(sqlSession,UserDao.class);
//3.调用Dao层操作
userDao.delete(user);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void update(User user) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
UserDao userDao = MapperFactory.getMapper(sqlSession,UserDao.class);
//3.调用Dao层操作
userDao.update(user);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public User findById(String id) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
UserDao userDao = MapperFactory.getMapper(sqlSession,UserDao.class);
//3.调用Dao层操作
return userDao.findById(id);
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public List<User> findAll() {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
UserDao userDao = MapperFactory.getMapper(sqlSession,UserDao.class);
//3.调用Dao层操作
return userDao.findAll();
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public PageInfo findAll(int page, int size) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
UserDao userDao = MapperFactory.getMapper(sqlSession,UserDao.class);
//3.调用Dao层操作
PageHelper.startPage(page,size);
List<User> all = userDao.findAll();
PageInfo pageInfo = new PageInfo(all);
return pageInfo;
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
1、创建servlet:com.itheima.web.controller.system.UserServlet
// uri:/system/user?operation=list
@WebServlet("/system/user")
public class UserServlet extends BaseServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String operation = request.getParameter("operation");
if("list".equals(operation)){
this.list(request,response);
}else if("toAdd".equals(operation)){
this.toAdd(request,response);
}else if("save".equals(operation)){
this.save(request, response);
}else if("toEdit".equals(operation)){
this.toEdit(request,response);
}else if("edit".equals(operation)){
this.edit(request,response);
}else if("delete".equals(operation)){
this.delete(request,response);
}
}
private void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//进入列表页
//获取数据
// UserService userService = new UserServiceImpl();
int page = 1;
int size = 5;
if(StringUtils.isNotBlank(request.getParameter("page"))){
page = Integer.parseInt(request.getParameter("page"));
}
if(StringUtils.isNotBlank(request.getParameter("size"))){
size = Integer.parseInt(request.getParameter("size"));
}
PageInfo all = userService.findAll(page, size);
//将数据保存到指定的位置
request.setAttribute("page",all);
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/system/user/list.jsp").forward(request,response);
}
private void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/system/user/add.jsp").forward(request,response);
}
private void save(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//将数据获取到,封装成一个对象
User user = BeanUtil.fillBean(request,User.class,"yyyy-MM-dd");
//调用业务层接口save
userService.save(user);
//跳转回到页面list
response.sendRedirect(request.getContextPath()+"/system/user?operation=list");
}
private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//查询要修改的数据findById
String id = request.getParameter("id");
User user = userService.findById(id);
//将数据加载到指定区域,供页面获取
request.setAttribute("user",user);
//查询部门信息
List<Dept> all = deptService.findAll();
//放入指定位置
request.setAttribute("deptList",all);
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/system/user/update.jsp").forward(request,response);
}
private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
//将数据获取到,封装成一个对象
User user = BeanUtil.fillBean(request,User.class,"yyyy-MM-dd");
//调用业务层接口save
userService.update(user);
//跳转回到页面list
response.sendRedirect(request.getContextPath()+"/system/user?operation=list");
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
//将数据获取到,封装成一个对象
User user = BeanUtil.fillBean(request,User.class);
//调用业务层接口save
userService.delete(user);
//跳转回到页面list
response.sendRedirect(request.getContextPath()+"/system/user?operation=list");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
2、修改BaseServlet
public class BaseServlet extends HttpServlet {
protected CompanyService companyService;
protected DeptService deptService;
protected UserService userService;
@Override
public void init() throws ServletException {
companyService = new CompanyServiceImpl();
deptService = new DeptServiceImpl();
userService = new UserServiceImpl();
}
}