鉴于MyBatis暂时不会用,所以用刚学的SpringJDBC代替,也很简洁。以下贴出几个重要的代码。
1.UserDaoImpl数据库操作实现类
1 package com.wxy.dao.impl; 2 3 import com.wxy.pojo.User; 4 import org.springframework.jdbc.core.BeanPropertyRowMapper; 5 import org.springframework.jdbc.core.JdbcTemplate; 6 import org.springframework.jdbc.core.RowMapper; 7 import org.springframework.stereotype.Repository; 8 import javax.annotation.Resource; 9 import java.util.ArrayList; 10 import java.util.List; 11 12 @Repository("userDao") 13 public class UserDaoImpl{ 14 //获取JdbcTemplate实例 15 @Resource(name="jdbcTemplate") 16 private JdbcTemplate jdbcTemplate; 17 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 18 this.jdbcTemplate = jdbcTemplate; 19 } 20 //执行SQL语句的方法 21 public void execute() { 22 jdbcTemplate.execute("create table account8( id int primary key auto_increment," 23 + "username varchar(50)," + "balance double)"); 24 } 25 //添加用户 26 27 public void addUser(User user) { 28 String sql = "insert into user(id,name,age) value("+user.getId()+",‘"+user.getName()+"‘,"+user.getAge()+")"; 29 this.jdbcTemplate.update(sql); 30 } 31 //更新用户信息 32 public void updateUser(User user){ 33 String sql = "update user set name=‘"+user.getName()+"‘,age="+user.getAge()+" where id="+user.getId()+""; 34 this.jdbcTemplate.update(sql); 35 } 36 //删除用户 37 public void deleteUser(int id) { 38 String sql = "delete from user where id =?"; 39 jdbcTemplate.update(sql,id); 40 } 41 //查询所有用户 42 public List<User> query() { 43 String listSql = "select * from user"; 44 RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class); 45 List<User> users = this.jdbcTemplate.query(listSql,rowMapper); 46 return users; 47 } 48 }
UserDaoImpl
2.UserController数据绑定和页面跳转
1 package com.wxy.controller; 2 3 import com.wxy.dao.impl.ServiceImpl; 4 import com.wxy.pojo.User; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 import org.springframework.stereotype.Controller; 9 import org.springframework.ui.Model; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestMethod; 12 13 import javax.annotation.Resource; 14 import javax.servlet.http.HttpServletRequest; 15 import javax.servlet.http.HttpServletResponse; 16 import java.util.ArrayList; 17 import java.util.List; 18 19 @Controller("userController") 20 public class UserController { 21 @Resource(name="userService") 22 ServiceImpl userService; 23 public void setUserService(ServiceImpl userService) { 24 this.userService = userService; 25 } 26 27 //跳转添加页面 28 @RequestMapping("touseradd") 29 public String useradd(){ return "useradd"; } 30 31 //执行添加操作,并跳转至成功页面 32 @RequestMapping("/useradd.action") 33 public String add(Model model, User user){ 34 userService.addUser(user); 35 return "success"; 36 } 37 38 //跳转至修改页面 39 @RequestMapping("/touserupdate") 40 public String userupdate(){ return "userupdate"; } 41 42 //执行修改操作 43 @RequestMapping("/userupdate.action") 44 public String update(User user){ 45 userService.updateUser(user); 46 return "success"; 47 } 48 49 //跳转至查询页面 50 @RequestMapping("/touserlist") 51 public String userselect(){ return "userlist"; } 52 53 //执行查询操作 54 @RequestMapping("/userselect.action") 55 public String query(HttpServletRequest request, HttpServletResponse response) { 56 List looklist = userService.query(); 57 request.setAttribute("looklist", looklist); 58 return "forward:touserlist"; 59 } 60 61 //执行删除操作 62 @RequestMapping("/delete.action") 63 public String delete(int id){ 64 userService.deleteUser(id); 65 return "forward:userselect.action"; 66 } 67 }
UserController
3.userlist.jsp显示用户信息
1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 3 <html> 4 <head> 5 <title>用户列表</title> 6 </head> 7 <body> 8 <table align="center" border=2> 9 <tr bgcolor="#eeee99"> 10 <td>序号</td> 11 <td>Id</td> 12 <td>Name</td> 13 <td>Age</td> 14 <td>选择</td> 15 <td>操作</td> 16 </tr> 17 <c:set var="j" value="1"></c:set> 18 <c:forEach var="lists" items="${requestScope.looklist }"> 19 <tr> 20 <td>${j }</td> 21 <td>${lists.id }</td> 22 <td>${lists.name }</td> 23 <td>${lists.age}</td> 24 <td><a href="${pageContext.request.contextPath}/touserupdate?id=${lists.id}">修改</a></td> 25 <td><a href="${pageContext.request.contextPath}/delete.action?id=${lists.id}">删除</a></td> 26 </tr> 27 <c:set var="j" value="${j+1 }"></c:set> 28 </c:forEach> 29 </table> 30 <br> 31 <center> 32 <form action="${pageContext.request.contextPath }/userselect.action"> 33 <input type="submit" value="刷新"> 34 </form> 35 <form action="${pageContext.request.contextPath }/touseradd"> 36 <input type="submit" value="返回添加"> 37 </form> 38 </center> 39 </body> 40 </html>
userlist.jsp
4.springmvc-servlet.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 10 <!--<mvc:annotation-driven/>--> 11 <context:annotation-config/> 12 <!--配置数据源--> 13 <bean id="dataSource" 14 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 15 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 16 <property name="url" value="jdbc:mysql://localhost:3306/springjdbc"/> 17 <property name="username" value="root"/> 18 <property name="password" value="root"/> 19 </bean> 20 <!--配置JDBC模板--> 21 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 22 <property name="dataSource" ref="dataSource"/> 23 </bean> 24 <!--配置注入类--> 25 <bean id="userDao" class="com.wxy.dao.impl.UserDaoImpl"> 26 <property name="jdbcTemplate" ref="jdbcTemplate"/> 27 </bean> 28 <bean id="userService" class="com.wxy.dao.impl.ServiceImpl"> 29 <property name="userDao" ref="userDao"/> 30 </bean> 31 <bean id="userController" class="com.wxy.controller.UserController"> 32 <property name="userService" ref="userService"></property> 33 </bean> 34 <!-- 指定需要扫描的包 --> 35 <context:component-scan base-package="com.wxy.controller"></context:component-scan> 36 <context:component-scan base-package="com.wxy.dao.impl"></context:component-scan> 37 <!-- 定义视图解析器 --> 38 <bean id="viewResolver" 39 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 40 <!-- 设置前缀 --> 41 <property name="prefix" value="/jsp/"></property> 42 <!-- 设置后缀 --> 43 <property name="suffix" value=".jsp"></property> 44 </bean> 45 </beans>
springmvc-servlet.xml
配置方式XML和注解选用一种即可。
原文地址:https://www.cnblogs.com/wxywxy/p/8723585.html
时间: 2024-10-12 12:17:27