我的第一个Springboot项目


  1. 第一步,新建Project

 

把application.properties改成application.yml

 application.yml的内容:url?后面参数很关键

# 应用服务 WEB 访问端口
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.demo.bean
spring:
datasource:
name: testdb
password: 123456
username: root
url: jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
server:
port: 8080


创建文件后的目录树:

pom.xml 的dependencies



org.springframework.boot
spring-boot-starter-web


org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4


mysql
mysql-connector-java
runtime


org.springframework.boot
spring-boot-starter-test
test


junit
junit



 UserBean 代码:

package com.example.demo.bean;

public class UserBean {
private int ID;
private String Name;
private String Password;
public int getID(){
return ID;
}
public void setID(int ID){
this.ID=ID;
}
public String getName(){
return Name;
}
public void setName(String name){
this.Name=name;
}
public String getPassword(){
return Password;
}
public void setPassword(String password){
this.Password =password;
}

}

LoginController 代码:
package com.example.demo.controller;


import com.example.demo.bean.UserBean;
import com.example.demo.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.bind.annotation.RequestMethod;

@Controller
public class LoginController {
@Autowired
UserService userService;

@RequestMapping("/login")
public String show(){
return "login";
}

@RequestMapping(value="/loginIn",method = RequestMethod.POST)
public String login(String name,String password){
UserBean userBean=userService.loginIn(name,password);
if(userBean!=null){
return "success";
}else{
return "error";
}
}
}

UserMapper 代码:
package com.example.demo.mapper;

import com.example.demo.bean.UserBean;

public interface UserMapper {
UserBean getInfo(String name,String password);
}

UserService 代码:
package com.example.demo.service;

import com.example.demo.bean.UserBean;

public interface UserService {
UserBean loginIn(String name,String password);
}

UserServiceImpl 代码:
package com.example.demo.serviceImpl;

import com.example.demo.bean.UserBean;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;

@Override
public UserBean loginIn(String name,String password){
return userMapper.getInfo(name,password);
}
}

DemoApplication 代码:
package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

UserMapper.xml 内容:

<?xml version="1.0" encoding="UTF-8"?>







error.html 内容:




error


登录失败!





login.html 内容:




login



账号:

密码:








success.html 内容:





success


登录成功!




TestApplicationTest 的代码:

package com.example.demo;

import com.example.demo.bean.UserBean;
import com.example.demo.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
class TestApplicationTests {

@Autowired
UserService userService;

@Test
public void contextLoads() {
UserBean userBean=userService.loginIn("a","a");
System.out.println("该用户ID为:");
System.out.println(userBean.getID());
}

}