- 第一步,新建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
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
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"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="getInfo" parameterType="String" resultType="com.example.demo.bean.UserBean">
SELECT * FROM T_User WHERE name = #{name} AND password = #{password}
</select>
</mapper>
error.html 内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h1>登录失败!</h1>
</body>
</html>
login.html 内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form role="form" action="/loginIn" method="post">
账号:<input type="text" id="name" name="name"><br/>
密码:<input type="password" id="password" name="password"><br/>
<input type="submit" id="login" value="login">
</form>
</body>
</html>
success.html 内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
<h1>登录成功!</h1>
</body>
</html>
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());
}
}