Forum_SSM
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:mvc-config.xml
1
spring
*.do
CharacterEncodingFilter
filter.CharacterEncodingFilter
characterEncoding
UTF-8
enabled
true
CharacterEncodingFilter
/*
component-scan标签默认情况下自动扫描指定路径下的包(含所有子包),将带有@Component、@Repository、@Service、@Controller标签的类自动注册到spring容器。对标记了@Required、@Autowired等注解的类进行对应的操作使注解生效。
/WEB-INF/jsp/
.jsp
package model;
public class Person {
private Integer id;
private Date datecreated;
private Boolean deleted;
private Integer version;
private String account;
private String birthday;
private Date datelastactived;
private String email;
private String ipcreated;
private String iplastactived;
private String name;
private String password;
private String sex;
//省略setter和getter方法
}
package mapper;
public interface PersonMapper {
Person getPerson(Person person);
Person selectByAccount(String account);
List selectBoardByPersonId(int id);
List selectAll();
int deleteByBoardId(Integer id);
int insertBoardAdministrator(@Param("boardId") int boardId, @Param("personId") int personId);
/*以上为自己实现的方法,保留了下面可能用到的方法*/
int deleteByPrimaryKey(Integer id);
int insert(Person record);
int insertSelective(Person record);
Person selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Person record);
int updateByPrimaryKey(Person record);
}
id, dateCreated, deleted, version, account, birthday, dateLastActived, email, ipCreated,
ipLastActived, name, password, sex
id, dateCreated, deleted, version, description, name, replyCount, threadCount, category_id,
last_reply_id, last_thread_id
delete from board_administrator
where board_id = #{boardId,jdbcType=INTEGER}
insert into board_administrator
values (#{boardId}, #{personId})
delete from person
where id = #{id,jdbcType=INTEGER}
insert into person (id, dateCreated, deleted,
version, account, birthday,
dateLastActived, email, ipCreated,
ipLastActived, name, password,
sex)
values (#{id,jdbcType=INTEGER}, #{datecreated,jdbcType=TIMESTAMP}, #{deleted,jdbcType=BIT},
#{version,jdbcType=INTEGER}, #{account,jdbcType=VARCHAR}, #{birthday,jdbcType=VARCHAR},
#{datelastactived,jdbcType=TIMESTAMP}, #{email,jdbcType=VARCHAR}, #{ipcreated,jdbcType=VARCHAR},
#{iplastactived,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR})
insert into person
id,
dateCreated,
deleted,
version,
account,
birthday,
dateLastActived,
email,
ipCreated,
ipLastActived,
name,
password,
sex,
#{id,jdbcType=INTEGER},
#{datecreated,jdbcType=TIMESTAMP},
#{deleted,jdbcType=BIT},
#{version,jdbcType=INTEGER},
#{account,jdbcType=VARCHAR},
#{birthday,jdbcType=VARCHAR},
#{datelastactived,jdbcType=TIMESTAMP},
#{email,jdbcType=VARCHAR},
#{ipcreated,jdbcType=VARCHAR},
#{iplastactived,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
update person
dateCreated = #{datecreated,jdbcType=TIMESTAMP},
deleted = #{deleted,jdbcType=BIT},
version = #{version,jdbcType=INTEGER},
account = #{account,jdbcType=VARCHAR},
birthday = #{birthday,jdbcType=VARCHAR},
dateLastActived = #{datelastactived,jdbcType=TIMESTAMP},
email = #{email,jdbcType=VARCHAR},
ipCreated = #{ipcreated,jdbcType=VARCHAR},
ipLastActived = #{iplastactived,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
where id = #{id,jdbcType=INTEGER}
update person
set dateCreated = #{datecreated,jdbcType=TIMESTAMP},
deleted = #{deleted,jdbcType=BIT},
version = #{version,jdbcType=INTEGER},
account = #{account,jdbcType=VARCHAR},
birthday = #{birthday,jdbcType=VARCHAR},
dateLastActived = #{datelastactived,jdbcType=TIMESTAMP},
email = #{email,jdbcType=VARCHAR},
ipCreated = #{ipcreated,jdbcType=VARCHAR},
ipLastActived = #{iplastactived,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
package DAO;
public interface PersonDAO {
public void save(Person person);
public int insert(Person person);
public Person selectById(int id);
public List selectBoardByPersonId(int id);
public List selectAll();
public Person findPersonByAccount(String account);
public Person getPerson(Person person);
public int deleteByBoardId(int id);
public int insertBoardAdministrator(int boardId, int personId);
}
package DAOImpl;
@Repository
public class PersonDAOImpl implements PersonDAO{
@Autowired
private PersonMapper personMapper;
@Override
public Person findPersonByAccount(String account) {
return personMapper.selectByAccount(account);
}
@Override
public Person getPerson(Person person) {
return personMapper.getPerson(person);
}
@Override
public void save(Person person) {
personMapper.updateByPrimaryKeySelective(person);
}
@Override
public int insert(Person person) {
return personMapper.insertSelective(person);
}
@Override
public Person selectById(int id) {
return personMapper.selectByPrimaryKey(id);
}
@Override
public List selectBoardByPersonId(int id) {
return personMapper.selectBoardByPersonId(id);
}
@Override
public List selectAll() {
return personMapper.selectAll();
}
@Override
public int deleteByBoardId(int id) {
return personMapper.deleteByBoardId(id);
}
@Override
public int insertBoardAdministrator(int boardId, int personId) {
return personMapper.insertBoardAdministrator(boardId, personId);
}
}
package Service;
public interface PersonService {
public void save(Person person);
public int insert(Person person);
public Person selectById(int id);
public List selectAll();
public List selectBoardByPersonId(int id);
public Person findPersonByAccount(String account);
public Person getPerson(Person person);
public int deleteByBoardId(int id);
public int insertBoardAdministrator(int boardId, int personId);
}
package ServiceImpl;
@Service
public class PersonServiceImpl implements PersonService{
@Autowired
private PersonDAO personDAO;
@Override
public Person findPersonByAccount(String account) {
return personDAO.findPersonByAccount(account);
}
@Override
public Person getPerson(Person person) {
Person newper=new Person();
newper.setAccount(person.getAccount());
newper.setPassword(MD5Util.calc(person.getPassword()));
return personDAO.getPerson(newper);
}
@Override
public void save(Person person) {
personDAO.save(person);
}
@Override
public int insert(Person person) {
if(findPersonByAccount(person.getAccount())!=null)
throw new RuntimeException("帐号 " + person.getAccount() + " 已经存在。");
person.setPassword(MD5Util.calc(person.getPassword()));
return personDAO.insert(person);
}
@Override
public Person selectById(int id) {
return personDAO.selectById(id);
}
@Override
public List selectBoardByPersonId(int id) {
return personDAO.selectBoardByPersonId(id);
}
@Override
public List selectAll() {
return personDAO.selectAll();
}
@Override
public int deleteByBoardId(int id) {
return personDAO.deleteByBoardId(id);
}
@Override
public int insertBoardAdministrator(int boardId, int personId) {
return personDAO.insertBoardAdministrator(boardId, personId);
}
}
package Controller;
@Controller
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping("person_initAdd.do")
public ModelAndView initAdd(HttpServletRequest request){
request.setAttribute("title", "用户注册");
request.setAttribute("person", new Person());
return new ModelAndView("person/addPerson");
}
@RequestMapping("person_add.do")
public ModelAndView add(@ModelAttribute Person person, HttpServletRequest request, HttpServletResponse response){
request.setAttribute("title", "用户注册");
person.setIpcreated(request.getRemoteAddr());
person.setIplastactived(request.getRemoteAddr());
person.setDatecreated(new Date());
person.setDatelastactived(new Date());
person.setDeleted(false);
if (person.getAccount() == null|| person.getAccount().trim().length() == 0) {
request.setAttribute("message", "请输入帐号");
return initAdd(request);
}
if (person.getPassword() == null|| person.getPassword().trim().length() == 0|| !person.getPassword().equals(request.getParameter("password1"))) {
request.setAttribute("message", "密码不一致");
return initAdd(request);
}
try {
personService.insert(person);//保存到数据库,此时没有id
PersonUtil.setPersonInf(request, response, personService.findPersonByAccount(person.getAccount()));
request.setAttribute("message", "注册成功");
return new ModelAndView("person/success");
} catch (Exception e) {
request.setAttribute("message", "注册失败,原因:" + e.getMessage());
return initAdd(request);
}
}
@RequestMapping("person_initLogin.do")
public ModelAndView initLogin(HttpServletRequest request){
request.setAttribute("person", new Person());
request.setAttribute("title", "用户登录");
return new ModelAndView("person/login");
}
@RequestMapping("person_login.do")
public ModelAndView login(@ModelAttribute Person person,HttpServletRequest request, HttpServletResponse response) throws Exception{
request.setAttribute("title", "用户登录");
Person person1=personService.getPerson(person);
if (person1 == null)
throw new AccountException("用户名密码错误");
PersonUtil.setPersonInf(request, response, person1);
person1.setIplastactived(request.getRemoteAddr());
person1.setDatelastactived(new Date());
personService.save(person1);
request.setAttribute("message", "欢迎回来");
return new ModelAndView("person/success");
}
@RequestMapping("person_logout.do")
public ModelAndView logout(HttpServletRequest request){
request.setAttribute("title", "用户注销");
request.getSession(true).setAttribute(PersonUtil.PERSON_INFO, null);
request.setAttribute("person", new Person());
return new ModelAndView("person/login");
}
@RequestMapping("person_view.do")
public ModelAndView view(HttpServletRequest request){
request.setAttribute("title", "查看用户资料");
int id=Integer.parseInt(request.getParameter("id"));
Person person=personService.selectById(id);
request.setAttribute("person", person);
List boardList=personService.selectBoardByPersonId(id);
request.setAttribute("boardList", boardList);
return new ModelAndView("person/viewPerson");
}
}
package model;
public class Category {
private Integer id;
private Date datecreated;
private Boolean deleted;
private Integer version;
private String name;
private List boardList; //省略setter、getter方法
}
package mapper;
public interface CategoryMapper {
List getList();
Category selectByName(String name);
List getAll();
/*以上为自己实现的方法,保留了下面可能用到的方法*/
int deleteByPrimaryKey(Integer id);
int insert(Category record);
int insertSelective(Category record);
Category selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Category record);
int updateByPrimaryKey(Category record);
}
id, dateCreated, deleted, version, name
delete from category
where id = #{id,jdbcType=INTEGER}
insert into category (id, dateCreated, deleted,
version, name)
values (#{id,jdbcType=INTEGER}, #{datecreated,jdbcType=TIMESTAMP}, #{deleted,jdbcType=BIT},
#{version,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
insert into category
id,
dateCreated,
deleted,
version,
name,
#{id,jdbcType=INTEGER},
#{datecreated,jdbcType=TIMESTAMP},
#{deleted,jdbcType=BIT},
#{version,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
update category
dateCreated = #{datecreated,jdbcType=TIMESTAMP},
deleted = #{deleted,jdbcType=BIT},
version = #{version,jdbcType=INTEGER},
name = #{name,jdbcType=VARCHAR},
where id = #{id,jdbcType=INTEGER}
update category
set dateCreated = #{datecreated,jdbcType=TIMESTAMP},
deleted = #{deleted,jdbcType=BIT},
version = #{version,jdbcType=INTEGER},
name = #{name,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
package DAO;
public interface CategoryDAO {
public List getList();
public int create(Category category);
public Category selectByName(String name);
public List getAll();
}
package DAOImpl;
@Repository
public class CategoryDAOImpl implements CategoryDAO{
@Autowired
private CategoryMapper categoryMapper;
@Override
public List getList() {
return categoryMapper.getList();
}
@Override
public int create(Category category) {
return categoryMapper.insertSelective(category);
}
@Override
public Category selectByName(String name) {
return categoryMapper.selectByName(name);
}
@Override
public List getAll() {
return categoryMapper.getAll();
}
}
package Service;
public interface CategoryService {
public List getList();
public int create(Category category);
public List getAll();
}
package ServiceImpl;
@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
private CategoryDAO categoryDAO;
@Override
public List getList() {
return categoryDAO.getList();
}
@Override
public int create(Category category) {
if(categoryDAO.selectByName(category.getName())!=null)
throw new RuntimeException("类别 " + category.getName() + " 已经存在。");
return categoryDAO.create(category);
}
@Override
public List getAll() {
return categoryDAO.getAll();
}
}
package Controller;
@Controller
public class CategoryController {
@Autowired
private CategoryService categoryService;
@RequestMapping("category_list.do")
public ModelAndView list(HttpServletRequest request){
request.setAttribute("title", "轻量级 Java EE 论坛程序");
List categoryList = categoryService.getList();
request.setAttribute("categoryList", categoryList);
return new ModelAndView("category/listCategory");
}
@RequestMapping("category_initAdd.do")
public ModelAndView initAdd(HttpServletRequest request){
request.setAttribute("title", "添加类别");
request.setAttribute("category", new Category());
return new ModelAndView("category/addCategory");
}
@RequestMapping("category_add.do")
public ModelAndView add(@ModelAttribute Category category, HttpServletRequest request){
request.setAttribute("title", "添加类别");
category.setDatecreated(new Date());
category.setDeleted(false);
categoryService.create(category);
return new ModelAndView("category/success");
}
}
create database forum character set utf8 ;
use forum;
CREATE TABLE if not exists person (
id int(11) NOT NULL AUTO_INCREMENT,
dateCreated datetime DEFAULT NULL,
deleted bit(1) NOT NULL,
version int(11) DEFAULT NULL,
account varchar(255) DEFAULT NULL,
birthday varchar(255) DEFAULT NULL,
dateLastActived datetime DEFAULT NULL,
email varchar(255) DEFAULT NULL,
ipCreated varchar(255) DEFAULT NULL,
ipLastActived varchar(255) DEFAULT NULL,
name varchar(255) DEFAULT NULL,
password varchar(255) DEFAULT NULL,
sex varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
);
CREATE TABLE category (
id int(11) NOT NULL AUTO_INCREMENT,
dateCreated datetime DEFAULT NULL,
deleted bit(1) NOT NULL,
version int(11) DEFAULT NULL,
name varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
);
CREATE TABLE thread (
id int(11) NOT NULL AUTO_INCREMENT,
dateCreated datetime DEFAULT NULL,
deleted bit(1) NOT NULL,
version int(11) DEFAULT NULL,
content longtext,
dateLastReplied datetime DEFAULT NULL,
hit int(11) NOT NULL,
ipCreated varchar(255) DEFAULT NULL,
readonly bit(1) NOT NULL,
replyCount int(11) NOT NULL,
title varchar(255) DEFAULT NULL,
topped bit(1) NOT NULL,
author_id int(11) DEFAULT NULL,
author_last_replied_id int(11) DEFAULT NULL,
board_id int(11) DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (author_last_replied_id) REFERENCES person (id),
FOREIGN KEY (author_id) REFERENCES person (id)
);
alter table thread add FOREIGN KEY (board_id) REFERENCES board (id);
CREATE TABLE reply (
id int(11) NOT NULL AUTO_INCREMENT,
dateCreated datetime DEFAULT NULL,
deleted bit(1) NOT NULL,
version int(11) DEFAULT NULL,
content varchar(255) DEFAULT NULL,
floor int(11) NOT NULL,
ipCreated varchar(255) DEFAULT NULL,
title varchar(255) DEFAULT NULL,
author_id int(11) DEFAULT NULL,
thread_id int(11) DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (author_id) REFERENCES person (id),
FOREIGN KEY (thread_id) REFERENCES thread (id)
);
CREATE TABLE board(
id int(11) NOT NULL AUTO_INCREMENT,
dateCreated datetime DEFAULT NULL,
deleted bit(1) NOT NULL,
version int(11) DEFAULT NULL,
description varchar(255) DEFAULT NULL,
name varchar(255) DEFAULT NULL,
replyCount int(11) NOT NULL,
threadCount int(11) NOT NULL,
category_id int(11) DEFAULT NULL,
last_reply_id int(11) DEFAULT NULL,
last_thread_id int(11) DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (last_thread_id) REFERENCES thread (id),
FOREIGN KEY (last_reply_id) REFERENCES reply (id),
FOREIGN KEY (category_id) REFERENCES category (id)
);
CREATE TABLE board_administrator (
board_id int(11) NOT NULL,
person_id int(11) NOT NULL,
PRIMARY KEY (board_id,person_id),
FOREIGN KEY (person_id) REFERENCES person (id),
FOREIGN KEY (board_id) REFERENCES board (id)
);