eclipse+maven+springboot+mybatis+jsp实现增删改查web项目的详细步骤

1、搭建环境

(1)、搭建环境准备工作

a、官网下载eclipse
b、官网下载maven,并配置maven环境,和本地仓库路径
c、eclipse配置maven
eclipse+maven+springboot+mybatis+jsp实现增删改查web项目的详细步骤_第1张图片
eclipse+maven+springboot+mybatis+jsp实现增删改查web项目的详细步骤_第2张图片

(2)、新建项目

(1)file—》new—》maven project
eclipse+maven+springboot+mybatis+jsp实现增删改查web项目的详细步骤_第3张图片
(2)点击next
eclipse+maven+springboot+mybatis+jsp实现增删改查web项目的详细步骤_第4张图片
(3)next
eclipse+maven+springboot+mybatis+jsp实现增删改查web项目的详细步骤_第5张图片
(4)finish完成
(5)哈哈到此环境就搭建完了

2、代码和框架构架

eclipse+maven+springboot+mybatis+jsp实现增删改查web项目的详细步骤_第6张图片
(1)、dao

@Mapper
public interface UserDAO extends BaseMapper<User> {

	List<User> selectUser();

	List<User> selectByUserId(String userId);

	User selectOneById(String userId);

	void addUser(User user);

	void updateUser(User user);

	void deleteByUserId(String userId);
}

(2)、server

public interface UserService {
	List<User> getUser();

	void addUser(User user);

	List<User> selectByUserId(String userId);

	void updateUser(User user);

	void deleteByUserId(String userId);

	User selectOneById(String userId);
}

(3)、serverimpl

@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDAO userDao;

	@Override
	public List<User> getUser() {
		return userDao.selectUser();
	}

	@Override
	public void addUser(User user) {
		userDao.addUser(user);
	}

	@Override
	public List<User> selectByUserId(String userId) {
		List<User> selectByUserId = userDao.selectByUserId(userId);
		return selectByUserId;
	}

	@Override
	public void updateUser(User user) {
		userDao.updateUser(user);
	}

	@Override
	public void deleteByUserId(String userId) {
		userDao.deleteByUserId(userId);
	}

	@Override
	public User selectOneById(String userId) {
		return userDao.selectOneById(userId);
	}

}

(4)、entity

public class User {

	  private String userId ;
	  private String userCode ;
	  private String userName;
		public String getUserId() {
			return userId;
		}
		public void setUserId(String userId) {
			this.userId = userId;
		}
		public String getUserCode() {
			return userCode;
		}
		public void setUserCode(String userCode) {
			this.userCode = userCode;
		}
		public String getUserName() {
			return userName;
		}
		public void setUserName(String userName) {
			this.userName = userName;
		}
		@Override
		public String toString() {
			return "User [userId=" + userId + ", userCode=" + userCode + ", userName=" + userName + "]";
		}
		  
	}

(5)、application.properties

server.port=8081
context-path:/demoWeb

spring.application.name=demoWeb
server.tomcat.uri-encoding=utf-8
 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/mysql?setUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
 
server.servlet.session.timeout=30m
mybatis.typeAliasesPackage=com.springboot.entity
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

spring.thymeleaf.content-type: text/html 
spring.thymeleaf.cache: false
spring.thymeleaf.mode: LEGACYHTML5

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

(6)、mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springboot.dao.UserDAO">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.springboot.entity.User">
        <id column="userId" property="userId" />
        <result column="userCode" property="userCode" />
        <result column="userName" property="userName" />
    </resultMap>
	<select id="selectUser"  resultType="com.springboot.entity.User">
        SELECT * FROM userstu
    </select>
    <select id="selectByUserId"  resultType="com.springboot.entity.User" parameterType="String">
        SELECT * FROM userstu where userId=#{userId}
    </select>
    
    <select id="selectOneById"  resultType="com.springboot.entity.User" parameterType="String">
        SELECT * FROM userstu where userId=#{userId}
    </select>
    
    <insert id="addUser" parameterType="com.springboot.entity.User">
    	insert into userstu(userId,userCode,userName)values(#{userId},#{userCode},#{userName})
    </insert>
    
    <update id="updateUser" parameterType="com.springboot.entity.User">
    	update userstu set userCode = #{userCode},userName = #{userName} where userId = #{userId}
    </update>
    
    <delete id="deleteByUserId" parameterType="String">
    	delete from userstu where userId = #{userId}
    </delete>
</mapper>

(7)、controller

@Controller
@RequestMapping("test")
public class UserController {

	@Autowired
	private UserService userService;

	@ApiOperation(value = "获取用户列表")
	@RequestMapping("index")
	public ModelAndView select() {
		ModelAndView mv = new ModelAndView();
		List<User> user = userService.getUser();
		mv.addObject("user", user);
		mv.setViewName("index");
		return mv;
	}

	// 通过id删除学生信息
	@RequestMapping("/deleteStudentById")
	public String deleteStudent(String id) {
		userService.deleteByUserId(id);
		return "redirect:index";
	}

	// 通过id查询学生的信息
	@RequestMapping("/selectStudentById")
	public ModelAndView selectStudentById(String id) {
		ModelAndView mv = new ModelAndView();// 创建模型视图对象
		List<User> user = userService.selectByUserId(id);
		mv.addObject("user", user);
		mv.setViewName("selectOne");
		return mv;
	}

	// 通过id信息(更新学生信息)
	@RequestMapping("/selectOneUserById")
	public ModelAndView selectOneUserByIds(String id) {
		ModelAndView mv = new ModelAndView();
		User user = userService.selectOneById(id);
		mv.addObject("user", user);
		mv.setViewName("update");
		return mv;
	}

	// 通过id信息
	@RequestMapping("/updateStudentById")
	public String updateStudent(User user) {
		userService.updateUser(user);
		return "redirect:index";
	}

	// 通过id信息
	@RequestMapping("/addUser")
	public String addUsers(User user) {
		userService.addUser(user);
		return "redirect:index";
	}

	@RequestMapping("/add")
	public ModelAndView addController(User user) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("add");
		return mv;
	}

	@RequestMapping("/adminLeft")
	public ModelAndView adminLef() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("adminLeft");
		return mv;
	}

	@RequestMapping("/indexUser")
	public ModelAndView indexUser() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("adminIndex");
		return mv;
	}

	@RequestMapping("/adminList")
	public ModelAndView adminLis() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("adminList");
		return mv;
	}

	@RequestMapping("/adminTop")
	public ModelAndView adminTo() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("adminTop");
		return mv;
	}

}

(8)、Application.java

@MapperScan("com.springboot.dao")
@EnableCaching
@EnableAutoConfiguration(exclude = { JpaRepositoriesAutoConfiguration.class })
@SpringBootApplication(exclude = { DataSourceTransactionManagerAutoConfiguration.class })
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

(9)、jsp
1 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>

<head>
<style>
.tab {
	border-top: 1px solid #000;
	border-left: 1px solid #000;
	text-align: center
}

.tab td {
	border-bottom: 1px solid #000;
	border-right: 1px solid #000;
}

#header {
	background-color: #5CADAD;
	color: white;
	text-align: center;
	padding: 5px;
}

#nav {
	line-height: 30px;
	background-color: #B3D9D9;
	height: 500px;
	width: 100px;
	float: left;
	padding: 5px;
	text-align: center;
}

#section {
	height: 500px;
	width: 1212px;
	background-color: #F5F5F5;
	text-align: center;
	float: left;
	padding: 5px;
	color: blue;
}

#footer {
	background-color: #B3D9D9;
	color: white;
	clear: both;
	text-align: center;
	padding: 5px;
}
</style>

</head>
<body>
	<div id="header">
		<h1>Welcome to my world!</h1>
	</div>
	<div id="nav"><br>
		 <a href="user"> 查 询</a><br> <br>
		 <a href="add"> 新 增</a><br>
	</div>

	<div id="section">
	<br>	<br>	<br>	<br>	<br> 	<br>	<br>
	<h2>人生若只如初见,何事秋风悲画扇</h2>						
	</div>
	<div id="footer">Good luck !</div>
</body>
</html>

2 add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>

<head>
<style>
.tab {
	border-top: 1px solid #000;
	border-left: 1px solid #000;
	text-align: center
}

.tab td {
	border-bottom: 1px solid #000;
	border-right: 1px solid #000;
}

#header {
	background-color: #5CADAD;
	color: white;
	text-align: center;
	padding: 5px;
}

#nav {
	line-height: 30px;
	background-color: #B3D9D9;
	height: 500px;
	width: 100px;
	float: left;
	padding: 5px;
	text-align: center;
}

#section {
	height: 500px;
	width: 1212px;
    background-color: #F5F5F5;
	text-align: center;
	float: left;
	padding: 5px;
}

#footer {
	background-color: #B3D9D9;
	color: white;
	clear: both;
	text-align: center;
	padding: 5px;
}
</style>
</head>

<body>

	<div id="header">
		<h1>Welcome to my world! </h1>
	</div>

	<div id="nav"><br>
	</div>

	<div id="section">
		<form action="addUser" method="post">
			<table class="tab" cellspacing="0" cellpadding="0" width="600" height="200">
				<tr>
					<td width="80px" height="30px">学号:</td>
					<td width="80px" height="30px"><input type="text"
						name="userId" value="${user.userId}" /></td>
				</tr>
				<tr>
					<td width="80px" height="30px">编号:</td>
					<td width="80px" height="30px"><input type="text"
						name="userCode" value="${user.userCode}" /></td>
				</tr>
				<tr>
					<td width="80px" height="30px">姓名:</td>
					<td width="80px" height="30px"><input type="text"
						name="userName" value="${user.userName}" /></td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" value="增加"> <a
						href="javascript:history.go(-1);" class="FunctionButton"><input
							type="button" value="返回"></a></td>
				</tr>
			</table>
	</form>
	</div>

	<div id="footer">Good luck !</div>

</body>
</html>

3 update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>

<head>
<style>
.tab {
	border-top: 1px solid #000;
	border-left: 1px solid #000;
	text-align: center
}

.tab td {
	border-bottom: 1px solid #000;
	border-right: 1px solid #000;
}

#header {
	background-color: #5CADAD;
	color: white;
	text-align: center;
	padding: 5px;
}

#nav {
	line-height: 30px;
	background-color: #B3D9D9;
	height: 500px;
	width: 100px;
	float: left;
	padding: 5px;
	text-align: center;
}

#section {
	height: 500px;
	width: 1212px;
    background-color: #F5F5F5;
	text-align: center;
	float: left;
	padding: 5px;
}

#footer {
	background-color: #B3D9D9;
	color: white;
	clear: both;
	text-align: center;
	padding: 5px;
}
</style>
</head>

<body>

	<div id="header">
		<h1>Welcome to my world! </h1>
	</div>

	<div id="nav"><br>
	</div>

	<div id="section">
		<form action="updateStudentById" method="post">
			<table class="tab" cellspacing="0" cellpadding="0" width="600" height="200">
				<tr>
					<td width="80px" height="30px">学号:</td>
					<td width="180px" height="30px"><input type="text"
						width="180px" height="30px" name="userId" value="${user.userId}" /></td>
				</tr>
				<tr>
					<td width="80px" height="30px">编号:</td>
					<td width="180px" height="30px"><input type="text"
						width="180px" height="30px" name="userCode"
						value="${user.userCode}" /></td>
				</tr>
				<tr>
					<td width="80px" height="30px">姓名:</td>
					<td width="180px" height="30px"><input type="text"
						width="180px" height="30px" name="userName"
						value="${user.userName}" /></td>
				</tr>
				<tr>
					<td ></td>
					<td><input type="submit" value="修改"
						class="FunctionButtonInput"> <a
						href="javascript:history.go(-1);" class="FunctionButton"><input
							type="button" value="返回"></a></td>
				</tr>
			</table>
		<from/>
	</div>

	<div id="footer">Good luck !</div>

</body>
</html>

4 selectOne.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<meta charset="UTF-8">

<title>Insert title here</title>
<style type="text/css">
  .tab{border-top:1px solid #000;border-left:1px solid #000;text-align:center}
  .tab td{border-bottom:1px solid #000;border-right:1px solid #000;}
</style>
<script>
	function users(){
		alter("123")
	}
</script>
</head>
<body>
<center>
        <table class="tab" cellspacing="0" cellpadding="0" width="600" height="200">
            <tr align="center" valign="middle" >
				<td>学号</td>
				<td>编号</td>
				<td>ˆ姓名</td>
				<td>操作</td>
			</tr>
			<c:forEach items="${user}" var="st">
				<tr align="center" valign="middle">
					<td>${st.userId}</td>
					<td>${st.userCode}</td>
					<td>${st.userName}</td>
					<td >
                    	  <a href="deleteStudentById?id=${st.userId}">删除</a>
                   		 <a href="selectOneUserById?id=${st.userId}">修改</a>
                   		 <a href="javascript:history.go(-1);" class="FunctionButton"><input type="button" value="返回"></a>
                    </td>
				</tr>
			</c:forEach>
	</table>
</center>
</body>
</html>

5 user.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>

<head>
<style>
.tab {
	border-top: 1px solid #000;
	border-left: 1px solid #000;
	text-align: center
}

.tab td {
	border-bottom: 1px solid #000;
	border-right: 1px solid #000;
}

#header {
	background-color: #5CADAD;
	color: white;
	text-align: center;
	padding: 5px;
}

#nav {
	line-height: 30px;
	background-color: #B3D9D9;
	height: 500px;
	width: 100px;
	float: left;
	padding: 5px;
	text-align: center;
}
#nav1 {
	background-color: #B3D9D9;
	color: white;
	clear: both;
	text-align: center;
	padding: 5px;
}

#section {
	height: 500px;
	width: 1212px;
    background-color: #F5F5F5;
	text-align: center;
	float: left;
	padding: 5px;
}

#footer {
	background-color: #B3D9D9;
	color: white;
	clear: both;
	text-align: center;
	padding: 5px;
}
</style>
</head>

<body>

	<div id="header">
		<h1>Welcome to my world! </h1>
	</div>

	<div id="nav"><br>
	<a href="index" class="FunctionButton">返回</a><br>
	<a href="add"> 新增</a>
	</div>

	<div id="section">
		<table class="tab" cellspacing="0" cellpadding="0" width="600"
			height="200">
			<tr align="center" valign="middle">
				<td>学号</td>
				<td>编号</td>
				<td>姓名</td>
				<td>操作</td>
			</tr>

			<c:forEach items="${user}" var="st">
				<tr align="center" valign="middle">
					<td>${st.userId}</td>
					<td>${st.userCode}</td>
					<td>${st.userName}</td>
					<td><a href="deleteStudentById?id=${st.userId}">删除 </a> <a
						href="selectOneUserById?id=${st.userId}">修改</a> <a
						href="selectOneUserById?id=${st.userId}">查询</a></td>
				</tr>
			</c:forEach>

		</table>
	</div>
	<div id="footer">Good luck !</div>

</body>
</html>

(10)、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xxx.ssm</groupId>
  <artifactId>demoWeb</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>demoWeb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
	</parent>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  <!-- Spring Boot web依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- Spring Boot 热部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<!-- tomcat的支持. -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- 访问JSP页面必须配置 -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		</dependency>
     	 <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
         </dependency>
      	<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- web开发包:包含Tomcat和Springmvc -->
        
          <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
       
		<dependency>           
			<groupId>tk.mybatis</groupId>            
			<artifactId>mapper-spring-boot-starter</artifactId>            
			<version>2.0.0</version>        
		</dependency>
		<!-- thymeleaf -->
		<!-- <dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency> -->
		<dependency> 
			<groupId>net.sourceforge.nekohtml</groupId> 
			<artifactId>nekohtml</artifactId> 
		</dependency> 
		<!--帮助页面-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
 
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
        
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
             <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId> 
            <scope>provided</scope>
        </dependency>
         <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jsp-api</artifactId>
        </dependency>
		
	</dependencies>
	<build>
		<plugins>
       		  <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
          <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
    	</plugins>
	<resources>
		<resource>
			<directory>src/main/java</directory>
			<includes>
				<include>**/*.yml
				**/*.properties</include>
				<include>**/*.xml
			
			false
		
		
			src/main/resources
			
				**/*.yml</include>
				<include>**/*.properties
				**/*.xml</include>
			</includes>
			<filtering>false</filtering>
		</resource>
	</resources>
	</build>
</project>


3、访问地址和结果展示

(1)请求地址
http://localhost:8081/test/index
(2)、结果展示
eclipse+maven+springboot+mybatis+jsp实现增删改查web项目的详细步骤_第7张图片

你可能感兴趣的:(eclipse+maven+springboot+mybatis+jsp实现增删改查web项目的详细步骤)