标签:
小结:Spring3开始,尽量使用注解,能不用XML配置就不用XML配置。
jar包
aopalliance.jar
commons-dbcp-1.4.jar
commons-logging-1.2.jar
commons-pool.jar
hamcrest-core-1.3.jar
jstl-1.2.jar
jstl-impl.jar
junit-4.11.jar
log4j-1.2.17.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
mysql-connector-java-5.1.7-bin.jar
spring-aop-3.2.9.RELEASE.jar
spring-aspects-3.2.9.RELEASE.jar
spring-beans-3.2.9.RELEASE.jar
spring-build-src-3.2.9.RELEASE.jar
spring-context-3.2.9.RELEASE.jar
spring-context-support-3.2.9.RELEASE.jar
spring-core-3.2.9.RELEASE.jar
spring-expression-3.2.9.RELEASE.jar
spring-instrument-3.2.9.RELEASE.jar
spring-instrument-tomcat-3.2.9.RELEASE.jar
spring-jdbc-3.2.9.RELEASE.jar
spring-jms-3.2.9.RELEASE.jar
spring-orm-3.2.9.RELEASE.jar
spring-oxm-3.2.9.RELEASE.jar
spring-struts-3.2.9.RELEASE.jar
spring-test-3.2.9.RELEASE.jar
spring-tx-3.2.9.RELEASE.jar
spring-web-3.2.9.RELEASE.jar
spring-webmvc-3.2.9.RELEASE.jar
spring-webmvc-portlet-3.2.9.RELEASE.jar
standard-1.1.2.jar
目录结构
Domain与数据库 略
Dao
1 package com.baobaotao.dao; 2 3 import com.baobaotao.domain.User; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.jdbc.core.JdbcTemplate; 6 import org.springframework.jdbc.core.RowCallbackHandler; 7 import org.springframework.stereotype.Repository; 8 9 import java.sql.ResultSet; 10 import java.sql.SQLException; 11 12 /** 13 * Created by sherry on 000028/6/28 0:53. 14 */ 15 @Repository/*注解定义一个Dao*/ 16 public class UserDao { 17 /*通过类型进行依赖注入*/ 18 @Autowired 19 private JdbcTemplate jdbcTemplate; 20 21 /** 22 * 用户存在数目 23 * @param userName 用户名 24 * @param password 密码 25 * @return int 26 */ 27 public int getMatchCount(String userName,String password){ 28 String sql = " SELECT count(1) FROM t_user WHERE user_name = ? AND password = ? "; 29 return jdbcTemplate.queryForInt(sql,new Object[]{userName,password}); 30 } 31 32 /** 33 * 查找用户 34 * @param userName 用户名 35 * @return User 36 */ 37 public User findUserByUserName(String userName){ 38 String sql = " SELECT user_id,user_name,credits FROM t_user WHERE user_name = ? "; 39 final User user = new User(); 40 jdbcTemplate.query(sql, new Object[]{userName}, new RowCallbackHandler() { 41 @Override 42 public void processRow(ResultSet resultSet) throws SQLException { 43 user.setUserId(resultSet.getInt("user_id")); 44 user.setUserName(userName); 45 user.setCredits(resultSet.getInt("credits")); 46 } 47 }); 48 return user; 49 } 50 51 /** 52 * 更新用户登录信息 53 * @param user User 54 */ 55 public void updateLoginInfo(User user){ 56 String sql = " UPDATE t_user SET last_visit = ?,last_ip = ?,credits = ? WHERE user_id = ? "; 57 jdbcTemplate.update(sql,new Object[]{user.getLastVisit(),user.getLastIp(),user.getCredits(),user.getUserId()}); 58 } 59 }
1 package com.baobaotao.dao; 2 3 import com.baobaotao.domain.LoginLog; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.jdbc.core.JdbcTemplate; 6 import org.springframework.stereotype.Repository; 7 8 /** 9 * Created by sherry on 000028/6/28 1:04. 10 */ 11 @Repository/*注解定义一个Dao*/ 12 public class LoginLogDao { 13 @Autowired 14 private JdbcTemplate jdbcTemplate; 15 16 /** 17 * 新增登录日志 18 * @param loginLog 日志信息 19 */ 20 public void insertLoginLog(LoginLog loginLog){ 21 String sql = " INSERT INTO t_login_log(user_id,ip,login_datetime) VALUES(?,?,?) "; 22 Object[] args = {loginLog.getUserId(),loginLog.getIp(),loginLog.getLoginDate()}; 23 jdbcTemplate.update(sql,args); 24 } 25 }
Service
package com.baobaotao.service; import com.baobaotao.dao.LoginLogDao; import com.baobaotao.dao.UserDao; import com.baobaotao.domain.LoginLog; import com.baobaotao.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * Created by sherry on 000028/6/28 1:09. */ @Service/*注解一个服务层的Bean*/ @Transactional(rollbackFor = Exception.class) public class UserService { @Autowired private UserDao userDao; @Autowired private LoginLogDao loginLogDao; /** * 是否存在用户 * @param userName 用户名 * @param password 密码 * @return boolean */ @Transactional(readOnly = true) public boolean hasMatchUser(String userName,String password){ return userDao.getMatchCount(userName,password)>0; } @Transactional(readOnly = true) public User findUserByUserName(String userName) { User user = userDao.findUserByUserName(userName); return user; } public void loginSuccess(User user) { user.setCredits(user.getCredits() + 5); LoginLog loginLog = new LoginLog(); loginLog.setUserId(user.getUserId()); loginLog.setIp(user.getLastIp()); loginLog.setLoginDate(user.getLastVisit()); userDao.updateLoginInfo(user); loginLogDao.insertLoginLog(loginLog); } }
Spring配置
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!--打开自动扫描--> <context:component-scan base-package="com.baobaotao.dao"/> <context:component-scan base-package="com.baobaotao.service"/> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${driverClassName}" p:url="${url}" p:username="${username_mysql}" p:password="${password}"/> <!--定义JDBC模板bean--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"/> <!--事务--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <!--使用注解配置事务--> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
<?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="com.baobaotao.web"/> <import resource="classpath:applicationContext.xml"/> <!--视图解析规则--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/> </beans>
Controller
package com.baobaotao.web; import com.baobaotao.domain.User; import com.baobaotao.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.util.Date; /** * Created by sherry on 000025/6/25 21:18. */ @Controller/*注解为Spring MVC Controller*/ public class LoginController { @Autowired private UserService userService; @RequestMapping(value = "/index.html") public String loginPage(){ return "login"; } @RequestMapping(value = "/loginController.html") public ModelAndView handle(HttpServletRequest request, LoginCommand loginCommand) { boolean isValidateUser = userService.hasMatchUser(loginCommand.getUserName(),loginCommand.getPassword()); if (!isValidateUser){ return new ModelAndView("login","error","用户名或密码错误"); }else { User user = userService.findUserByUserName(loginCommand.getUserName()); user.setLastIp(request.getLocalAddr()); user.setLastVisit(new Date()); userService.loginSuccess(user); request.getSession().setAttribute("user",user); return new ModelAndView("main"); } } }
package com.baobaotao.web; /** * Created by sherry on 000025/6/25 21:19. */ public class LoginCommand { private String userName; private String password; 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; } }
页面:略
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--从类路径加载配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--启动Spring监听容器--> <listener> <listener-class>org.springframework.web.context.ContextLoader</listener-class> </listener> <!--Spring MVC 主控 Servlet--> <servlet> <servlet-name>baobaotao</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>baobaotao</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
属性文件与日志文件:略
标签:
原文地址:http://www.cnblogs.com/sherrykid/p/4605036.html