開(kāi)始折騰SpringBoot池充,需要配置的東西不多躯保,相比Spring原有的一大堆maven依賴(lài)來(lái)說(shuō)往扔,簡(jiǎn)直是web開(kāi)發(fā)利器贩猎!一個(gè)登陸頁(yè)面為例,以此記錄萍膛。
準(zhǔn)備
maven配置
新建一個(gè)maven項(xiàng)目吭服,不用多說(shuō),配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 直接繼承父類(lèi)蝗罗,記得添加最后的repository標(biāo)簽的內(nèi)容-->
<!-- 也可以自己指定SpringBoot依賴(lài)包的版本號(hào) -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- 一個(gè)很好用的注解插件艇棕,需要在IDEA-Setting-Plugin中安裝lombok插件才能用 -->
<!-- 也可以不加入這個(gè)依賴(lài)包,用傳統(tǒng)方式生成getter和setter -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
<scope>provided</scope>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- spring boot -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- spring boot -->
<!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
數(shù)據(jù)庫(kù)配置
插兩張表和一條記錄
CREATE TABLE IF NOT EXISTS t_user(
user_id INT AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(30),
credits INT,
password VARCHAR(32),
last_visit datetime,
last_ip VARCHAR(23)
)ENGINE=InnoDB;
CREATE TABLE t_login_log(
login_log_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
ip VARCHAR(23),
login_datetime datetime
)ENGINE=InnoDB;
INSERT INTO t_user(user_name, password)VALUES('admin','123456');
然后在項(xiàng)目中添加application.properties文件串塑,配置數(shù)據(jù)庫(kù)信息沼琉。
spring.datasource.url=jdbc:mysql://localhost:3306/webstudy
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-wait=10000
spring.datasource.max-active=50
spring.datasource.max-idle=10
spring.datasource.min-idle=8
spring.datasource.test-on-borrow=true
spring.datasource.validation-query=select 1
# 指定視圖路徑的前綴
spring.mvc.view.prefix=/WEB-INF/jsp/
# 指定視圖文件的后綴
spring.mvc.view.suffix=.jsp
項(xiàng)目文件結(jié)構(gòu)
dao層:負(fù)責(zé)直接和數(shù)據(jù)庫(kù)中的數(shù)據(jù)打交道。
domain:VO或SO拟赊,主要是出參入?yún)⒌臄?shù)據(jù)結(jié)構(gòu)類(lèi)刺桃。
service:業(yè)務(wù)邏輯層,帶事務(wù)的邏輯一般都放在這層吸祟。
controller:web頁(yè)面跳轉(zhuǎn)控制瑟慈。
右擊項(xiàng)目名稱(chēng)->Add Framework Support -> 添加Web Application模塊。
要注意的是:IDEA自動(dòng)添加的web模塊位置需要調(diào)整一下屋匕,不然會(huì)報(bào)找不到j(luò)ps文件的錯(cuò)誤葛碧。web目錄結(jié)構(gòu)如下。
代碼
此案例用最基本的JDBC和數(shù)據(jù)庫(kù)交互过吻,習(xí)慣性先寫(xiě)domain層进泼,簡(jiǎn)單暴力蔗衡。(如果用mybatis可以自動(dòng)生成mapping文件)
domain層
User.java
public class User implements Serializable {
//lombok注解工具,也可以直接使用傳統(tǒng)的方法生成getter和setter
@Getter @Setter
private int userId;
@Getter @Setter
private String userName;
@Getter @Setter
private int credits;
@Getter @Setter
private String lastIp;
@Getter @Setter
private Date lastVisit;
}
LoginLog.java
public class LoginLog implements Serializable {
@Getter @Setter
private int loginLogId;
@Getter @Setter
private int userId;
@Getter @Setter
private String ip;
@Getter @Setter
private Date loginDate;
}
LoginCommand.java
//登錄使用的入?yún)⑷槿疲竺鏁?huì)用到
public class LoginCommand {
@Getter @Setter
private String userName;
@Getter @Setter
private String password;
}
dao層
UserDao.java
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
private final static String MATCH_COUNT_SQL = "SELECT count(*) FROM t_user WHERE user_name=? AND password=?";
private final static String UPDATE_LOGIN_INFO_SQL =
"UPDATE t_user SET last_visit=?, last_ip=?, credits=?, WHERE user_id=?";
//查詢(xún)數(shù)據(jù)庫(kù)中是否存在相應(yīng)用戶(hù)
public int getMatchCount(String userName, String password){
return jdbcTemplate.queryForObject(MATCH_COUNT_SQL, new Object[] {userName, password}, Integer.class);
}
//根據(jù)用戶(hù)名查詢(xún)用戶(hù)信息
public User findUserByUserName(final String userName){
final User user = new User();
jdbcTemplate.query(MATCH_COUNT_SQL, new Object[]{userName}, new RowCallbackHandler() {
public void processRow(ResultSet resultSet) throws SQLException {
user.setUserId(resultSet.getInt("user_id"));
user.setUserName(userName);
user.setCredits(resultSet.getInt("credits"));
}
});
return user;
}
//用戶(hù)登錄后绞惦,更新數(shù)據(jù)庫(kù)信息,修改用戶(hù)最后訪問(wèn)時(shí)間和登錄ip洋措,增加用戶(hù)積分
public void updateLoginInfo(User user){
jdbcTemplate.update(UPDATE_LOGIN_INFO_SQL, user.getLastVisit(),
user.getLastIp(), user.getCredits(), user.getUserId());
}
}
LoginDao.java
@Repository
public class LoginLogDao {
@Autowired
private JdbcTemplate jdbcTemplate;
private final static String INSERT_LOGIN_LOG_SQL=
"INSERT INTO t_login_log(user_id, ip, login_dateteme)VALUES(?,?,?)";
//在數(shù)據(jù)庫(kù)中插入用戶(hù)登錄的日志信息
public void insertLoginLog(LoginLog loginLog){
Object[] args = {loginLog.getUserId(), loginLog.getIp(), loginLog.getLoginDate()};
jdbcTemplate.update(INSERT_LOGIN_LOG_SQL, args);
}
}
service層
@Service
public class UserService {
@Autowired
private UserDao userDao;
@Autowired
private LoginLogDao loginLogDao;
public boolean hasMatchUser(String userName, String password){
int matchCount = userDao.getMatchCount(userName, password);
return matchCount > 0;
}
public User findUserByUserName(String userName){
return userDao.findUserByUserName(userName);
}
/**
* 事務(wù)增強(qiáng)
* @param user
*/
@Transactional
public void loginSuccess(User user){
user.setCredits(5 + user.getCredits());
LoginLog loginLog = new LoginLog();
loginLog.setUserId(user.getUserId());
loginLog.setIp(user.getLastIp());
loginLog.setLoginDate(user.getLastVisit());
//方法做了事務(wù)增強(qiáng)济蝉,此處同時(shí)修改用戶(hù)表和登錄日志表,如果在過(guò)程中出錯(cuò)或者中斷菠发,
//數(shù)據(jù)庫(kù)內(nèi)容自動(dòng)就會(huì)回滾到?jīng)]有執(zhí)行這個(gè)方法的狀態(tài)王滤。
userDao.updateLoginInfo(user);
loginLogDao.insertLoginLog(loginLog);
}
}
Controller
@Controller
public class LoginController {
@Autowired
private UserService userService;
//可以配置多個(gè)映射路徑
@RequestMapping(value = {"/", "/index.html"}, method = RequestMethod.GET)
public ModelAndView loginPage(){
return new ModelAndView("login");
}
@RequestMapping(value = "/loginCheck.html", method = RequestMethod.GET)
public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand){
boolean isValidUser = userService.hasMatchUser(loginCommand.getUserName(), loginCommand.getPassword());
if(!isValidUser){
return new ModelAndView("login", "error","用戶(hù)名密碼錯(cuò)誤");
} 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");
}
}
jsp代碼
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>xhtc</title>
</head>
<body>
<c:if test="${!empty error}">
<font color="red"><c: out value="${error}"/></font>
</c:if>
<form action='<c:url value="/loginCheck.html"/>' method="post">
用戶(hù)名:
<input type="text" name="userName">
<br>
密碼:
<input type="password" name="password">
<br>
<input type="submit" value="登錄"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>
此時(shí)的工程目錄結(jié)構(gòu)。
啟動(dòng)SpringBoot
application.java
@SpringBootApplication
//開(kāi)啟事務(wù)支持滓鸠,可以在Service方法上標(biāo)注@Transaction表示事務(wù)增強(qiáng)
@EnableTransactionManagement
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
return application.sources(Application.class);
}
/**
* 自定義事務(wù)管理方法
* 此時(shí)SpringBoot會(huì)加載自定義的事務(wù)管理器雁乡,不會(huì)重新實(shí)例化其他事務(wù)管理器
* @param dataSource
* @return
*/
@Bean
public PlatformTransactionManager txManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
"基于Spring Boot應(yīng)用,由于當(dāng)前應(yīng)用包含了一個(gè)可直接執(zhí)行的Application類(lèi)糜俗,所以在開(kāi)發(fā)過(guò)程中踱稍,大家很容易在IDE(如IDEA工具)中單擊右鍵鼠標(biāo)運(yùn)行當(dāng)前類(lèi)。雖然可以啟動(dòng)當(dāng)前應(yīng)用吩跋,在非Web應(yīng)用中可能不會(huì)有什么問(wèn)題寞射,單在Web應(yīng)用中,如果采用上述方法直接運(yùn)行應(yīng)用锌钮,那么在訪問(wèn)有視圖的頁(yè)面時(shí)(如JSP)桥温,會(huì)一直報(bào)404錯(cuò)誤。因?yàn)橹苯舆\(yùn)行當(dāng)前啟動(dòng)類(lèi)梁丘,Spring Boot無(wú)法找到當(dāng)前頁(yè)面資源侵浸。因此,基于Spring Boot的應(yīng)用在開(kāi)發(fā)調(diào)試的時(shí)候氛谜,一定要基于Spring Boot提供的spring-boot-maven-plugin插件命令來(lái)運(yùn)行應(yīng)用或通過(guò)Spring Boot命令行來(lái)運(yùn)行應(yīng)用掏觉。"
—— 摘自<精通Spring 4.x ——企業(yè)應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)>
所以,在web應(yīng)用中值漫,要用這樣的方法去啟動(dòng)SpringBoot澳腹。
啟動(dòng)成功后,在瀏覽器輸入 http://localhost:8080/ 即可看到登錄頁(yè)面杨何。
** 部署成功酱塔!**