实习第二周2
到了周三下午,我SSH学的差不多的时候,PM看了我的工作情况,跟我说你用hibernate干吗,这是一个笨重的东西,直接用Spring和Struts。
然后我又只能调转方向去研究Spring+Struts2。
不使用hibernate,那么只能使用jdbctemplete,不过可以使用jdbcdaosupport。在extends JdbcDaoSupport类后可以使用this.getJdbcTemplate()获得jdbctemplete,在Spring配置注入时也只需要注入datasourse,以下为各种代码。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/icsa?characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="" /> </bean> <!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate"> <property name = "dataSource" ref="dataSource"/> </bean>--> <bean id="userDAO" class="dao.impl.UserDAOImpl"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="userService" class="service.impl.UserServiceImpl"> <property name="userDAO" ref="userDAO" /> <constructor-arg ref="userDAO" /> </bean> <bean name="LoginAction" class="action.LoginAction"> <property name="userService" ref="userService" /> </bean> <bean name="TestAction" class="action.TestAction"> <property name="userService" ref="userService" /> </bean> </beans>
在具体增删改查时则不用hql,改用原来的sql,为了安全,这里使用了PreparedStatement编译SQL语句。
package dao.impl; import common.DaoException; import dao.UserDao; import entity.UserEntity; import org.springframework.jdbc.core.PreparedStatementSetter; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; /** * Created by PRO_HD_Rorz on 2015/8/23. */ public class UserDAOImpl extends JdbcDaoSupport implements UserDao{ @Override public boolean exists(UserEntity userEntity) { return false; } @Override public String getPassword(UserEntity userEntity) { String sql = "select password from user where username like ?"; //List<java.lang.String> gpassword = template.find(hql, userEntity.getName()); java.util.List<String> password; password = this.getJdbcTemplate().query(sql, new Object[]{userEntity.getUsername()}, new UserRowMapper()); if(password.size() > 0) { return password.get(0); } return null; } @Override public int create(Object object) throws DaoException { int result = 0; if (object == null || object instanceof UserEntity) return result; final UserEntity userEntity = (UserEntity) object; String sql = "insert into user(username, password, role, reserve1, reserve2, reserve3, reserve4) values (?,?,?,?,?,?,?)"; try { result = this.getJdbcTemplate().update(sql,new PreparedStatementSetter() { @Override public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1, userEntity.getUsername()); ps.setString(2, userEntity.getPassword()); ps.setString(3, userEntity.getRole()); ps.setString(4, userEntity.getReserve1()); ps.setString(5, userEntity.getReserve2()); ps.setString(6, userEntity.getReserve3()); ps.setString(7, userEntity.getReserve4()); } }); } catch (Exception e) { throw new DaoException("error"); } //this.getJdbcTemplate().update("insert into user values (?)", userEntity); return result; } @Override public int updata(Object object) throws DaoException { int result = 0; if (object == null || object instanceof UserEntity) return result; final UserEntity userEntity = (UserEntity) object; String sql = "update user set password = ?, username = ? where user_id = ?"; try { result = this.getJdbcTemplate().update(sql, new PreparedStatementSetter() { @Override public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1, userEntity.getPassword()); ps.setString(2, userEntity.getUsername()); ps.setInt(3, userEntity.getUserId()); ps.setString(4, userEntity.getReserve1()); ps.setString(5, userEntity.getReserve2()); ps.setString(6, userEntity.getReserve3()); ps.setString(7, userEntity.getReserve4()); } }); } catch (Exception e) { throw new DaoException("error"); } return result; } @Override public int delete(Object object) throws DaoException { int result = 0; if (object == null || object instanceof UserEntity) return result; UserEntity userEntity = (UserEntity) object; String sql = "delete from user where user_id = ?"; try { result = this.getJdbcTemplate().update(sql, userEntity.getUserId()); } catch (Exception e) { throw new DaoException("error"); } return result; } @Override public Object find(Object object) throws DaoException { return null; } @Override public List<Object> findall(Object object) { return null; } @Override public int findcount(Object object) { return 0; } private class UserRowMapper implements RowMapper { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { String pwd = new String(); pwd = rs.getString("password"); return pwd; } } }
然后Struts2的Action处理
package action; import com.opensymphony.xwork2.ModelDriven; import common.BaseAction; import entity.UserEntity; import common.ServiceManager; import service.impl.UserServiceImpl; /** * Created by PRO_HD_Rorz on 2015/7/21. */ public class LoginAction extends BaseAction implements ModelDriven<UserEntity>{ //public class LoginAction{ private UserEntity userEntity = new UserEntity(); private UserServiceImpl userService; public UserEntity getModel() { return userEntity; } public String execute () throws Exception { serviceManager = new ServiceManager(); UserServiceImpl userService = serviceManager.getUserService(); try { if(userService.verifyUser(userEntity)) { saveCookie("username", userEntity.getUsername(), 24*60*60); //HttpSession session = request.getSession(); //session.setAttribute("name", userEntity.getName()); //session.setMaxInactiveInterval(60 * 60); return SUCCESS; } } catch (Exception e) { System.out.println(e.getMessage()); } return ERROR; } public void setUserService(UserServiceImpl userService) { this.userService = userService; } public UserServiceImpl getUserService() { return userService; } }
login的jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <%-- Created by IntelliJ IDEA. User: PRO_HD_Rorz Date: 2015/7/21 Time: 17:06 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>login</title> </head> <body> <s:form action="login" > <s:textfield label="用户名" name="username" /> <s:password label="密码" name="password" /> <s:submit value="登陆" /> </s:form> </body> </html>