- 浏览: 115749 次
- 性别:
- 来自: 南京
最新评论
-
zhchyun2008:
鱼崽崽 写道大家可以看看这个,里面有真正的实例:http:// ...
springmvc+MyBatis简单CRUD -
鱼崽崽:
大家可以看看这个,里面有真正的实例:http://43.249 ...
springmvc+MyBatis简单CRUD -
w-mamba:
适合初学者学习,蛮好…
ajax+jquery+json分页demo -
aa00aa00:
我笑了............................ ...
springmvc+MyBatis简单CRUD -
u010100704:
大哥。能不能加个项目结构图。再加个运行结果图,你这样写我都不知 ...
springmvc+MyBatis简单CRUD
实体类:Student.java
BaseEntity.java(可扩展,加入分页基本参数)
==============================
IStudentDao.java
StudentDaoImpl.java
==============
IStudentService.java
StudentServiceImpl.java
===================
StudentInfoMapper.xml
================
StudentController.java
===============================配置文件=============================================
SqlMapConfig.xml
applicationContext.xml
spring核心配置文件:servlet.xml
jdbc.properties
web.xml
============================jsp=========================================
allStudent.jsp
package demo.entity; public class Student extends BaseEntity { private int id; private String name; private String sex; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
BaseEntity.java(可扩展,加入分页基本参数)
package demo.entity; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class BaseEntity { public String toString() { //得到类中的成员变量 Field[] fields = this.getClass().getDeclaredFields(); StringBuffer strBuf = new StringBuffer(); for(int i=0; i<fields.length; i++) { //成员变量名称 String fieldName = fields[i].getName(); //拼接出方法名:getXxx() String methodName = "get" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1); try { //得到方法 Method method = this.getClass().getMethod(methodName, new Class[]{}); //调用方法 Object value = method.invoke(this, new Object[]{}); strBuf.append(fieldName + ":"); strBuf.append(value + " "); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return strBuf.toString(); } }
==============================
IStudentDao.java
package demo.dao; import java.util.List; import demo.entity.Student; public interface IStudentDao { public boolean insert(Student stu); public boolean delete(int id); public boolean update(Student stu); public List<Student> findAll(); public Student findById(int id); }
StudentDaoImpl.java
package demo.dao.impl; import java.util.List; import javax.annotation.Resource; import org.mybatis.spring.SqlSessionTemplate; import demo.dao.IStudentDao; import demo.entity.Student; @SuppressWarnings("unchecked") public class StudentDaoImpl implements IStudentDao { /*sql 语句*/ private static final String INSERT = "insert"; private static final String UPDATE = "update"; private static final String DELETE = "delete"; private static final String SELECTALL = "selectAll"; private static final String SELECTBYID = "selectById"; private SqlSessionTemplate sqlSession; @Resource public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } public boolean delete(int id) { // TODO Auto-generated method stub String sql = this.getStatementId(Student.class, DELETE); sqlSession.delete(sql, id); return true; } public List<Student> findAll() { // TODO Auto-generated method stub String sql = this.getStatementId(Student.class, SELECTALL); List<Student> list = (List<Student>)sqlSession.selectList(sql); return list; } public Student findById(int id) { // TODO Auto-generated method stub String sql = this.getStatementId(Student.class, SELECTBYID); Student stu = (Student)sqlSession.selectOne(sql, id); return stu; } @SuppressWarnings("static-access") public boolean insert(Student stu) { // TODO Auto-generated method stub String sql = this.getStatementId(Student.class, INSERT); this.sqlSession.insert(sql, stu); return true; } public boolean update(Student stu) { // TODO Auto-generated method stub String sql = this.getStatementId(Student.class, UPDATE); this.sqlSession.update(sql, stu); return true; } /** * 映射sqlid */ private String getStatementId(Class entityClass, String suffix) { String sqlStr = entityClass.getName() + "." + suffix; System.out.println("getStatementId:" + sqlStr); return sqlStr; } }
==============
IStudentService.java
package demo.service; import java.util.List; import demo.entity.Student; public interface IStudentService { public boolean insert(Student stu); public boolean delete(int id); public boolean update(Student stu); public List<Student> findAll(); public Student findById(int id); }
StudentServiceImpl.java
package demo.service.impl; import java.util.List; import javax.annotation.Resource; import demo.dao.IStudentDao; import demo.entity.Student; import demo.service.IStudentService; public class StudentServiceImpl implements IStudentService { private IStudentDao stuDao; @Resource public void setStuDao(IStudentDao stuDao) { this.stuDao = stuDao; } public boolean delete(int id) { // TODO Auto-generated method stub return stuDao.delete(id); } public List<Student> findAll() { // TODO Auto-generated method stub return stuDao.findAll(); } public Student findById(int id) { // TODO Auto-generated method stub return stuDao.findById(id); } public boolean insert(Student stu) { // TODO Auto-generated method stub stuDao.insert(stu); return true; } public boolean update(Student stu) { // TODO Auto-generated method stub return stuDao.update(stu); } }
===================
StudentInfoMapper.xml
<?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="demo.entity.Student"> <!-- 起别名 <alias> <typeAlias alias="" type="" /> </alias> --> <!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql --> <insert id="insert" parameterType="demo.entity.Student" useGeneratedKeys="true" keyProperty="id"> <![CDATA[ INSERT INTO student ( name, sex, address ) VALUES ( #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR} ) ]]> <!-- oracle: order="BEFORE" SELECT sequenceName.nextval AS ID FROM DUAL DB2: order="BEFORE"" values nextval for sequenceName <selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="userId"> SELECT sequenceName.nextval AS ID FROM DUAL </selectKey> --> </insert> <update id="update" parameterType="demo.entity.Student"> <![CDATA[ UPDATE student SET name = #{name,jdbcType=VARCHAR}, sex = #{sex,jdbcType=VARCHAR}, address = #{address,jdbcType=VARCHAR} WHERE id = #{id,jdbcType=INTEGER} ]]> </update> <delete id="delete" parameterType="demo.entity.Student"> delete from student where id = #{id} </delete> <select id="selectById" resultType="demo.entity.Student" parameterType="Integer"> select * from student where id = #{id} </select> <select id="selectAll" resultType="demo.entity.Student"> select * from student; </select> <delete id="deleteByPrimaryKey" parameterType="Integer"> delete from student where id = #{id} </delete> </mapper>
================
StudentController.java
package demo.controller; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import demo.entity.Student; import demo.service.IStudentService; @Controller @RequestMapping("/student") public class StudentController { private static final Log log = LogFactory.getLog(StudentController.class); private IStudentService stuService; @Resource public void setStuService(IStudentService stuService) { this.stuService = stuService; } /** * 增 */ @RequestMapping(value = "/insert", method = RequestMethod.POST) public String insert(HttpServletRequest request, HttpServletResponse response, Student stu) { stuService.insert(stu); log.info(stu); return "redirect:/student/allStudent.do"; } /** * 删 */ @RequestMapping("/delete/{id}") public String delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") int id) { stuService.delete(id); return "redirect:/student/allStudent.do"; } /** * 得到所有 */ @RequestMapping("/allStudent") public ModelAndView allStudent(HttpServletRequest request, HttpServletResponse response) { ModelAndView modelAndView = new ModelAndView(); List<Student> stuList = stuService.findAll(); modelAndView.addObject("stuList", stuList); modelAndView.setViewName("allStudent"); return modelAndView; } /** * 修改 */ @RequestMapping("/pre4Update") public ModelAndView update(HttpServletRequest request, HttpServletResponse response, Student stu) { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("stu", stu); modelAndView.setViewName("updateStudent"); return modelAndView; } @RequestMapping("/updateStudent") public String updateStudent(HttpServletRequest request, HttpServletResponse response, Student stu) { this.stuService.update(stu); return "redirect:/student/allStudent.do"; } /** * 用于跳转 */ @RequestMapping("redir/{url}") public String redir(HttpServletRequest request, HttpServletResponse response, @PathVariable("url") String url) { return url; } }
===============================配置文件=============================================
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> <configuration> <mappers> <mapper resource="demo/mapper/StudentInfoMapper.xml" /> </mappers> </configuration>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="stuDao" class="demo.dao.impl.StudentDaoImpl"></bean> <bean id="stuService" class="demo.service.impl.StudentServiceImpl"> <property name="stuDao" ref="stuDao" /> </bean> </beans>
spring核心配置文件:servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="demo.controller"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 数据库连接配置文件路径及读取方式 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- 数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置mybatis固定的写法 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="/WEB-INF/classes/ibatis/SqlMapConfig.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean> </beans>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost/demo?characterEncoding=gb2312 jdbc.username=root jdbc.password=root
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>demo2</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring/*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>demo2</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
============================jsp=========================================
allStudent.jsp
<%@ page language="java" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="e"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Student 列表</title> <script type="text/javascript"> function update(event) { alert(event.srcElement.parentNode.previousSibling.childNodes[0].nodeValue); document.myForm.elements[0].value = event.srcElement.parentNode.previousSibling.previousSibling.previousSibling.previousSibling.childNodes[0].nodeValue; document.myForm.elements[1].value = event.srcElement.parentNode.previousSibling.previousSibling.previousSibling.childNodes[0].nodeValue; document.myForm.elements[2].value = event.srcElement.parentNode.previousSibling.previousSibling.childNodes[0].nodeValue; document.myForm.elements[3].value = event.srcElement.parentNode.previousSibling.childNodes[0].nodeValue; document.myForm.submit(); } </script> </head> <body> <table width="50%" border="1"> <tr> <td> id </td> <td> 姓名 </td> <td> 性别 </td> <td> 地址 </td> <td> 操作 </td> </tr> <e:forEach items="${requestScope.stuList}" var="stu"> <tr> <td> ${stu.id} </td> <td> ${stu.name} </td> <td> ${stu.sex} </td> <td> ${stu.address} </td> <td> <a onclick="javascript:update(event)" href="javascript:void(0)">修改</a>|| <a href="delete/${stu.id}.do"> 删除</a> </td> </tr> </e:forEach> </table> <a href="redir/addStudent.do">添加</a> <form name="myForm" action="pre4Update.do" method="post"> <input type="hidden" name="id" /> <input type="hidden" name="name" /> <input type="hidden" name="sex" /> <input type="hidden" name="address" /> </form> </body> </html>
评论
5 楼
zhchyun2008
2016-11-10
4 楼
鱼崽崽
2015-07-11
大家可以看看这个,里面有真正的实例:
http://43.249.81.29:8080/index.html
http://43.249.81.29:8080/index.html
3 楼
aa00aa00
2014-09-18
我笑了..................................
2 楼
u010100704
2014-08-09
大哥。能不能加个项目结构图。再加个运行结果图,你这样写我都不知道。哎,咱写就写好行不行
1 楼
仗剑者
2014-07-02
求完整项目代码啊亲
发表评论
-
spring execution 表达式
2012-08-29 15:18 986Spring AOP 用户可能会经常使用 executio ... -
TOMCAT热部署
2012-08-23 11:05 969tomcat如何设置成热启动,即修改源代码时不需要重启tomc ... -
spring jar详解
2012-08-21 15:12 715spring jar 包详解 spri ... -
The prefix "aop" for element "aop:config" is not bound.
2012-08-21 15:02 2878错误提示:The prefix "aop" ... -
ajax+json自动提示Demo
2012-08-16 10:30 1030下载地址:http://download.csdn.net/d ... -
MyBatis入门
2012-08-03 17:35 1292MyBatis学习 之 一、MyBatis简介与 ... -
SpringMVC之用注解控制器
2012-05-23 17:05 993在传统的Spring MVC开 ... -
SpringMVC的CRUD
2012-08-02 18:11 5151实体类:Student.java package demo ... -
iText导出PDF经典实现
2012-07-23 15:07 1226转自:http://blog.csdn.net/lenotan ... -
POI导出EXCEL经典实现
2012-07-23 15:01 1111转自:http://blog.csdn.net/lenotan ...
相关推荐
在本系统中,Mybatis主要负责数据库的CRUD(Create、Read、Update、Delete)操作。通过XML或注解配置,Mybatis能够动态生成SQL语句,执行数据库查询,并将结果自动映射为Java对象。这种方式降低了SQL与Java代码的...
Mybatis则可以配合Mybatis-Plus扩展,进一步简化CRUD操作。通过深入学习和理解这两个框架,开发者不仅可以提高开发效率,还能更好地应对复杂的企业级需求。 这个压缩包资源对于初学者和有经验的开发者都是宝贵的,...
本资源提供了一个简单的实现案例,它基于SpringMVC、MyBatis和EasyUI这三个流行的技术框架,旨在帮助开发者理解如何将这些组件集成并实现一个具有基本留言功能的系统。下面我们将详细探讨这个案例中的关键知识点。 ...
在本项目中,MyBatis与SpringMVC结合,使得数据库操作更为便捷,通过Mapper接口即可完成对数据库的CRUD操作。 MySQL是一个广泛使用的开源关系型数据库管理系统,以其高效、稳定和易于管理而著称。在这个项目中,...
这是网易云课堂里面一个简单的增删改查的项目,感觉挺适合初学者的推荐给大家,在看这个项目时学习到了很多,前端使用了bootstrap,后端使用spring+springmvc+mybatis的rest风格设计
【SpringMVC+MyBatis内测机试题】 在IT行业中,SpringMVC和MyBatis是两个非常重要的Java Web开发框架。SpringMVC作为Spring框架的一部分,主要用于处理Web应用程序的请求-响应流程,而MyBatis则是一个轻量级的持久...
在本项目中,"springMVC+mybatis实现登录和增删改查"是一个典型的Web应用程序,它结合了SpringMVC、MyBatis两大主流框架,以及Bootstrap前端库,来构建一个用户友好的数据管理平台。以下是这个项目涉及到的主要知识...
标题 "spring+springMVC+mybatis+easyui 增删查改以及分页源码" 描述了一个使用SSM(Spring、SpringMVC、MyBatis)框架与EasyUI前端库联合实现的Web应用项目,特别是关注于CRUD(创建、读取、更新、删除)操作和无...
【标题】"spring+springmvc+mybatis搭建的酒店管理系统附带mysql数据库"涉及的核心技术是Java企业级开发中的Spring框架、SpringMVC模块以及MyBatis持久层框架,同时结合了MySQL数据库来存储和管理数据。这个项目是...
SSM(Spring+SpringMVC+Mybatis)是Java开发中常用的Web应用开发框架,它将Spring的IOC(Inversion of Control,控制反转)和AOP(Aspect Oriented Programming,面向切面编程)功能、SpringMVC作为视图控制器以及...
Mybatis 实现了对象与数据库表记录的映射,通过 Mapper 接口,可以方便地进行 CRUD(创建、读取、更新、删除)操作。 4. **SqlSession 和 Executor** SqlSession 是 Mybatis 的工作接口,用于执行 SQL 查询和事务...
在这个系统中,Mybatis可能用于与数据库进行交互,执行CRUD操作,如添加、修改、查询和删除学生信息。 【学生管理系统】 学生管理系统是一个典型的业务管理系统,用于存储和管理学生的个人信息、成绩、课程等数据。...
在IT行业中,构建Java Web应用程序是一项常见的任务,而"Maven构建Spring4+SpringMVC+Mybatis3"的组合是这种任务的一种流行实现方式。这个项目利用了三个强大的开源框架:Spring Framework作为核心容器,Spring MVC...
在这个"简单springmvc+mybatis例子"中,我们将探讨如何将这两个框架集成,实现一个基本的数据CRUD(创建、读取、更新和删除)功能。 首先,SpringMVC的核心是DispatcherServlet,它作为前端控制器接收请求并分发到...
在DEMO中,MyBatis用于数据库操作,通过XML或注解定义SQL语句,与SpringMVC配合实现数据的CRUD操作。 4. EasyUI: EasyUI是一个基于jQuery的UI组件库,主要用于构建桌面级的Web应用界面。它提供了一系列的UI组件,...
在SpringMVC+MyBatis的项目中,MyBatis作为数据访问层,处理数据库的CRUD(创建、读取、更新、删除)操作。通过Mapper接口和XML配置文件或注解,可以方便地定义SQL查询。 Spring框架是整个项目的基石,提供了依赖...
视频教程将逐步指导如何配置和使用这些组件,从创建Maven项目开始,到导入相应的依赖,然后设置Spring的IoC容器,接着配置SpringMVC,实现请求分发,再引入Mybatis并配置数据源,最后进行实际的CRUD操作。...
SSM框架,即Spring、SpringMVC和Mybatis的整合,是Java开发中常见的Web应用程序框架。这个开源小项目提供了一种便捷的方式,帮助初学者和有经验的开发者理解和实践这三个核心组件的集成。 首先,Spring框架是Java...
本项目"springmvc+mybatis+mysql整合实现列表展示、新增、更新、删除功能"旨在提供一个基础的Maven项目模板,帮助开发者快速搭建具备CRUD(Create、Read、Update、Delete)操作的Web应用。 首先,让我们深入理解...
SpringMVC负责处理Web请求,Spring负责依赖注入和AOP,Mybatis负责数据库的CRUD操作。 二、必要的JAR包 在SpringMVC+Spring+Mybatis框架中,需要以下JAR包: * SpringMVC:spring-webmvc-3.2.4.RELEASE.jar * ...