一、在idea中创建一个meven项目
1.导入一个mysql的数据库的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.在里面创建三个不同的包
controller:用于实现用户与服务器之间的交互
service:实现业务的功能
dao:与数据库交互的
项目的结构图:
(1)首先创建user实例
package com.shujia.bean;
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User() {
}
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;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
(2)创建UserController类
package com.shujia.controller;
import com.shujia.bean.User;
import com.shujia.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService ;
@GetMapping("/login")
public String login(String username,String password){
User user = new User(username, password);
String login = userService.login(user);
return login;
}
@GetMapping("/register")
public String register(String username,String password,String newPassword){
String register = userService.register(username,password,newPassword);
return register;
}
}
(3)创建UserService的接口和实现UserService的UserServiceImpl实现类
下面这个是UserService接口
package com.shujia.service;
import com.shujia.bean.User;
public interface UserService {
String login(User user);
String register(String username,String password,String newPassword );
}
下面是UserService的实现类
package com.shujia.service.impl;
import com.shujia.bean.User;
import com.shujia.dao.UserDao;
import com.shujia.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao ;
@Override
public String login(User user) {
if (user.getUsername().trim().isEmpty() | user.getPassword().trim().isEmpty()){
return "用户名密码不能为空";
}
User user1 = userDao.queryUserByUsername(user.getUsername());
if (user1==null){
return "用户名不存在";
}
if (! user.getPassword().equals(user1.getPassword())){
return "密码不正确";
}
return "登录成功";
}
@Override
public String register(String username, String password, String newPassword) {
if (username.trim().isEmpty() | password.trim().isEmpty()| newPassword.trim().isEmpty()){
return "用户名密码不能为空";
}
if (! password.equals(newPassword)){
return "密码不一致";
}
User user = userDao.queryUserByUsername(username);
if (user!=null){
return "用户已存在";
}
boolean flag = userDao.insert(new User(username, password));
if (! flag){
return "注册失败";
}
return "注册成功";
}
}
(4)创建UserDao接口和实现该接口的实现类UserDaoImpl,该部分是与数据库进行交互的
下面是UserDao接口的代码块
package com.shujia.service;
import com.shujia.bean.User;
public interface UserService {
String login(User user);
String register(String username,String password,String newPassword );
}
下面UserDao接口的实现类UserDaoImpl的实现类
package com.shujia.dao.impl;
import com.shujia.bean.User;
import com.shujia.dao.UserDao;
import org.springframework.stereotype.Component;
import java.sql.*;
@Component
public class UserDaoImpl implements UserDao {
private static Connection con;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:mysql://master:3306/user", "root", "123456");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public User queryUserByUsername(String username) {
User user = null;
try {
PreparedStatement stat = con.prepareStatement("select * from t_user where username=?");
stat.setString(1,username);
ResultSet resultSet = stat.executeQuery();
if (resultSet.next()){
String username1 = resultSet.getString("username");
String password = resultSet.getString("password");
user = new User(username1,password);
}
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
@Override
public boolean insert(User user) {
try {
PreparedStatement stat = con.prepareStatement("insert into t_user(username,password) values(?,?)");
stat.setString(1,user.getUsername());
stat.setString(2,user.getPassword());
int i = stat.executeUpdate();
if (i==1){
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
总结:这样使用面向接口写代码,使代码间的耦合性降低了,如需修改代码,不用整体的动代码,只需要修改需要修改的代码块。这样写的好处可以分给不同的人来实现不能的部分,大大加快的项目的效率,我们需要学习这种编写代码的风格。