Handwritten SSM Add/Delete Change Framework Set

Keywords: xml Spring Java JSP

1. Create entity classes

lombok-1.18.4.jar: (guide, short for pepper) used to simplify the code with annotations instead of get set accessors, with and without parameters, toString method.

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
	private int eid;
	private String ename;
	private String epwd;
	private double money;

}

2. Create beans.xml

beans.xml was created in Spring Bean Definition file

Next comes the code area:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///mybatis"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="typeAliasesPackage" value="com.wjt.po"></property>
		<property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"></property>
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"></property>
		<property name="basePackage" value="com.wjt.dao"></property>
	</bean>
</beans>

3. Create Spring-mvc.xml

Spring-mvc.xml is to protect jsp pages from external access by configuring prefixes and suffixes, Spring-mvc.xml to add context
Code area

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!--scanning Spring annotation -->
	<context:component-scan base-package="com.wjt"></context:component-scan>
	<!-- Configuration view parser -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- Prefix configuration -->
		<property name="prefix" value="/WEB-INF/views/"></property>
		<!-- Configuration suffix -->
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

4. Create Mapper.xml

Mapper.xml is for writing sql statements

<?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.wjt.dao.EmpDao">
	<select id="findAll" resultType="Emp">
		select * from emp
	</select>
	<select id="findbyid" resultType="Emp" >
		select * from emp where eid=#{eid}
	</select>

	<delete id="delete">
		delete from emp where eid=#{eid}
	</delete>

	<insert id="insert">
		insert into emp(ename,epwd,money)
		values(#{ename},#{epwd},#{money})
	</insert>

	<update id="update">
		update emp set
		ename=#{ename},epwd=#{epwd},money=#{money} where eid=#{eid}
	</update>
</mapper>

5. Create Dao interface

package com.wjt.dao;

import java.util.List;

import com.wjt.po.Emp;

public interface EmpDao {

	List<Emp> findAll();

	Emp findbyid(int eid);

	int delete(int eid);

	int insert(Emp emp);

	int update(Emp emp);

}

6. Create Service Interface

String reception is used to write the bullet box output characters in the implementation class

package com.wjt.service;

import java.util.List;

import com.wjt.po.Emp;

public interface EmpService {

	List<Emp> findAll();

	Emp findbyid(int eid);

	String delete(int eid);

	String insert(Emp emp);

	String update(Emp emp);

}

7. Create Service Implementation Class ServiceImpl

Inheritance service interface

package com.wjt.service.impl;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.wjt.dao.EmpDao;
import com.wjt.po.Emp;
import com.wjt.service.EmpService;

@Service
public class EmpServiceImpl implements EmpService {
	@Resource
	private EmpDao ed;
      
	@Override
	public List<Emp> findAll() {
		// TODO Auto-generated method stub
		return ed.findAll();
	}

	@Override
	public String delete(int eid) {
		// TODO Auto-generated method stub

		return ed.delete(eid) > 0 ? "Delete successful" : "Delete failed";
	}

	@Override
	public String insert(Emp emp) {
		// TODO Auto-generated method stub
		return ed.insert(emp) > 0 ? "New success" : "New failure";
	}

	@Override
	public Emp findbyid(int eid) {
		// TODO Auto-generated method stub
		return ed.findbyid(eid);
	}

	@Override
	public String update(Emp emp) {
		// TODO Auto-generated method stub
		return ed.update(emp) > 0 ? "Modified success" : "Modification failed";
	}

}

8. Create Handler

@ Controller labels the Handler class as a controller instead of a Servlet

package com.wjt.handler;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wjt.po.Emp;
import com.wjt.service.EmpService;

@Controller  //Mark this class as a controller
public class Handler {
	@Resource
	private EmpService es;

	// Query all
	@RequestMapping("/findall")
	public String test(Map<String, Object> map) {
		map.put("list", es.findAll());
		return "findall";
	}

	// Query single
	@RequestMapping("/findbyid")
	public String findbyid(int eid, Map<String, Object> map) {
		map.put("list", es.findbyid(eid));
		return "update";
	}

	// modify
	@RequestMapping("/update")
	public void update(Emp emp, HttpServletResponse res) {
		mtalert(es.update(emp), res);

	}

	// Jump to add pages
	@RequestMapping("/toinsert")
	public String insert() {
		return "insert";
	}

	// Newly added
	@RequestMapping("/add")
	public void toinsert(Emp emp, HttpServletResponse res) {
		mtalert(es.insert(emp), res);
	}

	// delete
	@RequestMapping("/del")
	public void delete(int eid, HttpServletResponse res) {
		mtalert(es.delete(eid), res);
	}

	// Custom Method
	public void mtalert(String msg, HttpServletResponse res) {

		try {
			res.setContentType("text/html; charset=UTF-8");
			res.getWriter().print("<script>alert('" + msg + "');location.href='findall'</script>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

9. Create the query page findall.jsp

All JSPS should be placed under the views folder

Note the import of the C label:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ 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.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div>

		<table align="center" border="1" width="50%">
			<tr align="center">
				<td>number</td>
				<td>Name</td>
				<td>Password</td>
				<td>wages</td>
				<td>operation</td>
			</tr>
			<c:forEach items="${list }" var="li">
				<tr align="center">
					<td><a href="findbyid?eid=${li.eid}"> ${li.eid}</a></td>
					<td>${li.ename }</td>
					<td>${li.epwd }</td>
					<td>${li.money }</td>
					<td><a href="del?eid=${li.eid}">delete</a></td>
				</tr>
			</c:forEach>

		</table>
		<a href="toinsert">Add information</a>
	</div>
</body>
</html>

10. Create a new page: insert.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="add" method="post">

		<p>
			Full name<input type="text" name="ename">
		</p>
		<p>
			Password<input type="password" name="epwd">
		</p>
		<p>
			wages<input type="text" name="money">
		</p>
		<p>
			<input type="submit" value="Add to">
		</p>

	</form>
</body>
</html>

11. Create a modified page update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="update" method="post">
		<p>
			number<input type="text" name="eid" value="${list.eid }"
				readonly="readonly">
		</p>
		<p>
			Full name<input type="text" name="ename" value="${list.ename }">
		</p>
		<p>
			Password<input type="password" name="epwd" value="${list.epwd }">
		</p>
		<p>
			wages<input type="text" name="money" value="${list.money }">
		</p>
		<p>
			<input type="submit" value="Submission"> <input type="reset"
				value="cancel">
		</p>

	</form>
</body>
</html>

Comprehensive Tree Map

12. Write web.xml

classpath:beans.xml

<display-name>3166SpringSpringMVCMybatis</display-name>
	<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

classpath:Spring-mvc.xml

<!-- The front controller of this Spring Web application, responsible for 
		handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:Spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

Guide packages used in this project

Posted by EdN on Thu, 03 Oct 2019 08:06:40 -0700