最近段时间正在学习Spring MVC和MyBatis的一些知识。自己也在网络上面找了一些例子来练习。但是都不是很完整。所以,今天,自己也抽空写了个完成的关于Spring MVC + Spring + MyBatis(简称 SSM)的一个CRUD的完整Web 演示例子。如果你也是刚好学习这几个框架的新手,或许我做的这个例子对你刚好有所帮助哦!
下面来说下这个演示的小例子。首先,我是使用MyEclipse工具做的这个例子,整合了Sping 3 、Spring MVC 3 、MyBatis框架,演示数据库采用MySQL数据库。例子中主要操作包括对数据的添加(C)、查找(R)、更新(U)、删除(D)。我在这里采用的数据库连接池是来自阿里巴巴的Druid,至于Druid的强大之处,我在这里就不做过多的解释了,有兴趣的朋友们可以去网上谷歌或者百度一下哦!好了下面我就贴上这次这个演示例子的关键代码:
BaseController:
package com.bky.controller; import java.util.List; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.bky.model.Add; import com.bky.service.BaseService; @Controller public class BaseController { private BaseService baseService; public BaseService getBaseService() { return baseService; } @Autowired public void setBaseService(BaseService baseService) { this.baseService = baseService; } @SuppressWarnings("finally") @RequestMapping("addInfo") public String add(Add add,HttpServletRequest request){ try { add.setId(UUID.randomUUID().toString()); System.out.println(add.getId() + ":::::" + add.getTname() + ":::::" + add.getTpwd()); String str = baseService.addInfo(add); System.out.println(str); request.setAttribute("InfoMessage", str); } catch (Exception e) { e.printStackTrace(); request.setAttribute("InfoMessage", "添加信息失败!具体异常信息:" + e.getMessage()); } finally { return "result"; } } @RequestMapping("getAll") public String getAddInfoAll(HttpServletRequest request){ try { List<Add> list = baseService.getAll(); System.out.println(list); request.setAttribute("addLists", list); return "listAll"; } catch (Exception e) { e.printStackTrace(); request.setAttribute("InfoMessage", "信息载入失败!具体异常信息:" + e.getMessage()); return "result"; } } @SuppressWarnings("finally") @RequestMapping("del") public String del(String tid,HttpServletRequest request){ try { String str = baseService.delete(tid); request.setAttribute("InfoMessage", str); } catch (Exception e) { e.printStackTrace(); request.setAttribute("InfoMessage", "删除信息失败!具体异常信息:" + e.getMessage()); } finally { return "result"; } } @RequestMapping("modify") public String modify(String tid,HttpServletRequest request){ try { Add add = baseService.findById(tid); request.setAttribute("add", add); return "modify"; } catch (Exception e) { e.printStackTrace(); request.setAttribute("InfoMessage", "信息载入失败!具体异常信息:" + e.getMessage()); return "result"; } } @SuppressWarnings("finally") @RequestMapping("update") public String update(Add add,HttpServletRequest request){ try { String str = baseService.update(add); request.setAttribute("InfoMessage", str); } catch (Exception e) { e.printStackTrace(); request.setAttribute("InfoMessage", "更新信息失败!具体异常信息:" + e.getMessage()); } finally { return "result"; } } }
Service的实现:
package com.bky.service.impl; import java.util.List; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.bky.dao.AddMapper; import com.bky.model.Add; import com.bky.service.BaseService; @Service("baseService") public class BaseServiceImpl implements BaseService { private AddMapper addMapper; public AddMapper getAddMapper() { return addMapper; } @Autowired public void setAddMapper(AddMapper addMapper) { this.addMapper = addMapper; } @Override public String addInfo(Add addInfo) { if (addMapper.insertSelective(addInfo) == 1) { return "添加成功"; } return "添加失败"; } @Override public List<Add> getAll() { return addMapper.getAll(); } @Override public String delete(String id) { if (addMapper.deleteByPrimaryKey(id) == 1) { return "删除成功"; } return "删除失败"; } @Override public Add findById(String id) { return addMapper.selectByPrimaryKey(id); } @Override public String update(Add addInfo) { if (addMapper.updateByPrimaryKeySelective(addInfo) == 1) { return "更新成功"; } return "更新失败"; } }
Mapper:
<?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.bky.dao.AddMapper" > <resultMap id="BaseResultMap" type="com.bky.model.Add" > <id column="id" property="id" jdbcType="VARCHAR" /> <result column="tname" property="tname" jdbcType="VARCHAR" /> <result column="tpwd" property="tpwd" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, tname, tpwd </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" > select <include refid="Base_Column_List" /> from tadd where id = #{id,jdbcType=VARCHAR} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.String" > delete from tadd where id = #{id,jdbcType=VARCHAR} </delete> <insert id="insert" parameterType="com.bky.model.Add" > insert into tadd (id, tname, tpwd ) values (#{id,jdbcType=VARCHAR}, #{tname,jdbcType=VARCHAR}, #{tpwd,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" parameterType="com.bky.model.Add" > insert into tadd <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="tname != null" > tname, </if> <if test="tpwd != null" > tpwd, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=VARCHAR}, </if> <if test="tname != null" > #{tname,jdbcType=VARCHAR}, </if> <if test="tpwd != null" > #{tpwd,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.bky.model.Add" > update tadd <set > <if test="tname != null" > tname = #{tname,jdbcType=VARCHAR}, </if> <if test="tpwd != null" > tpwd = #{tpwd,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=VARCHAR} </update> <update id="updateByPrimaryKey" parameterType="com.bky.model.Add" > update tadd set tname = #{tname,jdbcType=VARCHAR}, tpwd = #{tpwd,jdbcType=VARCHAR} where id = #{id,jdbcType=VARCHAR} </update> <select id="getAll" resultMap="BaseResultMap"> SELECT * FROM tadd </select> </mapper>
以上就是几个关键位置的代码,我全部贴出来了。至于配置文件什么的,由于时间原因没有贴出。如果大家要是感兴趣的话,可以下载我的这个演示项目包,里面的东西都齐全着,导入到MyEclipse上面直接部署到服务器上面就可以运行。数据库就是里面的那个.sql文件。建个库然后将数据导入就是。哦,对了。导完数据后,记得别忘了到config.properties里面去把数据库的连接信息换成你自己哦!
本次演示例子源码完整下载:http://pan.baidu.com/s/1jDA2q(百度网盘)
本文转自:http://www.baikeyang.com/code/96610.html