**此篇代码是在SSM框架的下编写的增加,删除,修改,查询等操作,流程思路如下:**
** **1,在controller层创建UserController类;在service层创建Userservice接口,再创建一个实现类的包(impl)在包内创建一个UserServiceImpl的实现类,再在持久成(Dao层)创建UserDao接口;在resources中创建一个路径和UserDao接口一样的UserDao.xml文件,创建好的包结构如下:
**2,开始代码编写(首先要清楚Controller层是实现后端与前端页面的跳转,Controller调用service层,service层调用Dao层,UserDao.xml文件实现sql语句的编写)**
(1),UserController中代码
package com.sm.controller; import com.sm.domain.User; import com.sm.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/findAll") public String findAll(Model model){ List<User> users = userService.findAll(); System.out.println(users); model.addAttribute("users",users); return "list"; } // 增加数据 先跳转页面再保存数据 @RequestMapping("/addjsp") public String addjsp(){ return "add"; } @RequestMapping("/saveUser") public String saveUser(User user){ userService.saveUser(user); return "redirect:/findAll"; } // 修改数据 先用id查询 @RequestMapping("/findUser") public ModelAndView findUser(Integer id){ // 转发到页面 ModelAndView mv=new ModelAndView(); User user=userService.findUser(id); mv.addObject(user); mv.setViewName("update"); return mv; } // 修改保存 @RequestMapping("/updateUser") public String updateUser(User user){ userService.updateUser(user); return "redirect:/findAll"; } // 按照id删除数据 @RequestMapping("/delUser") public String delUser(Integer id){ System.out.println(id); userService.delUser(id); return "redirect:/findAll"; } // 分页查询 @RequestMapping("/findAllByPage") public ModelAndView findAllByPage(@RequestParam(name = "page",required = true,defaultValue = "1") Integer page, @RequestParam(name = "size",required = true,defaultValue = "3") Integer size){ ModelAndView mv=new ModelAndView(); List<User> users=userService.findAllByPage(page,size); //把查询到的数据交给分页模型处理 PageInfo pageInfo =new PageInfo(users); mv.addObject("pageInfo",pageInfo); mv.setViewName("list"); return mv; } }
(2)UserService接口代码
package com.sm.service; import com.sm.domain.User; import java.util.List; public interface UserService { List<User>findAll(); void saveUser(User user); void delUser(Integer id); User findUser(Integer id); void updateUser(User user); }
(3)UserServiceImpl类代码
package com.sm.service.impl; import com.sm.dao.UserDao; import com.sm.domain.User; import com.sm.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("userService") public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public List<User> findAll() { return userDao.findAll(); } @Override public void saveUser(User user) { userDao.saveUser(user); } @Override public void delUser(Integer id) { userDao.delUser(id); } @Override public User findUser(Integer id) { return userDao.findUser(id); } @Override public void updateUser(User user) { userDao.updateUser(user); } }
(4)UserDao代码
package com.sm.dao; import com.sm.domain.User; import org.springframework.stereotype.Component; import java.util.List; @Component public interface UserDao { List<User> findAll(); void saveUser(User user); void delUser(Integer id); void updateUser(User user); User findUser(Integer id); }
当然不要忘了实体类(User)的创建
(5)UserDao.xml文件
<?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.sm.dao.UserDao"> <!--数据库总属性和User中属性不同时可以用 --> <!-- <resultMap id="userMap" type="user">--> <!-- <id column="u_id" property="id"></id>--> <!-- <result column="u_name" property="username"></result>--> <!-- <result column="u_gender" property="usergender"></result>--> <!-- <result column="u_address" property="useraddress"></result>--> <!-- </resultMap>--> <select id="findAll" resultType="User"> select * from user </select> <insert id="saveUser" parameterType="User"> insert into user (username,usergender,useraddress)values (#{username},#{usergender},#{useraddress}) </insert> <select id="findUser" parameterType="int" resultType="user"> select * from user where id=#{id} </select> <update id="updateUser" parameterType="User"> update user set username=#{username},usergender=#{usergender},useraddress=#{useraddress} where id=#{id} </update> <delete id="delUser" parameterType="int"> delete from user where id=#{id} </delete> </mapper>
3,前端页面
(1)index.jsp页面主要是用于跳转到主页面中主要代码
<a href="findAll">查询用户</a>
(2)核心页面
<table class="table table-hover"> <h3><a href="/addjsp"><button type="button" class="btn btn-warning">添加用户</button></a></h3> <%-- 表格设计--%> <tr> <th>id</th> <th>name</th> <th>gender</th> <th>address</th> </tr> <%-- 获取值--%> <c:forEach items="${users}" var="user"> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.usergender}</td> <td>${user.useraddress}</td> <td> <button type="button" class="btn btn-warning" onclick="updateUser('${user.id}')">编辑</button> <button type="button" class="btn btn-danger" onclick="delUser('${user.id}')" >删除</button> </td> </tr> </c:forEach> <tr> <td> <button type="button" class="btn btn-danger" >全删除</button> </td> </tr> </table> <script> <%-- 删除的方法--%> function delUser(id){ var flag=confirm("确定删除?"); if(flag){ location.href="delUser?id="+id; } } function updateUser(id) { location.href="findUser?id="+id; } </script>
最终页面展示:
版权归原作者 云端的数据 所有, 如有侵权,请联系我们删除。