SpringMVC+Spring+MyBatis整合完整版Web實例(附數據)


  最近段時間正在學習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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM