在前端页面上显示购物数据库数据,并且可以这增、删、改、查
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>