由于第一次写博客,没排版经验,spring、springmvc、mybatis-config等配置写在了项目代码后面,造成不便,多多谅解
jdk:1.8
IDEA:2018.2.4
Boostrap:4
本项目版本为 tomcat 7
配置如下图
- entity下书写模型类 要和数据库表中的列名对应
package com.ssm.demo.entity;
public class User {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String username;
private String password;
}
package com.ssm.demo.entity;
public class Worker {
private Integer wid;
@Override
public String toString() {
return "Worker{" +
"wid=" + wid +
", wname='" + wname + '\'' +
", wjob='" + wjob + '\'' +
", waddress='" + waddress + '\'' +
'}';
}
private String wname;
private String wjob;
private String waddress;
public Integer getWid() {
return wid;
}
public void setWid(Integer wid) {
this.wid = wid;
}
public String getWname() {
return wname;
}
public void setWname(String wname) {
this.wname = wname;
}
public String getWjob() {
return wjob;
}
public void setWjob(String wjob) {
this.wjob = wjob;
}
public String getWaddress() {
return waddress;
}
public void setWaddress(String waddress) {
this.waddress = waddress;
}
}
- dao层代码书写 注意此层文件和 resources的mapper目录下文件对应
package com.ssm.demo.dao;
import org.apache.ibatis.annotations.Param;
import com.ssm.demo.entity.User;
public interface UserDao {
/**
* 查找用户名和密码
* @param username 登录用户名
* @return
*/
User findByUsername(String username);
/**
* 注册用户和密码
*/
void registerByUsernameAndPassword(@Param("username") String username,
@Param("password") String password);
void updateByUsernameAndPassword(@Param("username") String username,@Param("password") String password);
}
这里用到了动态sql语句
package com.ssm.demo.dao;
import com.ssm.demo.entity.Worker;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface WorkerDao {
List getAllUser();
void add(@Param("wname") String wname, @Param("wjob") String wjob, @Param("waddress") String waddress);
void delete(@Param("wid") Integer wid);
void update(@Param("wid") Integer wid,@Param("wname") String wname, @Param("wjob") String wjob, @Param("waddress") String waddress);
List getWorkerByJob(@Param("wjob") String wjob);
}
- mapper下写查询语句的dao层映射
insert into user (username,password)
values (#{username},#{password})
update user set password = #{password} where username = #{username}
insert into worker(wname,wjob,waddress) values (#{wname},#{wjob},#{waddress})
delete from worker where wid =#{wid}
update worker set wname=#{wname},wjob=#{wjob},waddress=#{waddress} where wid=#{wid}
- 下面来写service层的内容,service下写接口,service下还有子包serviceimpl,里面写同名接口继承了service的类,同时 还引入了dao层。
package com.ssm.demo.service;
import com.ssm.demo.entity.User;
public interface UserService {
//检验用户登录
User checkLogin(String username, String password);
void Regist(User user);
void updateUser(User user);
}
package com.ssm.demo.service.impl;
import com.ssm.demo.dao.UserDao;
import com.ssm.demo.entity.User;
import com.ssm.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
/*
* 检验用户登录业务
*
*/
@Override
public User checkLogin(String username, String password) {
User user = userDao.findByUsername(username);
if(user != null && user.getPassword().equals(password)){
return user;
}
return null;
}
@Override
public void Regist(User user) {
userDao.registerByUsernameAndPassword(user.getUsername(),user.getPassword());
}
@Override
public void updateUser(User user) {
userDao.updateByUsernameAndPassword(user.getUsername(),user.getPassword());
}
}
package com.ssm.demo.service;
import com.ssm.demo.entity.Worker;
import java.util.List;
public interface WorkerService {
List list();
void add(Worker worker);
void delete(Worker worker);
void update(Worker worker);
List listbyjob(Worker worker);
}
package com.ssm.demo.service.impl;
import com.ssm.demo.dao.WorkerDao;
import com.ssm.demo.entity.Worker;
import com.ssm.demo.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WorkerServiceImpl implements WorkerService {
@Autowired
private WorkerDao workerDao;
/**
* 查询所有用户
* @return
*/
@Override
public List list() {
return workerDao.getAllUser();
}
@Override
public void add(Worker worker) {
workerDao.add(worker.getWname(),worker.getWjob(),worker.getWaddress());
}
@Override
public void delete(Worker worker) {
workerDao.delete(worker.getWid());
}
@Override
public void update(Worker worker) {
workerDao.update(worker.getWid(),worker.getWname(),worker.getWjob(),worker.getWaddress());
}
@Override
public List listbyjob(Worker worker) {
return workerDao.getWorkerByJob(worker.getWjob());
}
}
-最后来写我们的controller层,为了图省事我将两个表的controller写在了一起,注释有区分
package com.ssm.demo.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.demo.entity.Worker;
import com.ssm.demo.service.WorkerService;
import com.ssm.demo.service.impl.WorkerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ssm.demo.entity.User;
import com.ssm.demo.service.UserService;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/user")
//这里用了@SessionAttributes,可以直接把model中的user(也就key)放入其中
//这样保证了session中存在user这个对象
@SessionAttributes("user")
public class UserController {
@Autowired
private UserService userServivce;
@Autowired
private WorkerService pageService;
//正常访问login页面
@RequestMapping("/login")
public String login(){
return "login";
}
//表单提交过来的路径
@RequestMapping("/checkLogin")
public String checkLogin(User user, Model model){
user = userServivce.checkLogin(user.getUsername(), user.getPassword());
if(user != null){
model.addAttribute("user",user);
return "success";
}
return "fail";
}
//测试超链接跳转到另一个页面是否可以取到session值
@RequestMapping("/anotherpage")
public String hrefpage(){
return "anotherpage";
}
@RequestMapping("/back")
public String back(){
return "login";
}
//注销方法
@RequestMapping("/outLogin")
public String outLogin(HttpSession session){
session.invalidate();
return "login";
}
@RequestMapping("/add")
public String add(){
return "add";
}
@RequestMapping("/delete")
public String delete(){
return "delete";
}
@RequestMapping("/update")
public String update(){
return "update";
}
@RequestMapping("/index")
public String index(){
return "index";
}
@RequestMapping("/updatepassword")
public String updatepassword(){
return "updatepassword";
}
@RequestMapping("/regist")
public String regist(){
return "regist";
}
@RequestMapping("/query")
public String query(){
return "query";
}
@RequestMapping("/doRegist")
public String doRegist(User user, Model model,HttpSession session){
User userFromDb = userServivce.checkLogin(user.getUsername(), user.getPassword());
if(userFromDb == null){
userServivce.Regist(user);
session.invalidate();
return "login";
}
return "regist";
}
@RequestMapping("/updateUser")
public String updateUser(User user, Model model){
if(user != null){
userServivce.updateUser(user);
return "login";
}
return "fail";
}
//woker的操作在下面!
@RequestMapping(value="/list",method= RequestMethod.GET)
public String pageList(ModelMap map, @RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){
Integer pageSize=4;//每页显示记录数
//分页查询
PageHelper.startPage(pageNo, pageSize);
List workerList = pageService.list();//获取所有用户信息
PageInfo pageInfo=new PageInfo(workerList);
map.addAttribute("pageInfo", pageInfo);
return "list";
}
@RequestMapping("/doadd")
public String add(Worker worker, Model model){
if(worker != null){
pageService.add(worker);
}
return "ok";
}
@RequestMapping("/dodelete")
public String delete(Worker worker, Model model){
if(worker != null){
pageService.delete(worker);
return "ok";
}
else{
return "fail";
}
}
@RequestMapping("/doupdate")
public String update(Worker worker, Model model){
if(worker != null){
pageService.update(worker);
return "ok";
}
else{
return "fail";
}
}
@RequestMapping(value="/doquery",method= RequestMethod.POST)
public String pageList2(Worker worker,ModelMap map, @RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){
Integer pageSize=4;//每页显示记录数
//分页查询
PageHelper.startPage(pageNo, pageSize);
List workerList2 = pageService.list();//获取所有用户信息
PageInfo pageInfo2=new PageInfo(workerList2);
map.addAttribute("pageInfo2", pageInfo2);
return "query";
}
}
4.0.0
com.ssm.demo
ssmdemo
1.0-SNAPSHOT
war
ssmdemo Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
org.slf4j
slf4j-api
1.7.12
ch.qos.logback
logback-core
1.1.1
ch.qos.logback
logback-classic
1.1.1
com.github.pagehelper
pagehelper
5.1.2
mysql
mysql-connector-java
5.1.35
runtime
c3p0
c3p0
0.9.1.2
org.mybatis
mybatis
3.3.0
org.mybatis
mybatis-spring
1.2.3
taglibs
standard
1.1.2
jstl
jstl
1.2
javax.servlet.jsp
javax.servlet.jsp-api
2.2.1
provided
com.fasterxml.jackson.core
jackson-databind
2.5.4
javax.servlet
javax.servlet-api
3.1.0
org.springframework
spring-core
4.2.0.RELEASE
org.springframework
spring-beans
4.2.0.RELEASE
org.springframework
spring-context
4.2.0.RELEASE
org.springframework
spring-jdbc
4.2.0.RELEASE
org.springframework
spring-tx
4.2.0.RELEASE
org.springframework
spring-web
4.2.0.RELEASE
org.springframework
spring-webmvc
4.2.0.RELEASE
org.springframework
spring-test
4.2.0.RELEASE
commons-io
commons-io
2.4
org.apache.commons
commons-lang3
3.4
commons-logging
commons-logging
1.1.1
ssmdemo
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
报红用alt+enter 将项目下载或自动导入
ssm的配置
spring下的三个可以合在一起写,此处为了更加清楚将其分开
spring-dao
spring-service
spring-web
mybatis-config
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm-demo?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
logback
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
web.xml
login.jsp
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
org.springframework.web.util.IntrospectorCleanupListener
userLogin
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/spring-*.xml
1
userLogin
/
到这里我们的后台页面就基本完成了
**本项目重点不在前台,故我将webapp下全部内容上传至百度云**
链接:https://pan.baidu.com/s/1uDOGafmP7jFxzicZZSOXdA
提取码:kjk6
现在只列出jsp页面以供参考
login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
AdminLTE 3 | Log in
CRUD操作系统
regist.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
AdminLTE 3 | Log in
CRUD操作系统注册
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<
SSM |员工管理 主页
主页
*---------