目录
1.创建web Maven项目
2.创建java源码文件和resources资源文件
3.创建数据库配置文件:jdbc.properties
4.项目总体目录:
5.添加spring配置文件:applicationContext.xml
6.添加springMVC配置文件:springMVC.xml
7.修改web.xml
8.创建数据库相关表
9.根据数据库表创建pojo类:User.java
10.编写dao层->mapper接口和xml文件 :UserMapper.java、UserMapper.xml
11.编写service层->UserService.java、UserServiceImpl.java
12.编写controller层->UserController.java
13.在jsp文件夹下创建edit.jsp和view.jsp
14.配置tomcat,启动服务器(记得加载项目名)
15.查看运行结果(已经成功)
16.项目源码
前言:学习maven后,感觉很厉害就搭建个项目小项目玩玩。
功能:实现表的增删改查
工具:IDEA jdk 1.8 mysql
整体项目结构:
1.创建web Maven项目
,接下来在pom文件加入项目所需依赖:
org.springframework
spring-context
4.2.4.RELEASE
org.springframework
spring-tx
4.2.4.RELEASE
org.springframework
spring-webmvc
4.2.4.RELEASE
mysql
mysql-connector-java
5.1.46
com.alibaba
druid
1.1.10
org.mybatis
mybatis
3.4.1
org.mybatis
mybatis-spring
1.3.0
org.springframework
spring-jdbc
4.2.4.RELEASE
junit
junit
4.12
test
javax.servlet
jstl
1.2
taglibs
standard
1.1.2
2.创建java源码文件和resources资源文件
3.创建数据库配置文件:jdbc.properties
resources目录下添加:jdbc.properties
#数据库配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
4.项目总体目录:
先给出项目总体目录
创建好java目录下的分层的包,resources下创建存放mapper的包,WEB-INF下创建jsp放跳转的页面(下面的配置文件中会配置这些路径)
5.添加spring配置文件:applicationContext.xml
resources目录下添加:applicationContext.xml
6.添加springMVC配置文件:springMVC.xml
resources目录下添加:springMVC.xml,并且根据文件中‘视图定位’的配置,所以在WEB-INF下创建jsp文件夹
7.修改web.xml
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC.xml
1
mvc-dispatcher
/
8.创建数据库相关表
/*
Navicat Premium Data Transfer
Source Server : phpStudy
Source Server Type : MySQL
Source Server Version : 50553
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 50553
File Encoding : 65001
Date: 16/09/2019 19:46:40
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 9 CHARACTER SET = gbk COLLATE = gbk_chinese_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, \'cheng\', \'128946\');
INSERT INTO `user` VALUES (3, \'eff\', \'f\');
INSERT INTO `user` VALUES (4, \'fwefw\', \'wef\');
INSERT INTO `user` VALUES (8, \'fwef\', \'13\');
SET FOREIGN_KEY_CHECKS = 1;
9.根据数据库表创建pojo类:User.java
pojo的编写规则:数据库字段对应类字段,字段类型要一直 。
package com.demo.pojo;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String name;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
}
10.编写dao层->mapper接口和xml文件 :UserMapper.java、UserMapper.xml
UserMapper.xml放在resources\mapper下,UserMapper.java放在com.demo.dao这个包下:
注意namespace="com.demo.dao.UserMapper",这里一定写对应的mapper接口具体路径
UserMapper接口的编写规则:
(1)方法名和对应的mapper配置文件中查询语句的id相同
(2)返回类型和resultType的类型一致,没有就是void。
(3)方法中的参数列表中的类型和parameterType一致。
(4)mapper配置文件的namespace对应mapper接口类的全路径。
UserMapper.java
package com.demo.dao;
import com.demo.pojo.User;
import java.util.List;
public interface UserMapper {
List list();
void del(int id);
void update(User user);
void add(User user);
User get(int id);
}
UserMapper.xml
update user set name=#{name} ,password=#{password} where id=#{id}
insert into user values (null,#{name},#{password});
delete from user where id=#{id}
11.编写service层->UserService.java、UserServiceImpl.java
UserService.java
package com.demo.service;
import com.demo.pojo.User;
import java.util.List;
public interface UserService {
List list();
void del(int id);
void update(User user);
void add(User user);
User get(int id);
}
UserServiceImpl.java
package com.demo.service.impl;
import com.demo.dao.UserMapper;
import com.demo.pojo.User;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper;
public List list() {
return userMapper.list();
}
public void add(User user) {
userMapper.add(user);
}
public void del(int id) {
userMapper.del(id);
}
public void update(User user) {
userMapper.update(user);
}
public User get(int id) {
return userMapper.get(id);
}
}
12.编写controller层->UserController.java
package com.demo.controller;
import com.demo.pojo.User;
import com.demo.service.UserService;
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;
@Controller
@RequestMapping("")
public class UserController {
@Autowired
UserService userService;
@RequestMapping("list")
public String list(Model model){
List us= userService.list();
model.addAttribute("us", us);
return "view";
}
@RequestMapping("add")
public String add(User user,Model model){
userService.add(user);
return "redirect:list";
}
@RequestMapping("del")
public String del(int id){
userService.del(id);
return "redirect:list";
}
@RequestMapping("editUI")
public String editUI(int id,Model model){
User user = userService.get(id);
model.addAttribute("user",user);
return "edit";
}
@RequestMapping("update")
public String update(User user){
userService.update(user);
return "redirect:list";
}
}
13.在jsp文件夹下创建edit.jsp和view.jsp
edit.jsp
<%@ page cOntentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
姓名:
密码:
view.jsp
<%@ page cOntentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
姓名:
密码:
最后把index.jsp内容修改为:
<%@ page cOntentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%
response.sendRedirect(request.getContextPath()+"/list");
%>
14.配置tomcat,启动服务器(记得加载项目名)
15.查看运行结果(已经成功)
16.项目源码
源码+sql:https://www.lanzous.com/i679e2d
如果有什么问题,可以私聊我,看见解决
欢迎不会的可以相互交流。
交流群:629383010(免费)