1、DAO 层的数据源和数据库连接的参数都是在配置文件中进行配置的。2、Entity层: 实体层 数据库在项目中的类3、Service层:业务层 控制业务Service层主要负责业务模块的逻辑应用设计。和DAO层一样都是先设计放接口的类,再创建实现的类,然后在配置文件中进行配置其实现的关联。接下来就可以在service层调用接口进行业务逻辑应用的处理。封装Service层的业务逻辑有利于业务逻辑的独立性和重复利用性。4、Controller层: 控制层 控制业务逻辑Controller层负责具体的业务模块流程的控制,其实就是与前台互交,把前台传进来的参数进行处理,controller层主要调用Se
2、rvice层里面的接口控制具体的业务流程,控制的配置也需要在配置文件中进行。四、概要设计Goods表Goods_user表已存储的商品信息表五、项目实现Controller层商品实现代码:用户实现代码:Dao层对应的mapper文件对应的实体:Service层:controller层:Goods:package com.controller;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired
3、;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.entity.Goods;import com.service.GoodsService;ControllerRequestMapping(/goods)public class GoodsController Autowired private GoodsService goodsService; public GoodsService getGoo
4、dsService() return goodsService; public void setGoodsService(GoodsService goodsService) this.goodsService = goodsService; RequestMapping(/selectAll public String selectAll(HttpServletRequest request) throws Exception List goods = goodsService.selectAll(); request.setAttribute(goods, goods); return f
5、orward:/product/productlist.jsp;/save public String save(Goods goods) goodsService.save(goods);/goods/selectAll.do/delete) public String delete(Integer id) throws Exception goodsService.delete(id);/selectByID public String selectByID(Integer id,HttpServletRequest request) Goods goods = goodsService.
6、selectByID(id);/product/update.jsp/updateGoods public String updateGoods(Goods goods) System.out.println(goods); goodsService.updateGoods(goods);User:import javax.servlet.http.HttpSession;import com.entity.User;import com.service.UserService;/userpublic class UserController private UserService userS
7、ervice; public UserService getUserService() return userService; public void setUserService(UserService userService) this.userService = userService; /注册/register) /接受数据 public String register(User user) throws Exception /调用Service 中的注册功能 userService.register(user); /跳转器/user/login.html /登录/login publ
8、ic String login(String user_name,String password,HttpSession session) throws Exception User user = userService.login(user_name, password); if(user!=null) session.setAttribute(name, user.getUser_name(); String name = (String)session.getAttribute(); System.out.println(name); /跳转页面 return else dao层pack
9、age com.dao;public interface GoodsDAO /查询所有商品 public List selectAll(); /添加商品 public void save(Goods goods); /删除商品 public void delete(Integer id); /通过ID查询 public Goods selectByID(Integer id); /修改商品信息 public void updateGoods(Goods goods);对应mapper文件!DOCTYPE mapper PUBLIC -/mybatis.org/DTD Mapper 3.0/EN
10、 ./mybatis-3-mapper.dtd mapper namespace=com.dao.GoodsDAO selectByID select * from goods where id=#idinsert id=save insert into goods(id,name,price,description) values(#id,#name,#price,#description)/insertdelete id=delete delete from goods where id=#id /deleteupdate id=updateGoods update goodsset pr
11、ice! price=#price,description description=#description where id =#id/set/update/mapperimport org.apache.ibatis.annotations.Param;public interface UserDAO /插入一条数据 public void save(User user); /通过user_name和password查询 public User selectByUsernameAndPassword(Param(u)String user_name,Param(p)String passw
12、ord);com.dao.UserDAO insert into goods_user (user_name,name,age,password,sex) values(#user_name,#name,#age,#password,#sex)selectByUsernameAndPasswordUser select * from goods_user where user_name=#u and password=#pEntity层:goods:package com.entity;public class Goods private Integer id; private String
13、name; private double price; private String description; 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; public double getPrice() return price; public void setPrice(double price) this
14、.price = price; public String getDescription() return description; public void setDescription(String description) this.description = description; public Goods(Integer id, String name, double price, String description) public Goods() super(); / TODO Auto-generated constructor stub Override public Str
15、ing toString() Goods id= + id + , name= + name + , price= + price + , description= + description + public class User private String user_name; private int age; private String password; private String sex; public String getUser_name() return user_name; public void setUser_name(String user_name) this.
16、user_name = user_name; public int getAge() return age; public void setAge(int age) this.age = age; public String getPassword() return password; public void setPassword(String password) this.password = password; public String getSex() return sex; public void setSex(String sex) this.sex = sex; public
17、User(String user_name, String name, int age, String password, String sex) public User() User user_name= + user_name + , age= + age + , password= + password + , sex= + sex + service层package com.service;public interface GoodsService /增加商品 /修改对应服务层接口:import org.springframework.stereotype.Service;import
18、 org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import com.dao.GoodsDAO;ServiceTransactionalpublic class GoodsServiceImpl implements GoodsService private GoodsDAO goodsDAO; public GoodsDAO getGoodsDAO() return goodsDAO; public void setGoodsDAO(GoodsDAO goodsDAO) this.goodsDAO = goodsDAO; /提高查询效率 Transactional(propagation=Propagation.SUPPORTS,readOnly=true) selectAll() goods = goodsDAO.selectAll(); return goods; public void save(Goods goods) goodsDAO.save(goods); publi
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1