标签:tty log4j integer oct valueof user 技术分享 nod row
承接上文 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"; } } }
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 ""; } }
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)+""; } }
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); }
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>
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; } }
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; } }
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; } }
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; } }
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; //登录 @Transactional(propagation = Propagation.SUPPORTS) public int loginhandle(Loginvo vo){ String username = vo.getUsername(); Memberdto dto = dao.logindao(username); if(dto == null){ //用户名错误 return 0; }else{ if(dto.getUserpass().equals(vo.getUserpass())){ //验证完成,返回id return dto.getUserid(); }else{ //密码错误 return -1; } } } //注册 @Transactional(propagation = Propagation.REQUIRED) public int registerhandle(Loginvo vo) { String username = vo.getUsername(); if(dao.logindao(username)==null){ Memberdto dto = new Memberdto(); dto.setUsername(username); dto.setUserpass(vo.getUserpass()); try{ dao.registerdao(dto); //插入用户注册数据后,创建通讯录表 Memberdto dto1 = dao.logindao(username); dao.createbook(dto1.getUserid()); //注册完成 return 1; }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } }else{ //用户名已存在 return 0; } } //展示联系人列表 @Transactional(propagation = Propagation.SUPPORTS) public List<Bookdto> getlist(int userid){ return dao.listdao(userid); } //条件查找列表 @Transactional(propagation = Propagation.SUPPORTS) public List<Bookdto> findhandle(Listvo vo, int userid) { Bookdto dto = new Bookdto(); dto.setName(vo.getName()); dto.setTelephone(vo.getTelephone()); dto.setAddress(vo.getAddress()); dto.setEmail(vo.getEmail()); return dao.findbycondition(dto, userid); } //导出 @Transactional(propagation = Propagation.SUPPORTS) public boolean exporthandle(Exportvo vo){ List<Bookdto> lb = dao.exportlist(vo.getMemberids(), vo.getUserid()); //实例化 XStream类 XStream xstream = new XStream(); //创建xml标签别名 xstream.alias("member", Bookdto.class); xstream.alias("addressbook", List.class); //忽略属性id,不创建相应标签 xstream.omitField(Bookdto.class, "id"); //将name转换为xml属性 xstream.useAttributeFor(Bookdto.class, "name"); //转换 String xml = xstream.toXML(lb); //写出到服务器 File f = null; OutputStreamWriter out = null; BufferedWriter bw = null; try { String part = vo.getRealwebbase() + "books/bookexport/addressbookbyid" + vo.getUserid()+".xml"; f = new File(part); out = new OutputStreamWriter(new FileOutputStream(f),"UTF-8"); bw = new BufferedWriter(out); String head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; bw.write(head); bw.newLine(); bw.write(xml); //完成 return true; } catch (IOException e) { e.printStackTrace(); //失败 return false; } finally { try{ if(bw != null){ bw.close(); } if(out != null){ out.close(); } }catch(IOException e){ e.printStackTrace(); } } } //导入 @Transactional(propagation = Propagation.REQUIRED) public boolean importhandle(Importvo vo){ SAXReader reader = new SAXReader(); InputStream in = null; Document doc = null; boolean r = false; try { //in = this.getClass().getResourceAsStream(vo.getSavepath()); in = new FileInputStream(vo.getSavepath()); doc = reader.read(in); // 获取根节点 Element root = doc.getRootElement(); // 获得根元素下的子节点 List<Element> childNodes = root.elements(); for(Element member:childNodes){ Bookdto dto = new Bookdto(); String name = member.attributeValue("name"); String telephone = member.elementText("telephone"); String address = member.elementText("address"); String email = member.elementText("email"); //检查值是否为空 dto.setName((name == null)?"":name); dto.setTelephone((telephone == null)?"":telephone); dto.setAddress((address == null)?"":address); dto.setEmail((email == null)?"":email); Writedto wd = new Writedto(); wd.setUserid(vo.getUserid()); wd.setBookdto(dto); dao.importlist(wd); } r = true; } catch (FileNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (Exception e) { //System.out.println("不是标准通讯录文件!!"); System.out.println(e); throw new RuntimeException(e); } finally { File f = new File(vo.getSavepath()); if(f.exists()){ f.delete(); } } return r; } //添加联系人 @Transactional(propagation = Propagation.REQUIRED) public int addhandle(Listvo vo, int userid) { Updatedto dto = new Updatedto(); dto.setUserid(userid); dto.setName(vo.getName()); dto.setTelephone(vo.getTelephone()); dto.setAddress(vo.getAddress()); dto.setEmail(vo.getEmail()); return dao.adddao(dto); } //删除联系人 @Transactional(propagation = Propagation.REQUIRED) public int deletehandle(Deletevo vo) { Updatedto dto = new Updatedto(); dto.setUserid(vo.getUserid()); dto.setMemberid(vo.getMemberid()); return dao.deletedao(dto); } //修改联系人信息 @Transactional(propagation = Propagation.REQUIRED) public int edithandle(Listvo vo, int id) { Updatedto dto = new Updatedto(); dto.setUserid(id); dto.setMemberid(vo.getId()); dto.setName(vo.getName()); dto.setTelephone(vo.getTelephone()); dto.setAddress(vo.getAddress()); dto.setEmail(vo.getEmail()); return dao.editdao(dto); } }
serviceIfc-->
BookserviceIfc.java
package com.hg.book.serviceIfc; import java.io.FileNotFoundException; import java.util.List; import com.hg.book.dto.Bookdto; 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 com.hg.book.vo.Loginvo; public interface BookserviceIfc { public int loginhandle(Loginvo vo); public int registerhandle(Loginvo vo); public List<Bookdto> getlist(int userid); public List<Bookdto> findhandle(Listvo vo, int userid); public boolean exporthandle(Exportvo vo); public boolean importhandle(Importvo vo) throws FileNotFoundException; public int addhandle(Listvo vo, int userid); public int deletehandle(Deletevo vo); public int edithandle(Listvo vo, int id); }
vo-->
Deletevo.java
package com.hg.book.vo; public class Deletevo { private Integer userid; private Integer memberid; 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; } }
Exportvo.java
package com.hg.book.vo; public class Exportvo { private Integer userid; private String memberids; private String username; private String realwebbase; 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 getRealwebbase() { return realwebbase; } public void setRealwebbase(String realwebbase) { this.realwebbase = realwebbase; } public String getMemberids() { return memberids; } public void setMemberids(String memberids) { this.memberids = memberids; } }
Importvo.java
package com.hg.book.vo; public class Importvo { private String savepath = null; private Integer userid = null; public String getSavepath() { return savepath; } public void setSavepath(String savepath) { this.savepath = savepath; } public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } }
Listvo.java
package com.hg.book.vo; public class Listvo { private Integer id; private String name; private String telephone; private String address; private String email; 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; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }
Loginvo.java
package com.hg.book.vo; public class Loginvo { private String username; private String userpass; 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; } }
log4j的配置文件-->
log4j.properties
#DEBUG,INFO,WARN,ERROR,FATAL #log4j.rootLogger=WARN,stdout,DailyFile #log4j.rootLogger=WARN,DailyFile log4j.rootLogger=DEBUG,stdout #log4j.logger.org.mybatis=DEBUG log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n #log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) %d{yyyy-MM-dd HH:mm:ss,SSS}%n Log from:[%c]%n Log msg:[%m]%n log4j.appender.DailyFile=org.apache.log4j.DailyRollingFileAppender log4j.appender.DailyFile.DatePattern=‘.‘yyyy-MM-dd log4j.appender.DailyFile.File=run.log log4j.appender.DailyFile.Append=true log4j.appender.DailyFile.Threshold=FATAL log4j.appender.DailyFile.layout=org.apache.log4j.PatternLayout log4j.appender.DailyFile.layout.ConversionPattern=%n%5p [%t] (%F:%L) %d{yyyy-MM-dd HH:mm:ss,SSS}%n Log from:[%c]%n Log msg:[%m]%n
spring-mybatis项目练习 - 通讯录系统(二),后端部分
标签:tty log4j integer oct valueof user 技术分享 nod row
原文地址:http://www.cnblogs.com/fu-feng/p/7637328.html