标签:3.3 target att beans rest ima ges vax java
1 package com.smart; 2 import org.springframework.boot.SpringApplication; 3 import org.springframework.boot.autoconfigure.SpringBootApplication; 4 import org.springframework.boot.builder.SpringApplicationBuilder; 5 import org.springframework.boot.context.web.SpringBootServletInitializer; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 8 import org.springframework.transaction.PlatformTransactionManager; 9 import org.springframework.transaction.annotation.EnableTransactionManagement; 10 import org.springframework.web.WebApplicationInitializer; 11 12 import javax.sql.DataSource; 13 14 //@Configuration 15 //@ComponentScan 16 //@EnableAutoConfiguration 17 @SpringBootApplication 18 @EnableTransactionManagement 19 public class Application extends SpringBootServletInitializer implements WebApplicationInitializer { 20 @Bean 21 public PlatformTransactionManager txManager(DataSource dataSource) { 22 return new DataSourceTransactionManager(dataSource); 23 } 24 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 25 return application.sources(Application.class); 26 } 27 28 public static void main(String[] args) throws Exception { 29 SpringApplication.run(Application.class, args); 30 } 31 }
UserDao类
findUserByUserName():根据用户名获取对象
updateLoginInfo():更新用户积分,最后登录IP及最后登录时间
1 package com.smart.dao; 2 import com.smart.domain.User; 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.jdbc.core.JdbcTemplate; 5 import org.springframework.jdbc.core.RowCallbackHandler; 6 import org.springframework.stereotype.Repository; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 @Repository 10 public class UserDao { 11 private JdbcTemplate jdbcTemplate; 12 13 private final static String MATCH_COUNT_SQL = " SELECT count(*) FROM t_user " + 14 " WHERE user_name =? and password=? "; 15 private final static String UPDATE_LOGIN_INFO_SQL = " UPDATE t_user SET " + 16 " last_visit=?,last_ip=?,credits=? WHERE user_id =?"; 17 18 public int getMatchCount(String userName, String password) { 19 20 return jdbcTemplate.queryForObject(MATCH_COUNT_SQL, new Object[]{userName, password}, Integer.class); 21 } 22 23 public User findUserByUserName(final String userName) { 24 String sqlStr = " SELECT user_id,user_name,credits " 25 + " FROM t_user WHERE user_name =? "; 26 final User user = new User(); 27 jdbcTemplate.query(sqlStr, new Object[]{userName}, 28 new RowCallbackHandler() { 29 public void processRow(ResultSet rs) throws SQLException { 30 user.setUserId(rs.getInt("user_id")); 31 user.setUserName(userName); 32 user.setCredits(rs.getInt("credits")); 33 } 34 }); 35 return user; 36 } 37 38 public void updateLoginInfo(User user) { 39 jdbcTemplate.update(UPDATE_LOGIN_INFO_SQL, new Object[]{user.getLastVisit(), 40 user.getLastIp(), user.getCredits(), user.getUserId()}); 41 } 42 @Autowired 43 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 44 this.jdbcTemplate = jdbcTemplate; 45 } 46 }
LoginLogDao类
1 package com.smart.dao; 2 3 import com.smart.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 @Repository 9 public class LoginLogDao { 10 @Autowired 11 private JdbcTemplate jdbcTemplate; 12 13 //保存登陆日志SQL 14 private final static String INSERT_LOGIN_LOG_SQL= "INSERT INTO t_login_log(user_id,ip,login_datetime) VALUES(?,?,?)"; 15 16 public void insertLoginLog(LoginLog loginLog) { 17 Object[] args = { loginLog.getUserId(), loginLog.getIp(), 18 loginLog.getLoginDate() }; 19 jdbcTemplate.update(INSERT_LOGIN_LOG_SQL, args); 20 } 21 }
UserService类
1 package com.smart.service; 2 3 import com.smart.dao.LoginLogDao; 4 import com.smart.dao.UserDao; 5 import com.smart.domain.LoginLog; 6 import com.smart.domain.User; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Service; 9 import org.springframework.transaction.annotation.Transactional; 10 11 @Service 12 public class UserService { 13 14 private UserDao userDao; 15 16 private LoginLogDao loginLogDao; 17 18 19 public boolean hasMatchUser(String userName, String password) { 20 int matchCount =userDao.getMatchCount(userName, password); 21 return matchCount > 0; 22 } 23 24 public User findUserByUserName(String userName) { 25 return userDao.findUserByUserName(userName); 26 } 27 28 @Transactional 29 public void loginSuccess(User user) { 30 user.setCredits( 5 + user.getCredits()); 31 LoginLog loginLog = new LoginLog(); 32 loginLog.setUserId(user.getUserId()); 33 loginLog.setIp(user.getLastIp()); 34 loginLog.setLoginDate(user.getLastVisit()); 35 userDao.updateLoginInfo(user); 36 loginLogDao.insertLoginLog(loginLog); 37 } 38 39 @Autowired 40 public void setUserDao(UserDao userDao) { 41 this.userDao = userDao; 42 } 43 44 @Autowired 45 public void setLoginLogDao(LoginLogDao loginLogDao) { 46 this.loginLogDao = loginLogDao; 47 } 48 }
LoginCommand
1 package com.smart.web; 2 3 public class LoginCommand { 4 private String userName; 5 6 private String password; 7 8 public String getPassword() { 9 return password; 10 } 11 12 public void setPassword(String password) { 13 this.password = password; 14 } 15 16 public String getUserName() { 17 return userName; 18 } 19 20 public void setUserName(String userName) { 21 this.userName = userName; 22 } 23 }
LoginController
1 package com.smart.web; 2 3 import com.smart.domain.User; 4 import com.smart.service.UserService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 import org.springframework.web.servlet.ModelAndView; 9 import javax.servlet.http.HttpServletRequest; 10 import java.util.Date; 11 12 @RestController 13 public class LoginController{ 14 private UserService userService; 15 16 @RequestMapping(value = {"/","/index.html"}) 17 public ModelAndView loginPage(){ 18 return new ModelAndView("login"); 19 } 20 21 @RequestMapping(value = "/loginCheck.html") 22 public ModelAndView loginCheck(HttpServletRequest request,LoginCommand loginCommand){ 23 boolean isValidUser = userService.hasMatchUser(loginCommand.getUserName(), 24 loginCommand.getPassword()); 25 if (!isValidUser) { 26 return new ModelAndView("login", "error", "用户名或密码错误。"); 27 } else { 28 User user = userService.findUserByUserName(loginCommand 29 .getUserName()); 30 user.setLastIp(request.getLocalAddr()); 31 user.setLastVisit(new Date()); 32 userService.loginSuccess(user); 33 request.getSession().setAttribute("user", user); 34 return new ModelAndView("main"); 35 } 36 } 37 38 @Autowired 39 public void setUserService(UserService userService) { 40 this.userService = userService; 41 } 42 }
pom.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 6 <parent> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-parent</artifactId> 9 <version>1.3.3.RELEASE</version> 10 </parent> 11 <modelVersion>4.0.0</modelVersion> 12 <artifactId>chapter3</artifactId> 13 <name>Spring4.x</name> 14 <packaging>war</packaging> 15 <dependencies> 16 <dependency> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-web</artifactId> 19 </dependency> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-jdbc</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-actuator</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>mysql</groupId> 30 <artifactId>mysql-connector-java</artifactId> 31 <version>${mysql.version}</version> 32 </dependency> 33 <dependency> 34 <groupId>org.apache.tomcat.embed</groupId> 35 <artifactId>tomcat-embed-jasper</artifactId> 36 <scope>provided</scope> 37 </dependency> 38 <dependency> 39 <groupId>javax.servlet</groupId> 40 <artifactId>jstl</artifactId> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-test</artifactId> 45 <scope>test</scope> 46 </dependency> 47 </dependencies> 48 <build> 49 <plugins> 50 <plugin> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-maven-plugin</artifactId> 53 </plugin> 54 <plugin> 55 <groupId>org.apache.maven.plugins</groupId> 56 <artifactId>maven-surefire-plugin</artifactId> 57 <configuration> 58 <useSystemClassLoader>false</useSystemClassLoader> 59 </configuration> 60 </plugin> 61 </plugins> 62 </build> 63 </project>
标签:3.3 target att beans rest ima ges vax java
原文地址:http://www.cnblogs.com/lwx521/p/7688896.html