创建数据表user_info
create table 'user_info'(
user_id int(11) NOT NULL AUTO_INCREMENT,
user_name varchar(20) NOT NULL,
user_sex varchar(20) NOT NULL,
PRIMARY KEY (`user_id`)
)
写一个实体类UserInfo.java
package hover.chen.entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="user_info")public class UserInfo { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="user_id") private int userId; @Column(name="user_name") private String userName; @Column(name="user_sex") private String userSex; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserSex() { return userSex; } public void setUserSex(String userSex) { this.userSex = userSex; } }
写一个 IUserDAO.java 接口
package hover.chen.dao;import hover.chen.entity.UserInfo;public interface IUserDAO { public int addUser(UserInfo user);}
UserDAOImpl 实现 IUserDAO.java
package hover.chen.dao;import hover.chen.entity.UserInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.stereotype.Repository;//通过注解告诉spring这是DAO层@Repositorypublic class UserDAOImpl implements IUserDAO{ //告诉spring容器自动将hibernateTemplate注入到DAO层 @Autowired private HibernateTemplate hibernateTemplate; public int addUser(UserInfo user){ String result = this.hibernateTemplate.save(user).toString(); if(result != null){ return Integer.parseInt(result); } return 0; } }
写一个 IUserService 接口
package hover.chen.service;import hover.chen.entity.UserInfo;public interface IUserService { public boolean addUser(UserInfo user);}
UserServiceImpl 实现 IUserService
package hover.chen.service;import hover.chen.dao.IUserDAO;import hover.chen.entity.UserInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;//告诉spring这是service层@Servicepublic class UserServiceImpl implements IUserService{ //告诉spring容器自动将userDAO注入到Service层 @Autowired private IUserDAO userDAO; public boolean addUser(UserInfo user){ return this.userDAO.addUser(user) > 0 ? true : false; }}
写一个Controller
package hover.chen.controller;import hover.chen.entity.UserInfo;import hover.chen.service.IUserService;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;//告诉spring这是controller@Controller//此类处理的是以/user路径的url请求@RequestMappingpublic class UserController { @Autowired private IUserService userService; //处代表的是整个方法处理addUser的post请求 @RequestMapping(value="/addUser",method=RequestMethod.POST) //处代表的是请求参数,其中value必须与页面元素的name值匹配,required=true则代表该参数必须要有 //return 是指跳转至相应的路径,在这里是指跳转至views/success.jsp或者views/fail.jsp public String addUserAction(@RequestParam(value="userName",required=true)String userName, @RequestParam(value="userSex",required=true)String userSex){ UserInfo user = new UserInfo(); user.setUserName(userName); user.setUserSex(userSex); return this.userService.addUser(user) == true ? "success" : "fail"; } }
接下来是使视图层
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>index
在 WEB-INF 下新建 views 文件夹,还记得上一篇在 mvc-context.xml 下配置
这里指定了视图的访问地址前缀。
在 views 下创建 success.jsp 和 fail.jsp
到这里所有都做完了,现在执行 clean install jetty:run
右键项目 run》》configure 双击 Maven Build 新建 configure
点击Apply ,点击run
OK,就到这里。。。
如果遇到什么问题可以在评论里提出谢谢,我看到了如果会的话,会尽力回答!