package com.sshblog.entity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "contacts")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer","handler","operations","roles","menus"})
public class Contacts {
private int id;
private String name;
private String address;
private String gender;
private Date dob;
private String email;
private Long mobile;
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Basic
@Column(name = "gender")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Basic
@Column(name = "dob")
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
@Basic
@Column(name = "email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Basic
@Column(name = "mobile")
public Long getMobile() {
return mobile;
}
public void setMobile(Long mobile) {
this.mobile = mobile;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Contacts contacts = (Contacts) o;
if (id != contacts.id) return false;
if (name != null ? !name.equals(contacts.name) : contacts.name != null) return false;
if (address != null ? !address.equals(contacts.address) : contacts.address != null) return false;
if (gender != null ? !gender.equals(contacts.gender) : contacts.gender != null) return false;
if (dob != null ? !dob.equals(contacts.dob) : contacts.dob != null) return false;
if (email != null ? !email.equals(contacts.email) : contacts.email != null) return false;
if (mobile != null ? !mobile.equals(contacts.mobile) : contacts.mobile != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (address != null ? address.hashCode() : 0);
result = 31 * result + (gender != null ? gender.hashCode() : 0);
result = 31 * result + (dob != null ? dob.hashCode() : 0);
result = 31 * result + (email != null ? email.hashCode() : 0);
result = 31 * result + (mobile != null ? mobile.hashCode() : 0);
return result;
}
}
Contacts.hbm.xml:
3.2、DAO层
ContactsDAOI.java:
package com.sshblog.dao;
import com.sshblog.entity.Contacts;
import java.util.List;
public interface ContactsDAOI {
public List findAllContacts();
public void saveContact(Contacts contacts);
public Contacts findById(int id);
public void deleteContact(int id);
public void updateContact(Contacts contacts);
public List findByContactName(String name);
}
ContactsImpl.java:
package com.sshblog.dao.impl;
import com.sshblog.dao.ContactsDAOI;
import com.sshblog.entity.Contacts;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
import java.util.List;
@Repository
@Transactional
public class ContactsDAOImpl implements ContactsDAOI {
@Autowired
private SessionFactory sessionFactory;
@Override
public List findAllContacts() {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Contacts.class);
return criteria.list();
}
@Override
public void saveContact(Contacts contacts) {
sessionFactory.getCurrentSession().save(contacts);
}
@Override
public Contacts findById(int id) {
Contacts contacts = (Contacts) sessionFactory.getCurrentSession().get(Contacts.class, id);
return contacts;
}
@Override
public void deleteContact(int id) {
Contacts contacts = findById(id);
sessionFactory.getCurrentSession().delete(contacts);
}
@Override
public void updateContact(Contacts contacts) {
sessionFactory.getCurrentSession().update(contacts);
}
@Override
public List findByContactName(String name) {
String hql = "from Contacts c where c.name like '%" + name + "%'";
return sessionFactory.getCurrentSession().createQuery(hql).list();
}
}
3.3、Service层
ContactsServiceI.java:
package com.sshblog.service;
import com.sshblog.entity.Contacts;
import java.util.List;
public interface ContactsServiceI {
public List findAllUsers();
public void saveContact(Contacts contacts);
public void deleteContact(int id);
public Contacts findById(int id);
public void updateContact(Contacts contacts);
public List findByUserName(String name);
}
ContactsServiceImpl.java:
package com.sshblog.service.impl;
import com.sshblog.dao.ContactsDAOI;
import com.sshblog.entity.Contacts;
import com.sshblog.service.ContactsServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;
@Service("contactsService")
@Transactional
public class ContactsServiceImpl implements ContactsServiceI {
@Autowired
private ContactsDAOI contactsDAOI;
public void setContactsDAOI(ContactsDAOI contactsDAOI) {
this.contactsDAOI = contactsDAOI;
}
@Override
public List findAllUsers() {
return this.contactsDAOI.findAllContacts();
}
@Override
public void saveContact(Contacts contacts) {
this.contactsDAOI.saveContact(contacts);
}
@Override
public void deleteContact(int id) {
this.contactsDAOI.deleteContact(id);
}
@Override
public Contacts findById(int id) {
return this.contactsDAOI.findById(id);
}
@Override
public void updateContact(Contacts contacts) {
this.contactsDAOI.updateContact(contacts);
}
@Override
public List findByUserName(String name) {
return this.contactsDAOI.findByContactName(name);
}
}
3.4、Controller层
SSHBlogController.java:
package com.sshblog.controller;
import com.sshblog.entity.Contacts;
import com.sshblog.service.ContactsServiceI;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/")
public class SSHBlogController {
@Autowired
private ContactsServiceI contactsServiceI;
public void setContactsServiceI(ContactsServiceI contactsServiceI) {
this.contactsServiceI = contactsServiceI;
}
@RequestMapping("index")
public ModelAndView indexPage(){
ModelAndView mav = new ModelAndView("index");
List contacts = this.contactsServiceI.findAllUsers();
mav.addObject("contacts",contacts);
return mav;
}
@RequestMapping(value = "save", method = RequestMethod.GET)
public ModelAndView newContactPage(){
ModelAndView mav = new ModelAndView("newContact");
return mav;
}
@RequestMapping(value = "save", method = RequestMethod.POST)
public String newContact(@RequestParam("dateOfb")String dob, Contacts contacts) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(dob);
contacts.setDob(date);
this.contactsServiceI.saveContact(contacts);
return "redirect:index.html";
}
@RequestMapping(value = "delete", method = RequestMethod.GET)
public String deleteContact(int id) {
this.contactsServiceI.deleteContact(id);
return "redirect:index.html";
}
@RequestMapping(value = "update", method = RequestMethod.GET)
public ModelAndView editContactPage(int id){
ModelAndView mav = new ModelAndView("editContact");
Contacts contacts = this.contactsServiceI.findById(id);
mav.addObject("editContact",contacts);
return mav;
}
@RequestMapping(value = "update", method = RequestMethod.POST)
public String editContact(@RequestParam("dateOfb")Date date, Contacts contacts){
contacts.setDob(date);
this.contactsServiceI.updateContact(contacts);
return "redirect:index.html";
}
@RequestMapping(value = "search", method = RequestMethod.POST)
public ModelAndView searchByContactName(String name){
ModelAndView mav = new ModelAndView("index");
List contacts = this.contactsServiceI.findByUserName(name);
mav.addObject("contacts",contacts);
return mav;
}
@RequestMapping(value = "findAllUsers", method = RequestMethod.GET)
@ResponseBody
public List findAllUsers(){
return this.contactsServiceI.findAllUsers();
}
}
视图控制层代码demo如下:
@Controller
@RequestMapping("/")
public class MessageController {
private final MessageRepository messageRepository;
@Autowired
public MessageController(Mes
作为一名
Linux SA,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者MAC绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名、ip信息、网关等配置。修改成特定的主机名在维护和管理方面也比较方便。如下脚本用途为:修改ip和主机名等相关信息,可以根据实际需求修改,举一反三!
#!/bin/sh
#auto Change ip netmask ga