通讯录方面
contact.java
package com.ssm.domain;
public class Contact {
private Integer t_id;
private Integer t_age;
private String t_name;
private String t_phone;
private String t_school;
private String t_address;
private String t_gender;
private Integer user_id;
private User user;
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getT_age() {
return t_age;
}
public void setT_age(Integer t_age) {
this.t_age = t_age;
}
public String getT_name() {
return t_name;
}
public void setT_name(String t_name) {
this.t_name = t_name;
}
public String getT_phone() {
return t_phone;
}
public void setT_phone(String t_phone) {
this.t_phone = t_phone;
}
public String getT_school() {
return t_school;
}
public void setT_school(String t_school) {
this.t_school = t_school;
}
public String getT_address() {
return t_address;
}
public void setT_address(String t_address) {
this.t_address = t_address;
}
public String getT_gender() {
return t_gender;
}
public void setT_gender(String t_gender) {
this.t_gender = t_gender;
}
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
contactMapper.java
package com.ssm.mapper;
import java.util.List;
import java.util.Map;
import com.ssm.domain.Contact;
public interface ContactMapper {
/**
* 保存通讯录
*/
public void save(Contact contact);
/**
* 根据主键更新通讯录
*/
public void update(Contact contact);
/**
* 根据主键删除通讯录
*/
public void deleteById(int t_id);
/**
* 根据用户ID查询全部
*/
public List<Contact> getAll(int id);
/**
* 根据ID条件查询单个记录,并不是查询全部,懒得改方法名
*/
public Contact getAllById(int t_id);
/**
* 动态查询
public List<Contact> dynamicSelect(int user_id,String t_name,String t_school,String t_gender);
*/
public List<Contact> dynamicSelect(Map<String,Object> map );
}
contactMapper.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.ssm.mapper.ContactMapper">
<resultMap id="contactResultMap" type="com.ssm.domain.Contact">
<id column="t_id" property="t_id" />
<result column="t_age" property="t_age" />
<result column="t_name" property="t_name" />
<result column="t_phone" property="t_phone" />
<result column="t_school" property="t_school" />
<result column="t_address" property="t_address" />
<result column="t_gender" property="t_gender" />
<result column="user_id" property="user_id" />
<association property="user" resultMap="com.ssm.mapper.UserMapper.userMap"/>
</resultMap>
<insert id="save" parameterType="com.ssm.domain.Contact">
insert into contact(t_age,t_name,t_phone,t_school,t_address,t_gender,user_id)
values(#{t_age},#{t_name},#{t_phone},#{t_school},#{t_address},#{t_gender},#{user_id})
</insert>
<update id="update" parameterType="com.ssm.domain.Contact">
update contact set
t_age=#{t_age},t_name=#{t_name},t_phone=#{t_phone},t_school=#{t_school},
t_address=#{t_address},t_gender=#{t_gender},user_id=#{user_id}
where
t_id=#{t_id}
</update>
<delete id="deleteById" parameterType="int" >
delete from contact where t_id=#{t_id}
</delete>
<select id="getAll" parameterType="int" resultMap="contactResultMap">
select * from contact where user_id=#{id}
</select>
<select id="getAllById" parameterType="int" resultMap="contactResultMap">
select * from contact where t_id=#{t_id}
</select>
<select id="dynamicSelect" parameterType="map" resultMap="contactResultMap">
select * from contact <!-- where user_id=#{user_id} -->
<where>
<if test="t_name!=''">
and t_name=#{t_name}
</if>
<if test="t_school!=''">
and t_school=#{t_school}
</if>
<if test="t_gender!=''">
and t_gender=#{t_gender}
</if>
</where>
</select>
</mapper>
package com.ssm.dao;
import java.util.List;
import java.util.Map;
import com.ssm.domain.Contact;
public interface ContactDao {
public void save(Contact contact);
public void update(Contact contact);
public void deleteById(int t_id);
public List<Contact> getAll(int id);
public Contact getAllById(int t_id);
public List<Contact> dynamicSelect(Map<String,Object> map);
}
contactDaoImpl.java
package com.ssm.dao;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import com.ssm.domain.Contact;
import com.ssm.mapper.ContactMapper;
@Repository("contactDao")
public class ContactDaoImpl implements ContactDao {
@Resource(name="contactMapper")
private ContactMapper contactMapper;
public ContactMapper getContactMapper() {
return contactMapper;
}
public void setContactMapper(ContactMapper contactMapper) {
this.contactMapper = contactMapper;
}
public void save(Contact contact) {
contactMapper.save(contact);
}
public void update(Contact contact) {
contactMapper.update(contact);
}
public void deleteById(int t_id) {
contactMapper.deleteById(t_id);
}
public List<Contact> getAll(int id) {
return contactMapper.getAll(id);
}
public Contact getAllById(int t_id) {
return contactMapper.getAllById(t_id);
}
public List<Contact> dynamicSelect(Map<String,Object> map) {
return contactMapper.dynamicSelect(map);
}
}
contactService.java
package com.ssm.service;
import java.util.List;
import java.util.Map;
import com.ssm.domain.Contact;
public interface ContactService {
public void save(Contact contact);
public void update(Contact contact);
public void deleteById(int t_id);
public List<Contact> getAll(int id);
public Contact getAllById(int t_id);
public List<Contact> dynamicSelect(Map<String,Object> map);
}
contactServiceImpl.java
package com.ssm.service;
import java.util.List;
import java.util.Map;
import com.ssm.domain.Contact;
public interface ContactService {
public void save(Contact contact);
public void update(Contact contact);
public void deleteById(int t_id);
public List<Contact> getAll(int id);
public Contact getAllById(int t_id);
public List<Contact> dynamicSelect(Map<String,Object> map);
}
contactController
package com.ssm.controller;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.ssm.domain.Contact;
import com.ssm.domain.User;
import com.ssm.service.ContactService;
@Controller
@Scope(value="prototype")
@RequestMapping("/contact")
public class ContactController {
@Resource
private ContactService contactService;
/*@RequestMapping(value="/getAll")
public String getAll(Model model) throws Exception{
List<Contact> contacts=contactService.getAll();
model.addAttribute("contacts", contacts);
return "getAll";
}*/
//查询用户的全部记录
@RequestMapping(value="/getAll")
public String getAll(HttpSession session,int id)throws Exception{
List<Contact> contacts =contactService.getAll(id);
session.setAttribute("contacts", contacts);
return "getAll";
}
//根据ID删除单条记录
@RequestMapping(value="/deleteById")
public ModelAndView deleteById(ModelAndView model,Integer t_id,int id){
contactService.deleteById(t_id);
model.setView(new RedirectView("/newssm/contact/getAll.action?id="+id));
return model;
}
//修改记录信息
@RequestMapping("/toUpdate")
public String toUpdate(int t_id,Model model){
Contact contact=contactService.getAllById(t_id);
model.addAttribute("contact", contact);
return "contact_update";
}
@RequestMapping(value="/doUpdate")
public ModelAndView updateById(ModelAndView model,Contact contact){
contactService.update(contact);
int lalala=contact.getUser_id();
model.setView(new RedirectView("/newssm/contact/getAll.action?id="+lalala));
return model;
}
/*@RequestMapping(value="/doUpdate")
public ModelAndView updateById(ModelAndView model,Contact contact){
contact.setT_age(11);
contact.setT_name("李锦记");
contactService.update(contact);
//System.out.println(contact.getT_address());
int lalala=contact.getUser_id();
model.setView(new RedirectView("/newssm/contact/getAll.action?id="+lalala));
return model;
}*/
//增加单条通讯录记录
@RequestMapping("/toSave")
public String toSave(int user_id,HttpSession session){
session.setAttribute("user_id", user_id);
return "contact_save";
}
@RequestMapping(value="/save")
public ModelAndView save(ModelAndView model,Contact contact){
contactService.save(contact);
int lalala=contact.getUser_id();
model.setView(new RedirectView("/newssm/contact/getAll.action?id="+lalala));
return model;
}
//动态查询
@RequestMapping("/dynamicSelect")
public ModelAndView dynamicSelect(Contact contact,HttpSession session,ModelAndView model)throws Exception{
Map<String,Object> map = new LinkedHashMap<String, Object>();
/*map.put("user_id", contact.getUser_id());*/
map.put("t_name",contact.getT_name());
map.put("t_school",contact.getT_school());
map.put("t_gender", contact.getT_gender());
List <Contact> dyContact=contactService.dynamicSelect(map);
session.setAttribute("dyContact", dyContact);
//odel.setViewName("dy_contact");
if(dyContact!=null){
model.setViewName("dy_contact");
}
else
{
model.addObject("message", "没查到相关信息");
int lalala=contact.getUser_id();
model.setView(new RedirectView("/newssm/contact/getAll.action?id="+lalala));
}
return model;
}
}