近期公司要开发新的项目,要用struts2+mybatis+spring框架,所以学习了下,来自己的博客发表下,希望能给大家带来帮助!
主要实现用户的增删改查操作
1、导入相应的jar包
2、配置web.xml主要是配置struts2和spring
web.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://.pdsu.edu.domain.User" id="userResult"> <result property="id" column="id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <result property="username" column="username" /> <result property="password" column="password" /> </resultMap> <select id="userLogin" parameterType="user" resultMap="userResult"> select * from user where username=#{username} and password=#{password} </select> <select id="selectAllUser" resultMap="userResult"> select * from user </select> <select id="findUserById" parameterType="int" resultMap="userResult"> select * from user where id=#{id} </select> <insert id="insertUser" parameterType="user"> <![CDATA[ insert into user(username,password) values(#{username},#{password}) ]]> </insert> <update id="updateUser" parameterType="user"> update user set username=#{username},password=#{password} where id=#{id} </update> <delete id="deleteUser" parameterType="int"> delete from user where id=#{id} </delete> </mapper>
7、User实体的写法
public class User implements Serializable { private static final long serialVersionUID = -4415990281535582814L; private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", password=" + password + ", username=" + username + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } }
8、UserDao的写法
public interface UserDao { public abstract void insertUser(User user); public abstract void updateUser(User user); public abstract void deleteUser(Integer userId); public abstract User findUserByid(Integer userId); public abstract List<User> findAll(); public abstract User userLogin(User user); }
9、UserDao的实现
@Repository public class UserDaoImpl implements UserDao { private final String INSERT_USER = "insertUser"; private final String UPDATE_USER = "updateUser"; private final String DELETE_USER = "deleteUser"; private final String FIND_USER_BYID = "findUserById"; private final String SELECT_ALL_USER = "selectAllUser"; private final String USER_LOGIN = "userLogin"; @Autowired private SqlSessionTemplate sqlSessionTemplate; public void insertUser(User user) { sqlSessionTemplate.insert(INSERT_USER, user); } public void updateUser(User user) { sqlSessionTemplate.update(UPDATE_USER, user); } public void deleteUser(Integer userId) { sqlSessionTemplate.delete(DELETE_USER, userId); } public User findUserByid(Integer userId) { return sqlSessionTemplate.selectOne(FIND_USER_BYID, userId); } public List<User> findAll() { return sqlSessionTemplate.selectList(SELECT_ALL_USER); } public User userLogin(User user) { return sqlSessionTemplate.selectOne(USER_LOGIN, user); } }
10、UserService接口
public interface UserService { // 添加用户 public abstract void addUser(User user); public abstract void updateUser(User user); public abstract void deleteUser(Integer userId); public abstract User findUserById(Integer userId); public abstract List<User> findAllUser(); public abstract User login(User user); }
11、UserService接口的实现
@Service @Transactional public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; // 添加用户 public void addUser(User user) { userDao.insertUser(user); } // 更新用户 public void updateUser(User user) { userDao.updateUser(user); } public void deleteUser(Integer userId) { userDao.deleteUser(userId); } public User findUserById(Integer userId) { return userDao.findUserByid(userId); } public List<User> findAllUser() { return userDao.findAll(); } public User login(User user) { return userDao.userLogin(user); } }
12、配置Struts2
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8"/> <!-- 指定默认编码集 ,作用于HttpServletRequest的setCharacterEncoding()和freemarker,vilocity的输出 --> <constant name="struts.configuration.xmlreload" value="true"/> <!-- 当struts配置文件修改时是否自动加载 --> <constant name="struts.devMode" value="true"/> <!-- 开发模式下打印详细的错误信息 --> <constant name="struts.ui.theme" value="xhtml"/> <package name="user" namespace="/user" extends="struts-default"> <action name="user_*" class="userAction" method="{1}"> <result name="success" type="redirectAction">user_queryAllUser.action</result> <result name="input">/index.jsp</result> <result name="userList">/userList.jsp</result> <result name="addUser">/userAdd.jsp</result> <result name="updateUser">/userUpdate.jsp</result> </action> </package> </struts>
13、UserAction具体实现
@Controller @Scope("prototype") public class UserAction extends ActionSupport { @Autowired private UserService userService; private User user; private List<User> userList; public String execute() throws Exception { return null; } public String login() { if (user != null) { User user2 = userService.login(user); if (user2 != null) { return SUCCESS; } } this.addFieldError("user.username", "用户名或密码错误!"); return INPUT; } public String addUI() { return "addUser"; } public String updateUI() { user = userService.findUserById(user.getId()); return "updateUser"; } public String add() { userService.addUser(user); return SUCCESS; } public String delete() { userService.deleteUser(user.getId()); return SUCCESS; } public String update() { userService.updateUser(user); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String queryAllUser() { userList = userService.findAllUser(); return "userList"; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } }
14、登录页面的实现
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>用户登录</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <s:head/> </head> <body> <center> <h1>用户登录</h1> <s:a action="user_addUI" namespace="/user">添加新用户</s:a> <s:form action="user_login" namespace="/user" method="post"> <s:textfield label="用户名" name="user.username"></s:textfield> <s:password label="密码" name="user.password"></s:password> <s:submit value="登录"></s:submit> </s:form> </center> </body> </html>
15、添加页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>添加新用户</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <center> <h1>添加新用户</h1> <s:form action="user_add" namespace="/user" method="post"> <s:textfield label="用户名" name="user.username"></s:textfield> <s:password label="密码" name="user.password"></s:password> <s:submit value="提交"></s:submit> </s:form> </center> </body> </html>
16、修改页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>修改用户</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <center> <h1>修改用户</h1> <s:form action="user_update" namespace="/user" method="post"> <s:hidden name="user.id"></s:hidden> <s:textfield label="用户名" name="user.username"></s:textfield> <s:password label="密码" name="user.password"></s:password> <s:submit value="提交"></s:submit> </s:form> </center> </body> </html>
17、列表页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用户列表</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <center> <h2>用户列表</h2> <h3><s:a action="user_addUI" namespace="/user">添加新用户</s:a> </h3> <table width="90%" border="1"> <tr> <th>用户id</th> <th>用户名称</th> <th>用户密码</th> <th>操作</th> </tr> <s:iterator value="userList"> <tr> <td><s:property value="id"/> </td> <td><s:property value="username"/> </td> <td><s:property value="password"/> </td> <td><s:a action="user_updateUI" namespace="/user"><s:param name="user.id">${id}</s:param>修改</s:a> <s:a action="user_delete" namespace="/user"><s:param name="user.id">${id}</s:param>删除</s:a></td> </tr> </s:iterator> </table> </center> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。