Mysql > select * from mysqld where mysqld = mybaits
(1) Configure the POM.xml dependency
<! Add MySql dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<! Add myBatis dependency -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
Copy the code
(2) Configure application.properties
Spring. The datasource. Url = JDBC: mysql: / / 127.0.0.1:3306 / database name is here? CharacterEncoding = utf-8 & useSSL = false&serverTimezone = GMT % 2 b8 spring. The datasource. The username = this is the account Spring. The datasource. Password = password spring here. The datasource. The driver - class - name = com. Mysql. Cj). The JDBC driver mybatis.mapper-locations=classpath:mappering/*.xmlCopy the code
(3) Create entity class
Create entities, Services, and Mapper folders in the Demo folder. Create a Mappering folder under Resources.
Create a new loginUser file in the entities folder:
package com.example.demo.entities;
public class loginUser {
private String username;
private int password;
public String getUsername(a) {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getPassword(a) {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public loginUser(a){};
public loginUser(String name,int pwd) {
this.password = pwd;
this.username = name;
}
@Override
public String toString(a) {
return "User [username=" + username + ", password=" + password + "]"; }}Copy the code
(4) Write the configuration file of mapper
Create userMappling.xml in the Mappering folder
<! DOCTYPEmapper PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entities.loginUser">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="INTEGER" property="password" />
</resultMap>
<select id="Sel" resultType="com.example.demo.entities.loginUser">
select * from User where id = #{id}
</select>
</mapper>
Copy the code
(5) Write Mapper mapping files
Create a usermapper.class file in the mapper folder
package com.example.demo.mapper;
import org.springframework.stereotype.Repository;
import com.example.demo.entities.loginUser;
@Repository
public interface UserMapper {
loginUser Sel(int id);
}
Copy the code
(6) Write service
Create a UserService folder in the Services folder
package com.example.demo.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.entities.loginUser;
import com.example.demo.mapper.*;
@Service
public class UserService {
@Autowired
UserMapper usermapper;
public loginUser Sel(int id){
returnusermapper.Sel(id); }}Copy the code
(7) Configure the Controller
Open the testController we created earlier. The connection
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.services.UserService;
@RestController
@RequestMapping("/testBoot")
public class testController {
@Autowired
private UserService userService;
@RequestMapping("/hello")
public String hello(a) {
return "Hello Spring Boot!";
}
@RequestMapping("/getUser")
public String GetUser(a){
System.out.print("Here we go.");
return userService.Sel(0).toString(); }}Copy the code
(8) Add the DidididiApplication file
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 DidididiApplication {
public static void main(String[] args) { SpringApplication.run(DidididiApplication.class, args); }}Copy the code