spring-mybatis项目练习 - 通讯录系统,后端部分

Posted 枫林晚月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-mybatis项目练习 - 通讯录系统,后端部分相关的知识,希望对你有一定的参考价值。

  承接上文 http://www.cnblogs.com/fu-feng/p/7634944.html ,按src目录放——

 

  controller-->

    Bookcontroller.java

package com.hg.book.controller;

import java.io.FileNotFoundException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hg.book.dto.Bookdto;
import com.hg.book.serviceIfc.BookserviceIfc;
import com.hg.book.vo.Deletevo;
import com.hg.book.vo.Exportvo;
import com.hg.book.vo.Importvo;
import com.hg.book.vo.Listvo;

import uploader.uploader;

@Controller
@RequestMapping("/")
public class Bookcontroller {
    
    @Autowired
    private BookserviceIfc se;
    
    //用户名
    @RequestMapping("head")
    //不配置springmvc,可以这样处理编码
    //@RequestMapping(value="head", produces="text/html;charset=UTF-8")
    @ResponseBody
    private String head(HttpSession session){
        return session.getAttribute("username").toString();
    }

    //列表
    @RequestMapping("list")
    @ResponseBody
    private List<Bookdto> list(HttpSession session){
        int userid = Integer.valueOf(session.getAttribute("userid").toString());
        return se.getlist(userid);
    }

    //增加
    @RequestMapping("add")
    @ResponseBody
    private String add(HttpSession session, String name, String tele, String addr, String emai){
        int userid = Integer.valueOf(session.getAttribute("userid").toString());

        if(name == null || tele == null || addr == null || emai == null){
            return "-1";
        }
        
        Listvo vo = new Listvo();
        vo.setName(name);
        vo.setTelephone(tele);
        vo.setAddress(addr);
        vo.setEmail(emai);

        return ""+se.addhandle(vo, userid);
    }

    //删除
    @RequestMapping("delete")
    @ResponseBody
    private String del(HttpSession session, @RequestParam("memberid") String memberid){
        int userid = Integer.valueOf(session.getAttribute("userid").toString());
        if(memberid == null){
            return "-1";
        }
        
        Deletevo vo = new Deletevo();
        vo.setUserid(userid); 
        vo.setMemberid(Integer.valueOf(memberid));
        
        return ""+se.deletehandle(vo);
    }

    //修改
    @RequestMapping("edit")
    @ResponseBody
    private String edit(HttpSession session, 
            @RequestParam("id")String memberid, String name, String tele, String addr, String emai){
        int userid = Integer.valueOf(session.getAttribute("userid").toString());

        if(memberid == null || name == null || tele == null || addr == null || emai == null){
            return "-1";
        }

        Listvo vo = new Listvo();
        vo.setId(Integer.valueOf(memberid));
        vo.setName(name);
        vo.setTelephone(tele);
        vo.setAddress(addr);
        vo.setEmail(emai);
        
        return ""+se.edithandle(vo, userid);
    }
    
    //查询
    @RequestMapping("find")
    @ResponseBody
    private List<Bookdto> find(HttpSession session, String name, String tele, String addr, String emai){
        int userid = Integer.valueOf(session.getAttribute("userid").toString());
        
        if(name==null) name = "";
        if(tele==null) tele = "";
        if(addr==null) addr = "";
        if(emai==null) emai = "";
        
        Listvo vo = new Listvo();
        vo.setName(name);
        vo.setTelephone(tele);
        vo.setAddress(addr);
        vo.setEmail(emai);
        
        return se.findhandle(vo, userid);
    }

    //导入
    @RequestMapping("import")
    @ResponseBody
    private String importgo(HttpServletRequest req, HttpSession session){
        //上传的保存路径
        String upload_path = "books/bookimport/";
        //临时文件保存路径
        String setrepository_path = "books/UploadTemp/";
        
        //允许上传的文件格式列表
        final String[] allowedExt = new String[] { "xml" };
        
        uploader up = new uploader();
        up.setrepository_path(setrepository_path);
        up.setType(allowedExt);
        up.setPath(upload_path);

        if(up.upload(req) == 1){
            String realwebbase = session.getServletContext().getRealPath("/");
            Importvo vo = new Importvo();
            vo.setSavepath(realwebbase + upload_path + up.getfilename());
            Integer id = Integer.valueOf(session.getAttribute("userid").toString());
            vo.setUserid(id);
            boolean result = false;
            try {
                result = se.importhandle(vo);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            if(result){
                return "1";
            }else{
                return "0";
            }
        }else{
            return "文件上传失败:"+up.getexc();
        }
    }

    //导出
    @RequestMapping("export")
    @ResponseBody
    private String export(HttpSession session, String memberids){
        Integer id = Integer.valueOf(session.getAttribute("userid").toString());
        String name = session.getAttribute("username").toString();
        String realwebbase = session.getServletContext().getRealPath("/");

        if(memberids == null){
            return "0";
        }
        
        Exportvo vo = new Exportvo();
        vo.setUserid(id);
        vo.setUsername(name);
        vo.setMemberids(memberids);
        vo.setRealwebbase(realwebbase);
        
        boolean r = se.exporthandle(vo);
        if(r){
            return id+"";
        }else{
            return "-1";
        }
    }
}
bookcontroller

    Logincontroller.java

package com.hg.book.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hg.book.serviceIfc.BookserviceIfc;
import com.hg.book.vo.Loginvo;

@Controller
@RequestMapping("/")
public class Logincontroller {
    
    @Autowired
    private BookserviceIfc se;
    
    //状态检查
    @RequestMapping("check")
    @ResponseBody
    private String check(HttpSession session) {
        if(session.getAttribute("userid") == null){
            return "1";
        }else{
            return "2";
        }
    }
    
    //登录
    @RequestMapping("login")
    @ResponseBody
    private String login(HttpSession session, @RequestParam("user_name") String name, @RequestParam("user_pw") String pass){
        if(name == null || pass == null){
            return "-2";
        }
        
        Loginvo vo = new Loginvo();
        vo.setUsername(name);
        vo.setUserpass(pass);
        
        int r = se.loginhandle(vo);
        if(r == 0){
            return "0";
        }else if(r == -1){
            return "-1";
        }else{
            //登陆成功,跳转到列表
            session.setAttribute("userid", r);
            session.setAttribute("username", name);
            return r+"";
        }
    }
    
    //退出登录
    @RequestMapping("leave")
    @ResponseBody
    private String leave(HttpSession session){
        session.setAttribute("userid", null);
        session.setAttribute("username", null);
        
        return "";
    }
}
Logincontroller

 

    Registercontroller.java

package com.hg.book.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hg.book.serviceIfc.BookserviceIfc;
import com.hg.book.vo.Loginvo;

@Controller
@RequestMapping("/register")
public class Registercontroller {
    @Autowired
    private BookserviceIfc se;

    @RequestMapping("")
    @ResponseBody
    private String register(@RequestParam("username") String name, @RequestParam("apassword") String pass){
        if(name == null || pass == null){
            return "-2";
        }
        
        Loginvo vo = new Loginvo();
        vo.setUsername(name);
        vo.setUserpass(pass);
        
        return se.registerhandle(vo)+"";
    }
}
Registercontroller

 

 

  dao-->

    Bookdao.java

package com.hg.book.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import com.hg.book.dto.Bookdto;
import com.hg.book.dto.Memberdto;
import com.hg.book.dto.Updatedto;
import com.hg.book.dto.Writedto;

@Repository
public interface Bookdao {
    
    //获取用户信息
    public Memberdto logindao(String username);

    //插入用户注册数据
    public void registerdao(Memberdto dto);

    //创建通讯录表
    public void createbook(Integer userid);

    //展示全部联系人
    public List<Bookdto> listdao(Integer userid);

    //条件查找联系人列表
    public List<Bookdto> findbycondition(@Param("dto") Bookdto dto,@Param("userid") Integer userid);

    //导出联系人列表
    public List<Bookdto> exportlist(@Param("memberids")String string, @Param("userid")Integer integer);

    //导入联系人,用javabean传递参数
    public void importlist(Writedto wd);

    //添加联系人
    public int adddao(Updatedto dto);

    //删除联系人
    public int deletedao(Updatedto dto);

    //修改联系人信息
    public int editdao(Updatedto dto);
    
}
Bookdao

    Bookmapper.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.hg.book.dao.Bookdao">

    <!-- 通讯录表手动映射 -->
    <resultMap type="book" id="bookmap">
        <id property="id" column="memberid"/>
        <!-- 下方元素可以自动映射,可省略 -->
        <result property="name" column="name"/>
        <result property="telephone" column="telephone"/>
        <result property="address" column="address"/>
        <result property="email" column="email"/>
    </resultMap>
    
    <!-- 获取用户信息 -->
    <select id="logindao" parameterType="String" resultType="member">
        select * from bookuser where username = #{username}
    </select>
    
    <!-- 插入用户注册数据 -->
    <insert id="registerdao">
        insert into bookuser (username, userpass) 
        values (#{username}, #{userpass})
    </insert>
    
    <!-- 创建通讯录表 -->
    <update id="createbook">
        CREATE TABLE #{userid}book (memberid INT(10) NOT NULL AUTO_INCREMENT, 
                NAME VARCHAR(30) NOT NULL, telephone VARCHAR(30) NOT NULL, 
                address VARCHAR(255), email VARCHAR(255), PRIMARY KEY(memberid))
    </update>
    
    <!-- 展示全部联系人 -->
    <select id="listdao" parameterType="int" resultMap="bookmap">
        select * from #{userid}book 
    </select>
    
    <!-- 条件查找联系人列表 -->
    <select id="findbycondition" resultMap="bookmap">
        select * from #{userid}book 
        <!-- prefix:前缀   prefixOverrides:删除前缀  -->
        <trim prefix="where" prefixOverrides="and | or">
            <if test="dto.name != \'\'">
                and name like concat(\'%\', #{dto.name}, \'%\')
            </if>
            <if test="dto.telephone != \'\'">
                and telephone like concat(\'%\', #{dto.telephone}, \'%\')
            </if>
            <if test="dto.address != \'\'">
                and address like concat(\'%\', #{dto.address}, \'%\')
            </if>
            <if test="dto.email != \'\'">
                and email like concat(\'%\', #{dto.email}, \'%\')
            </if>
        </trim>
    </select>
    
    <!-- 导出联系人列表 -->
    <select id="exportlist" resultMap="bookmap">
        select * from #{userid}book where memberid in (${memberids})
    </select>
    
    <!-- 导入联系人 -->
    <insert id="importlist" parameterType="com.hg.book.dto.Writedto">
        insert into #{userid}book (name,telephone,address,email) 
        values (#{bookdto.name}, #{bookdto.telephone}, #{bookdto.address}, #{bookdto.email})
    </insert>
    
    <!-- 添加联系人 -->
    <insert id="adddao" parameterType="update">
        insert into #{userid}book (name,telephone,address,email) 
        values (#{name}, #{telephone}, #{address}, #{email})
    </insert>
    
    <!-- 删除联系人 -->
    <delete id="deletedao" parameterType="update">
        delete from #{userid}book where memberid = #{memberid}
    </delete>
    
    <!-- 修改联系人信息 -->
    <update id="editdao" parameterType="update">
        update #{userid}book set 
        name = #{name}, 
        telephone = #{telephone}, 
        address = #{address}, 
        email = #{email} 
        where memberid = #{memberid}
    </update>
</mapper>
Bookmapper

 

 

  dto-->

    Bookdto.java

package com.hg.book.dto;

public class Bookdto {
    
    private Integer id;
    private String name;
    private String telephone;
    private String address;
    private String email;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

}
Bookdto

    Memberdto.java

package com.hg.book.dto;

public class Memberdto {
    private Integer userid;
    private String username;
    private String userpass;
    public Integer getUserid() {
        return userid;
    }
    public void setUserid(Integer userid) {
        this.userid = userid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUserpass() {
        return userpass;
    }
    public void setUserpass(String userpass) {
        this.userpass = userpass;
    }
    
}
Memberdto

    Updatedto.java

package com.hg.book.dto;

public class Updatedto {

    private Integer userid;
    private Integer memberid;
    private String name;
    private String telephone;
    private String address;
    private String email;
    public Integer getUserid() {
        return userid;
    }
    public void setUserid(Integer userid) {
        this.userid = userid;
    }
    public Integer getMemberid() {
        return memberid;
    }
    public void setMemberid(Integer memberid) {
        this.memberid = memberid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}
Updatedto

    Writedto.java

package com.hg.book.dto;

public class Writedto {

    private Integer userid;
    private Bookdto bookdto;
    public Integer getUserid() {
        return userid;
    }
    public void setUserid(Integer userid) {
        this.userid = userid;
    }
    public Bookdto getBookdto() {
        return bookdto;
    }
    public void setBookdto(Bookdto bookdto) {
        this.bookdto = bookdto;
    }
}
Writedto

 

 

 

  service-->

    Bookservice.java

package com.hg.book.service;

import java.io.*;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.dom4j.Element;
import com.hg.book.dao.Bookdao;
import com.hg.book.dto.*;
import com.hg.book.serviceIfc.BookserviceIfc;
import com.hg.book.vo.*;
import com.thoughtworks.xstream.XStream;

@Service
public class Bookservice implements BookserviceIfc{
    
    @Autowired
    private Bookdao dao;
    
    //登录
    @Transactio

以上是关于spring-mybatis项目练习 - 通讯录系统,后端部分的主要内容,如果未能解决你的问题,请参考以下文章

spring-mybatis项目练习 - 通讯录系统,ssm配置文件部分

spring-mybatis整合项目 异常处理

spring-mybatis

spring-mybatis整合的dao单元测试

[Go] Golang练习项目-web客服系统即时通讯websocket项目go-fly

spring-mybatis整合异常