SpringMVC是现在主流框架,包括我现在也是用的SpringBoot+SpringMvc+MySql+Mybatis+Redis+....。今天给大家带来整合SpringBoot+SpringMvc+mysql实现用户的登录和注册,并在mysql中保存和修改。
1. 打开http://start.spring.io/,填写需要用的项目功能。会自动生成基本的目录结构及porm.xml文件和dependencies,并自动配置需要的jar包。搭建spring boot常用的工具为Maven。
2. 下载Maven项目包。导入到eclipse。选择file--import---Existing Maven Project,然后自动生成如下项目结构
3. 自动生成的porm.xml文件如下。如果第一次构建,右击项目---Maven---update project会自动下载Jar包到Maven 的local Repository.(在D:\apache-maven-3.5.3\conf\settings.xml中设置
4.0.0
com.plin
bookebuy
0.0.1-SNAPSHOT
war
bookebuy
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
4. Spring Boot 的优点在于spring-boot-starter-parent集成了各做框架,只需要在Maven中把定义为parent标签。并且可以统一管理版本,避免版本间的冲突。
5.项目结构
5. application.properties 配置mysql连接信息
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/bookebuy
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active = 20
spring.datasource.max-idle=10
spring.datasource.min.idle=10
spring.datasource.initial-size=10
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
7. Spring MVC 三层代码
7.1 Controller层,Web访问服务器,会首先在这里请求,并映射到各个RequestMapping,然后处理各个逻辑。
//采用RestController,在返回时,如果registSucceful。jsp,直接返回这个页面,如果没有,直接返回该字符串
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.plin.bookebuy.domain.User;
import com.plin.bookebuy.service.IUserService;
//采用RestController,在返回时,如果registSucceful.jsp,直接返回这个页面,如果没有,直接返回该字符串
@RestController
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/user/regist")
public String regist(String username,String password,String email){
User user = new User();
user.setEmail(email);
user.setPassword(password);
user.setUsername(username);
userService.addUser(user);
System.out.println("consle---/user/regist");
return "registSucceful";
}
}
7.2 Service层
Controller层会调用Service层的方法。项目大部分的逻辑处理和事物处理都在Service层。比如判断用户或商品是否存在,然后调用Dao层进行处理或抛出全局异常到Controller及客户端。
package com.plin.bookebuy.service;
import com.plin.bookebuy.domain.User;
public interface IUserService {
void addUser(User user);
}
package com.plin.bookebuy.service;
import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import com.plin.bookebuy.dao.UserDao;
import com.plin.bookebuy.domain.User;
@Service
public class UserServiceImpl implements IUserService{
@Autowired
private UserDao userDao;
@Override
public void addUser(User user) {
userDao.addUser(user);
}
}
7.3 Dao层,连接数据库层,在这里执行连接数据库的操作
package com.plin.bookebuy.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.plin.bookebuy.domain.User;
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void addUser(User user) {
String sql = "INSERT INTO users VALUES(null,?,?,?,'user','0',null,null)";
jdbcTemplate
.update(sql,user.getUsername(),user.getPassword(),user.getEmail());
}
}
8. 程序入口,直接右键,run as java application, 即可启动这个Web项目。然后在服务端就可以访问。
package com.plin.bookebuy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BookebuyApplication {
public static void main(String[] args) {
SpringApplication.run(BookebuyApplication.class, args);
}
}
9.运行测试
在游览器中输入用户名和密码进行测试
数据库查看