利用Spring框架在前端實現對資料庫的增刪改查
阿新 • • 發佈:2018-12-16
在前端頁面上顯示購物資料庫資料,並且可以這增、刪、改、查
1.首先在WEB 配置檔案
<!-- 配置DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc2.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 利用過濾器,解決中文亂碼問題--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 初始化編碼指令 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <!-- 過濾器的對映 --> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2、配置spring-mvc2.xml檔案
<!--2. 開啟註解掃描 --> <context:component-scan base-package="com.zhiyou100"/> <!-- 3.開啟MVC註解掃描 --> <mvc:annotation-driven /> <!--4. 定義檢視解析器 --> <!-- 定義檢視解析器ViewResovler --> <bean id="viewResovler" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 資料來源 --> <bean id="ds" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"> <property name="url" value="jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8"/> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!-- session工廠 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="ds"/> <property name="mapperLocations" value="classpath:com/zhiyou100/mapper/*.xml"/> </bean> <!-- 掃描指定包下的介面,自動掃包,併產生例項 模糊掃描--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zhiyou100.Dao"/> </bean>
3.建立com.zhiyou100.entity.Shop
package com.zhiyou100.entity; public class Shop { private Integer sid; private String sname; private Double price; private String dese; public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getDese() { return dese; } public void setDese(String dese) { this.dese = dese; } }
4.建立com.zhiyou100.Dao.ShopMapper 介面,以實現對資料庫進行增刪改查、
package com.zhiyou100.Dao;
import java.util.List;
import com.zhiyou100.entity.Shop;
public interface ShopMapper {
List<Shop> findAll();
List<Shop> findById(int sid);
Shop find(int sid);
void save(Shop shop);
void update(Shop shop);
void delete(int sid);
}
5.利用Mapper實現介面的方法,建立com.zhiyou100.Mapper.ShopMapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.zhiyou100.Dao.ShopMapper">
<select id="findAll"
parameterType="com.zhiyou100.entity.Shop"
resultType="com.zhiyou100.entity.Shop">
select *from shopping
</select>
<select id="findById" parameterType="integer"
resultType="com.zhiyou100.entity.Shop">
select *from shopping where sid=#{sid}
</select>
<select id="find" parameterType="integer"
resultType="com.zhiyou100.entity.Shop">
select *from shopping where sid=#{sid}
</select>
<insert id="save" parameterType="com.zhiyou100.entity.Shop">
insert into shopping values(null,#{sname},#{dese},#{price})
</insert>
<update id="update" parameterType="com.zhiyou100.entity.Shop" >
update shopping set sname=#{sname},dese=#{dese},price=#{price} where sid=#{sid}
</update>
<delete id="delete" parameterType="int">
delete from shopping where sid=#{sid}
</delete>
</mapper>
6、建立com.zhiyou100.ShopService,用於前段進行增刪改查
public interface ShopService {
List<Shop> findAll();
List<Shop> findById(int sid);
void save(Shop shop);
void update(Shop shop);
void delete(int sid);
}
7、建立com.zhiyou100.ShopServiceImp,實現對前段增刪改查
package com.zhiyou100.SericeImp;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import com.zhiyou100.Dao.ShopMapper;
import com.zhiyou100.entity.Shop;
import com.zhiyou100.serivce.ShopService;
@Controller
public class shopSerivceImp implements ShopService{
@Resource
private ShopMapper shopMapper;
public List<Shop> findAll() {
return shopMapper.findAll();
}
public List<Shop> findById(int sid) {
return shopMapper.findById(sid);
}
public void save(Shop shop) {
shopMapper.save(shop);
}
public void update(Shop shop) {
// TODO Auto-generated method stub
shopMapper.update(shop);
}
public void delete(int sid) {
// TODO Auto-generated method stub
shopMapper.delete(sid);
}
}
8.建立com.zhiyou100.ShopController,是對前端進行控制,實現頁面的跳轉
package com.zhiyou100.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zhiyou100.entity.Shop;
import com.zhiyou100.serivce.ShopService;
@Controller
@RequestMapping("/shop")
public class ShopController {
@Resource
private ShopService shopService;
@RequestMapping("/findAll.do")
public String findAll(Model model) {
List<Shop> list = shopService.findAll();
model.addAttribute("shops", list);
return "shop/list";
}
@RequestMapping("/update.do")
public String findById(Model model, int sid) {
List<Shop> list = shopService.findById(sid);
model.addAttribute("shop", list);
return "shop/update";
}
@RequestMapping("/save.do")
public String Save() {
return "shop/save";
}
@RequestMapping("/commit.do")
public String save(Shop shop) {
shopService.save(shop);
return "redirect:../shop/findAll.do";
}
@RequestMapping("/cot.do")
public String update(Shop shop) {
shopService.update(shop);
return "redirect:../shop/findAll.do";
}
@RequestMapping("/delete.do")
public String detele(int sid) {
shopService.delete(sid);
return "redirect:../shop/findAll.do";
}
}
2、在WEB-INT下建立shop資料夾,是前端頁面
2.1、 檢視shop資訊情況,list.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>
<meta charset="UTF-8">
<title>購物訂單介面</title>
</head>
<body>
<div class="box">
<h3>購物訂單介面</h3>
<table border="10" width=60%>
<tr>
<th>使用者賬號</th>
<th>使用者名稱</th>
<th>備註</th>
<th>商品價格</th>
<th>操作</th>
</tr>
<c:forEach items="${shops}" var="s">
<tr>
<td>${s.sid }</td>
<td>${s.sname }</td>
<td>${s.dese}</td>
<td>${s.price }</td>
<td><a href="save.do"> <input value="新增"></a>
<a href="update.do?sid=${s.sid} "><input value="更改"></a>
<a href="delete.do?sid=${s.sid}"><input value="刪除"></a>
</td>
</c:forEach>
</table>
</div>
</body>
</html>
2.2 、新增物品情況。save.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="box">
<h3>新增使用者資訊</h3>
<form action="commit.do" method="post">
<table>
<tr>
<td>
<td>使用者名稱</td>
<td colspan="3" class="control"><input type="text" name="sname"
placeholder="使用者名稱"></td>
</tr>
<tr>
<td>商品價格</td>
<td colspan="3" class="control"><input type="text" name="price"
placeholder="商品價格" ></td>
</tr>
<tr>
<td>備註</td>
<td colspan="3" class="control"><input type="text" name="dese"
placeholder="備註"></td>
</tr>
</table>
<div class="buttons">
${fail}<br>
<input class="btn btn-primary va-bottom"
type="submit" value="儲存">
<a class="btn btn-default" href="javascript:history.go(-1)">返回</a>
</div>
</form>
</div>
</body>
</html>
2.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>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="box">
<h3>更改使用者資訊</h3>
<form action="cot.do" method="post">
<table >
<c:forEach items="${shop}" var="s">
<tr>
<td>
<td>使用者名稱</td>
<td colspan="3" class="control"><input type="text" name="sname"
placeholder="使用者名稱" value="${s.sname}"></td>
</tr>
<tr>
<td>備註</td>
<td colspan="3" class="control"><input type="text" name="dese"
placeholder="備註" value="${s.dese}"></td>
</tr>
<tr>
<td>商品價格</td>
<td colspan="3" class="control"><input type="text" name="price"
placeholder="商品價格" value="${s.price}"></td>
</tr>
</c:forEach>
</table>
<div class="buttons">
${fail}<br>
<input class="btn btn-primary va-bottom"
type="submit" value="儲存">
<a class="btn btn-default" href="javascript:history.go(-1)">返回</a>
</div>
</form>
</div>
</body>
</html>