IntelliJ IDEA Ultimate 2017.2.2
Apache-tomcat-9.0.5
JDK 1.8.0_121
MySQL 5.7
Maven 3.3.9
工程结构
最终完成后的工程结构:
———————————————————————————————————————————————————————
1. 新建Maven项目:
点击File -> New -> Project -> Maven -> 勾选 Create from archetype -> 选择 maven-archetype-webapp (注意:此处不要错选成上面的cocoom-22-archetype-webapp)
在弹出的new project 选项卡中填写GroupId和Artifactid,其中GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构,ArtifactID是项目的名称,就是项目根目录的名称。
切记:main需要变成蓝色才能建项目,如下:
pojo: 存放自定义的java类。如:paper类,user类,book类等,每个类的属性设为private,并提供public属性的getter/setter方法让外界访问
service:定义接口,包含系统所提供的功能。(之后还会在service包下再新建impl包)。
mapper:定义接口,包含与数据库进行交互的功能。
controller:控制器,负责接收页面请求,转发和处理。
在resource包下新建Directory:“mapper”(用于存放xxxMapper.xml文件)和“spring”(用于存放spring-xxx.xml配置文件),新建文件:“jdbc.properties”(mysql数据库配置文件),”log4j.properties”(日志输出配置文件),”mybatis-config.xml”(mybatis框架配置文件)
在web-inf目录下新建“jsp”包,存放xxx.jsp显示界面。
补充之后的项目目录如下图所示,至此项目骨架搭建完毕,开始写代码实现增删改查功能。
3. 自定义Paper类
在这之前要对项目的pom.xml文件进行配置,在此文件中配置项目所需要的jar包。
pom.xml
4.0.0
com
first
war
1.0-SNAPSHOT
first Maven Webapp
http://maven.apache.org
UTF-8
UTF-8
5.0.3.RELEASE
3.4.4
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet.jsp
jsp-api
2.2
provided
javax.servlet
jstl
1.2
taglibs
standard
1.1.2
com.fasterxml.jackson.core
jackson-databind
2.9.4
mysql
mysql-connector-java
5.1.38
com.mchange
c3p0
0.9.5.2
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.1
ch.qos.logback
logback-classic
1.1.1
first
org.apache.maven.plugins
maven-compiler-plugin
1.8
本项目所用的数据库为MySQL 5.7 建表语句如下:
/*
Navicat MySQL Data Transfer
Source Server : Java实现数据库的编程
Source Server Version : 50624
Source Host : 127.0.0.1:3306
Source Database : leave
Target Server Type : MYSQL
Target Server Version : 50624
File Encoding : 65001
Date: 2019-11-07 15:42:32
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for leaverecords
-- ----------------------------
DROP TABLE IF EXISTS `leaverecords`;
CREATE TABLE `leaverecords` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`Name` varchar(255) NOT NULL COMMENT '姓名',
`Time` datetime NOT NULL COMMENT '请假时间',
`Reason` varchar(255) NOT NULL COMMENT '请假原因',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
3.
下面开始写java代码:
package com.pojo;
import lombok.Data;
/**
* Created by ASUS on 2019/11/6.
*/
public class leave {
private int id;
private String Name;
private String Time;
private String Reason;
}
其中getter/setter方法可以在定义完私有属性之后使用IDEA提供的快捷键:ALT+Insert 来实现快速添加
在service包中新建接口 LeaveService:
package com.service;
import com.pojo.leave;
import java.util.List;
/**
* Created by ASUS on 2019/11/6.
*/
public interface leaveService {
List findall();
int add(leave le);
int delete(int id);
int update(leave le);
leave findid(int id);
}
在service包下新建impl包,并在此包中新建LeaServiceImpl.java
package com.service;
import com.mapper.LeaveMapper;
import com.pojo.leave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by ASUS on 2019/11/6.
*/
@Service("leaveService")
public class leaServiceImpl implements leaveService {
@Autowired
private LeaveMapper leaveMapper;
@Override
public List findall() {
return leaveMapper.findall();
}
@Override
public int add(leave le) {
return leaveMapper.add(le);
}
@Override
public int delete(int id) {
return leaveMapper.delete(id);
}
@Override
public int update(leave le) {
return leaveMapper.update(le);
}
@Override
public leave findid(int id) {
return leaveMapper.findid(id);
}
}
在dao包中新建接口 LeaveMapper.java
package com.mapper;
import com.pojo.leave;
import java.util.List;
/**
* Created by ASUS on 2019/11/6.
*/
public interface LeaveMapper {
List findall();
int add(leave le);
int delete(int id);
int update(leave le);
leave findid(int id);
}
在controller包中新建 LeaveController.java
package com.web;
import com.pojo.leave;
import com.service.leaveService;
import org.apache.ibatis.annotations.Param;
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 java.util.List;
/**
* Created by ASUS on 2019/11/6.
*/
@Controller
public class leaveController {
@Autowired
private leaveService leave;
@RequestMapping("index.html")
public String findall(Model model){
List list = leave.findall();
model.addAttribute("list",list);
return "index";
}
@RequestMapping("add")
public String all(leave le){
int i= leave.add(le);
if(i>0){
System.out.println("添加成功");
return "redirect:/index.html";
}
return "index";
}
@RequestMapping("delete")
public String delete(int id){
int i = leave.delete(id);
if(i>0){
System.out.println("删除成功");
return "redirect:/index.html";
}
return "index";
}
@RequestMapping("update")
public String update(leave le){
int i = leave.update(le);
if(i>0){
System.out.println("修改成功");
return "redirect:/index.html";
}
return "index";
}
@RequestMapping("findid")
public String findid(@Param("id")int id, @Param("model") Model model){
leave lea = leave.findid(id);
model.addAttribute("lea",lea);
return "update";
}
}
在resource/mapper包下新建 LeaveMapper.xml(mybatis框架mapper代理开发配置文件
insert into leaverecords(Name,Time,Reason)values(#{Name},#{Time},#{Reason})
delete from leaverecords where id=#{id}
update leaverecords set Name=#{Name},Time=#{Time},Reason=#{Reason} where id=#{id}
在resource/spring包下分别新建配置文件:applicationContext.xml, mybatis-config.xml , spring-servlet.xml ,log4j.properties
applicationContext.xml配置文件
mybatis-config.xml 配置文件
spring-servlet.xml配置文件
在jdbc.properties文件中配置mysql数据库(这里的数据库名、表名,用户名,密码一定要记得修改!!!)。
配置web.xml文件
index.jsp
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-servlet.xml
1
springmvc
/
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
index.jsp文件
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2019/10/30
Time: 14:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
学员信息列表
添加学员
编号
姓名
时间
原因
${list.id}
${list.getName()}
${list.getTime()}
${list.getReason()}
删除
修改
在WEB-INF/jsp包下分别新建addLeavejsp , updateLeave.jsp
addLeave .jsp
<%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2019/10/30
Time: 15:44
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
添加学员信息
updateLeave.jsp
<%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2019/11/6
Time: 15:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
修改学员信息
4.利用tomcat进行项目部署
选择default -> tomcat -> local 选择下载安装到本地的tomcat服务器的地址
切换到Deployment选项页 点击 + 号选择 Arctifact ,
添加 项目名:war exploded 打包
最后在Application context 中选择 空白 那一项,点击 Apply 应用。
5. 运行界面
1)在网页输入路径
2)简单的查询所有页面
新增
修改页面
至此,利用IDEA+Maven+SSM框架实现的简单的增、删、改、查功能已经构建完毕!感谢阅读!