目录
写博客的小背景
开发工具——intelij IDEA
建一个ssm小例子
建数据库
最终结果展示
大二结束后,跟着老师学习web开发,用的是SSM框架即spring+springMVC+mybatis。0基础学习,困难重重,好在有老师和学姐学长们的帮助,以及我们小组同学之间的不断交流,才渐渐的打开了web开发的大门,一步一步的向前走。
学而不思则罔,思而不学则殆。半个月的学习使我领略到了一个工程的开发远比写一句hello world要复杂的多,其中涉及到各种各样的知识技能,如若不加整理,终将是一堆散沙,久而久之,或许将随风而去。总结便是聚沙成塔的过程。
项目管理工具maven
下面介绍如何用maven建一个web工程
建好之后的目录结构
配置Tomcat
点击运行
运行结果
文件目录的结构如下:
这样建目录结构符合maven的约定
然后标记文件夹的属性
1.将java文件夹标记为sources root
2.将resources标记为resources root
3.同样的操作方式,依次将test目录下的java文件夹标记为test sources root
4.将test目录下的resources文件夹标记为test resources root
浏览一下项目的整个文件目录结构:
下面是各个文件的原码
开始配置pom.xml
4.0.0
com.snnu
ccs
1.0-SNAPSHOT
war
webapp1 Maven Webapp
http://www.ccs.com
UTF-8
1.7
1.7
junit
junit
4.12
test
org.slf4j
slf4j-api
1.7.23
ch.qos.logback
logback-classic
1.1.7
ch.qos.logback
logback-core
1.2.3
mysql
mysql-connector-java
5.1.41
runtime
c3p0
c3p0
0.9.1.2
org.mybatis
mybatis
3.3.0
org.mybatis
mybatis-spring
1.3.0
taglibs
standard
1.1.2
jstl
jstl
1.2
com.fasterxml.jackson.core
jackson-databind
2.8.0
javax.servlet
javax.servlet-api
3.1.0
org.springframework
spring-core
4.2.1.RELEASE
org.springframework
spring-beans
4.2.1.RELEASE
org.springframework
spring-context
4.2.1.RELEASE
org.springframework
spring-jdbc
4.2.1.RELEASE
org.springframework
spring-tx
4.2.1.RELEASE
org.springframework
spring-web
4.2.1.RELEASE
org.springframework
spring-webmvc
4.2.1.RELEASE
org.springframework
spring-test
4.2.1.RELEASE
webapp1
maven-clean-plugin
3.0.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.7.0
maven-surefire-plugin
2.20.1
maven-war-plugin
3.2.0
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
配置logback.xml
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
配置spring-dao.xml
配置spring-service.xml
配置spring-web.xml
/WEB-INF/jsp/
.jsp
配置jdbc.properties
说明:user=root (root是你的mysql的用户名)
password是你的mysql的密码
url=jdbc:mysql://127.0.0.1:3306/work?useUnicode=true&charsetEncoding=utf8&useSSL=false
work是你建的数据库的名字
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/work?useUnicode=true&charsetEncoding=utf8&useSSL=false
user=root
password=123456
配置mybatis-config.xml
配置web.xml
ccs-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/spring-*.xml
ccs-dispatcher
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
下面开始写各个文件
JobDao.java
package com.snnu.dao;
import com.snnu.entity.Job;
import java.util.List;
/**
* Create by pengweijie on 2018/7/27
*/
public interface JobDao {
Job selectByName(String jobName);
Job selectById(int jobId);
List queryAllJob();
void deleteById(int jobId);
void insertJob(Job job);
}
Worker_JobDao.java
package com.snnu.dao;
import com.snnu.entity.Worker_Job;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Create by pengweijie on 2018/7/27
*/
public interface Worker_JobDao {
void insert(Worker_Job worker_job);
List queryAllWorkerJob();
void deleteByWorkerIdAndJobId(@Param("workerId")Integer workerId, @Param("jobId")Integer jobId);
}
WorkerDao.java
package com.snnu.dao;
import com.snnu.entity.Worker;
import java.util.List;
/**
* Create by pengweijie on 2018/7/27
*/
public interface WorkerDao {
List queryAll();
Worker selectByName(String workerName);
Worker selectById(int workerId);
void deleteById(int workerId);
void insertWorker(Worker worker);
}
Job.java
package com.snnu.entity;
/**
* Create by pengweijie on 2018/7/27
*/
public class Job {
private Integer jobId;
private String jobName;
private Integer salary;
public int getJobId() { return jobId; }
public void setJobId(int jobId) {
this.jobId = jobId;
}
public String getJobName() {
return jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Job(Integer jobId, String jobName, Integer salary) {
this.jobId = jobId;
this.jobName = jobName;
this.salary = salary;
}
@Override
public String toString() {
return "Job{" +
"jobId=" + jobId +
", jobName='" + jobName + '\'' +
", salary=" + salary +
'}';
}
}
Worker.java
package com.snnu.entity;
/**
* Create by pengweijie on 2018/7/27
*/
public class Worker {
private Integer workerId;
private String workerName;
private String workerSex;
private String workerAge;
private String workerPhone;
public Worker(Integer workerId, String workerName, String workerSex,String workerAge, String workerPhone) {
this.workerId = workerId;
this.workerName = workerName;
this.workerSex=workerSex;
this.workerAge = workerAge;
this.workerPhone = workerPhone;
}
public int getWorkerId() {
return workerId;
}
public void setWorkerId(int workerId) {
this.workerId = workerId;
}
public String getWorkerName() {
return workerName;
}
public void setWorkerName(String workerName) {
this.workerName = workerName;
}
public String getWorkerAge() {
return workerAge;
}
public void setWorkerAge(String workerAge) {
this.workerAge = workerAge;
}
public String getWorkerPhone() {
return workerPhone;
}
public void setWorkerPhone(String workerPhone) {
this.workerPhone = workerPhone;
}
public String getWorkerSex() {
return workerSex;
}
public void setWorkerSex(String workerSex) {
this.workerSex = workerSex;
}
@Override
public String toString() {
return "Worker{" +
"workerId=" + workerId +
", workerName='" + workerName + '\'' +
", workerSex='" + workerSex + '\'' +
", workerAge='" + workerAge + '\'' +
", workerPhone='" + workerPhone + '\'' +
'}';
}
}
Worker_Job.java
package com.snnu.entity;
/**
* Create by pengweijie on 2018/7/27
*/
public class Worker_Job {
private Integer workerId;
private Integer jobId;
private String workerName;
private String jobName;
private Integer salary;
private Worker worker;
private Job job;
public Worker_Job(Integer workerId, Integer jobId, String workerName, String jobName, Integer salary) {
this.workerId = workerId;
this.jobId = jobId;
this.workerName = workerName;
this.jobName = jobName;
this.salary = salary;
}
public Worker_Job(Worker worker, Job job) {
this.workerId = worker.getWorkerId();
this.jobId = job.getJobId();
this.workerName = worker.getWorkerName();
this.jobName = job.getJobName();
this.salary = job.getSalary();
}
@Override
public String toString() {
return "Worker_Job{" +
"workerId=" + workerId +
", jobId=" + jobId +
", workerName='" + workerName + '\'' +
", jobName='" + jobName + '\'' +
", salary=" + salary +
'}';
}
public Integer getWorkerId() {
return workerId;
}
public void setWorkerId(Integer workerId) {
this.workerId = workerId;
}
public Integer getJobId() {
return jobId;
}
public void setJobId(Integer jobId) {
this.jobId = jobId;
}
public String getWorkerName() {
return workerName;
}
public void setWorkerName(String workName) {
this.workerName = workName;
}
public String getJobName() {
return jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
}
DistributeServiceImpl.java
package com.snnu.service.impl;
import com.snnu.dao.JobDao;
import com.snnu.dao.WorkerDao;
import com.snnu.dao.Worker_JobDao;
import com.snnu.entity.Job;
import com.snnu.entity.Worker;
import com.snnu.entity.Worker_Job;
import com.snnu.service.DistributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Create by pengweijie on 2018/7/27
*/
@Service
public class DistributeServiceImpl implements DistributeService {
@Autowired
private WorkerDao workerDao;
@Autowired
private JobDao jobDao;
@Autowired
private Worker_JobDao worker_jobDao;
@Override
public Worker getWorkerById(Integer workerId) {
return workerDao.selectById(workerId);
}
@Override
public List getWorkerList() {
return workerDao.queryAll();
}
@Override
public List getJobList() {
return jobDao.queryAllJob();
}
@Override
public List getWorkerJobList() {
return worker_jobDao.queryAllWorkerJob();
}
@Override
public void addWorker(Worker worker) {
workerDao.insertWorker(worker);
}
@Override
public void addJob(Job job) {
jobDao.insertJob(job);
}
@Override
public void deleteJob(Integer JobId) {
jobDao.deleteById(JobId);
}
@Override
public void fireWorker(Integer WorkerId) {
workerDao.deleteById(WorkerId);
}
@Override
public void DistributeJob(String workerName, String jobName) {
Worker worker = workerDao.selectByName(workerName);
Job job = jobDao.selectByName(jobName);
System.out.println(worker.toString());
System.out.println(job.toString());
Worker_Job worker_job = new Worker_Job(worker, job);
System.out.println(worker_job.toString());
worker_jobDao.insert(worker_job);
}
@Override
public void deleteWorkerJob(Integer workerId, Integer jobId) {
worker_jobDao.deleteByWorkerIdAndJobId(workerId, jobId);
}
}
DistributeService.java
package com.snnu.service;
import com.snnu.entity.Job;
import com.snnu.entity.Worker;
import com.snnu.entity.Worker_Job;
import java.util.List;
/**
* Create by pengweijie on 2018/7/27
*/
public interface DistributeService {
//获取单个工人
Worker getWorkerById(Integer workerId);
//获取所有工人信息
List getWorkerList();
//获取工作列表
List getJobList();
//获取工作安排信息表
List getWorkerJobList();
//安排工作
void DistributeJob(String workerName,String jobName);
//添加工人
void addWorker(Worker worker);
//开除工人
void fireWorker(Integer workerId);
//新增工作
void addJob(Job job);
//废除工作
void deleteJob(Integer jobId);
//删除“员工-工作”记录
void deleteWorkerJob(Integer workerId,Integer jobId);
}
DistributeController.java
package com.snnu.web;
import com.snnu.entity.Job;
import com.snnu.entity.Worker;
import com.snnu.entity.Worker_Job;
import com.snnu.service.DistributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
/**
* Create by pengweijie on 2018/7/27
*/
@Controller
@RequestMapping("/distribute")
public class DistributeController {
@Autowired
private DistributeService distributeService;
@RequestMapping(value = "/worker", method = RequestMethod.GET)
public String workerList(Model model) {
List workerlist = distributeService.getWorkerList();
model.addAttribute("workerList", workerlist);
return "workerList";
}
@RequestMapping(value = "/job", method = RequestMethod.GET)
public String jobList(Model model) {
List jobList = distributeService.getJobList();
model.addAttribute("jobList", jobList);
return "jobList";
}
@RequestMapping(value = "/workerjob", method = RequestMethod.GET)
public String workjobList(Model model) {
List worker_jobList = distributeService.getWorkerJobList();
model.addAttribute("work_jobList", worker_jobList);
return "worker_jobList";
}
@RequestMapping(value = "/{workerId}/deleteWorker", method = RequestMethod.GET)
public String deleteWorker(@PathVariable("workerId") Integer workerId, Model model) {
distributeService.fireWorker(workerId);
return "redirect:/distribute/worker";
}
@RequestMapping(value = "/{jobId}/deleteJob", method = RequestMethod.GET)
public String deleteJob(@PathVariable("jobId") Integer jobId) {
distributeService.deleteJob(jobId);
return "redirect:/distribute/job";
}
@RequestMapping(value = "/{workerId}/{jobId}/deleteWorkerJob", method = RequestMethod.GET)
public String deleteWorkerJob(@PathVariable("workerId") Integer workerId,
@PathVariable("jobId") Integer jobId) {
distributeService.deleteWorkerJob(workerId, jobId);
return "redirect:/distribute/workerjob";
}
@RequestMapping(value = "/addWorker", method = RequestMethod.GET)
public String addWorker() {
return "updateWorker";
}
@RequestMapping(value = "/addJob", method = RequestMethod.GET)
public String addJob() {
return "updateJob";
}
@RequestMapping(value = "/addWorkerJob", method = RequestMethod.GET)
public String addWorkerJob() {
return "updateWorkerJob";
}
@RequestMapping(value = "/registerworker")
public String registerworker(@RequestParam("workerId") Integer workerId,
@RequestParam("workerName") String workerName,
@RequestParam("workerSex") String workerSex,
@RequestParam("workerAge") String workerAge,
@RequestParam("workerPhone") String workerPhone) {
Worker worker = new Worker(workerId, workerName, workerSex, workerAge, workerPhone);
distributeService.addWorker(worker);
return "redirect:/distribute/worker";
}
@RequestMapping(value = "/registerjob")
public String registerworker(@RequestParam("jobId") Integer jobId,
@RequestParam("jobName") String jobName,
@RequestParam("salary") Integer salary) {
Job job = new Job(jobId, jobName, salary);
distributeService.addJob(job);
return "redirect:/distribute/job";
}
@RequestMapping(value = "distributeJob", method = RequestMethod.POST)
public String distribute(@RequestParam("workerName") String workerName,
@RequestParam("jobName") String jobName) {
System.out.println(workerName);
System.out.println(jobName);
distributeService.DistributeJob(workerName, jobName);
return "redirect:/distribute/workerjob";
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String backHomePage() {
return "index";
}
}
JobDao.xml
DELETE FROM job WHERE jobId = #{jobId}
INSERT INTO Job (
jobId,
jobName,
salary)
VALUES (
#{jobId},
#{jobName},
#{salary}
)
Worker_JobDao.xml
INSERT INTO worker_job (
workerId,
jobId,
workerName,
jobName,
salary)
VALUES (
#{workerId},
#{jobId},
#{workerName},
#{jobName},
#{salary}
)
DELETE
FROM worker_job
WHERE workerId = #{workerId} and jobId=#{jobId}
WorkerDao.xml
DELETE FROM worker WHERE workerId = #{workerId}
INSERT INTO worker (
workerId,
workerName,
workerSex,
workerAge,
workerPhone)
VALUES (#{workerId},
#{workerName},
#{workerSex},
#{workerAge},
#{workerPhone})
head.jsp
tag.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
common/index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="common/tag.jsp"%>
主界面
<%@include file="common/head.jsp"%>
jobList.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="common/tag.jsp"%>
职业列表页
<%@include file="common/head.jsp"%>
updateJob.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
修改工作信息
修改页面
updateWorker.jsp
<%@ page contentType="text/html;charset=utf-8" language="java" %>
修改用户信息
修改页面
updateWorkerJob.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
安排工作
安排工作
worker_jobList.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="common/tag.jsp"%>
已分配列表页
<%@include file="common/head.jsp"%>
workerList.jsp
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@include file="common/tag.jsp"%>
工人列表页
<%@include file="common/head.jsp"%>
WEB-INF/index.jsp
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@include file="WEB-INF/jsp/common/head.jsp"%>
欢迎页
<%@include file="WEB-INF/jsp/common/head.jsp"%>
欢迎光临
至此,项目所有的源代码都完了。
1.建名字为work的数据库
2.建表job,worker,worker_job
job表的项
worker表的设计
worker_job表的设计
到此结束!
如果有好的建议,欢迎留言。