直接上步骤
先上目录表:(每个目录用处就不多解释了)
如果报:Connection to @localhost failed. [08001] Could not create connection to database server. Attempt
在数据库路径后面添加:?serverTimezone=GMT
package com.example.test_1020.bean;
public class user {
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;
}
}
package com.example.test_1020.controller;
import com.example.test_1020.bean.user;
import com.example.test_1020.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 {
//将Service注入Web层
@Autowired
UserService userService;
@RequestMapping("/login")
public String show(){
return "login";
}
@RequestMapping(value = "/loginIn",method = RequestMethod.POST)
public String login(String name,String password){
user userBean = userService.loginIn(name,password);
if(userBean!=null){
return "success";
}else {
return "error";
}
}
}
package com.example.test_1020.mapper;
import com.example.test_1020.bean.user;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
@Component
public interface UserMapper {
// 如果有多个参数,需要加@param
user getInfo(@Param("name") String name,@Param("password") String password);
}
package com.example.test_1020.service;
import com.example.test_1020.bean.user;
import org.springframework.stereotype.Component;
@Component
public interface UserService {
user loginIn(String name, String password);
}
package com.example.test_1020.serviceImpl;
import com.example.test_1020.bean.user;
import com.example.test_1020.mapper.UserMapper;
import com.example.test_1020.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
//将DAO注入Service层
@Autowired
private UserMapper userMapper;
@Override
public user loginIn(String name, String password) {
return userMapper.getInfo(name,password);
}
}
Application文件一定要加@MapperScan注解,并标记mapper目录,不然会找不到
@SpringBootApplication
@MapperScan("com.example.test_1020.mapper")
public class Test1020Application {
public static void main(String[] args) {
SpringApplication.run(Test1020Application.class, args);
}
}
测试类:
package com.example.test_1020;
import com.example.test_1020.bean.user;
import com.example.test_1020.service.UserService;
import org.junit.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
public class TestApplicationTests {
@Autowired
UserService userService;
@Test
public void contextLoads() {
user userBean = userService.loginIn("qwe","111");
System.out.println("该用户ID为:");
System.out.println(userBean.getId());
}
}
在resources下创建mapper文件夹,用来存放xml文件
UserMapper.xml:
SELECT * FROM user WHERe name = #{name} AND password = #{password}
templates:
error
登录失败!
hello
你好!初学者,我是SpringBoot的简单启动页面!
login
success
登录成功!
application.yml:
## 应用名称
#spring.application.name=test_1020
## 应用服务 WEB 访问端口
#server.port=8080
##下面这些内容是为了让MyBatis映射
##指定Mybatis的Mapper文件
#mybatis.mapper-locations=classpath:mappers/*xml
##指定Mybatis的实体目录
#mybatis.type-aliases-package=com.example.test_1020.mybatis.entity
## 数据库驱动:
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
## 数据源名称
#spring.datasource.name=defaultDataSource
## 数据库连接地址
#spring.datasource.url=jdbc:mysql://localhost:3306/blue?serverTimezone=UTC
## 数据库用户名&密码:
#spring.datasource.username=***
#spring.datasource.password=***
server:
port:8080:
spring:
datasource:
name: test #数据库名
url: jdbc:mysql://localhost:3306/test #url
username: root #用户名
password: root #密码
driver-class-name: com.mysql.cj.jdbc.Driver #数据库链接驱动
mybatis:
mapper-locations: classpath:mapper/*.xml #配置映射文件
type-aliases-package: com.example.test_1020.bean #配置实体类
**
如果报时区异常,可以在url: jdbc:mysql://localhost:3306/test后加?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
**
一些包必须匹配
4.0.0 com.example test_1020 0.0.1-SNAPSHOT test_1020 Demo project for Spring Boot 1.8 UTF-8 UTF-8 2.3.7.RELEASE org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-thymeleaf 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 org.junit.vintage junit-vintage-engine junit junit org.springframework.boot spring-boot-test org.springframework spring-test org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE com.example.test_1020.Test1020Application repackage repackage



