创建数据表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层 @Repository public 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层 @Service public 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请求 @RequestMapping public 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"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>index</title> </head> <body> <form action="addUser" method="post"> <table align="center"> <tr> <td>用户名:</td> <td><input type="text" name="userName" /></td> </tr> <tr> <td>性别:</td> <td> <input type="radio" name="userSex" checked="checked" value="男" /> 男 <input type="radio" name="userSex" value="女" /> 女 </td> </tr> <tr> <td colspan="2"> <input type="submit" value="注册"> </td> </tr> </table> </form> </body> </html>
在 WEB-INF 下新建 views 文件夹,还记得上一篇在 mvc-context.xml 下配置
<!-- 代表返回视图进行解析:前缀为prefix 后缀为suffix --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean>
这里指定了视图的访问地址前缀。
在 views 下创建 success.jsp 和 fail.jsp
到这里所有都做完了,现在执行 clean install jetty:run
右键项目 run》》configure 双击 Maven Build 新建 configure
点击Apply ,点击run
OK,就到这里。。。
如果遇到什么问题可以在评论里提出谢谢,我看到了如果会的话,会尽力回答!