Create a spring boot with Eclipse (connect mysql, simply use mybatis)

1. Connect to mysql (mybaits used)

(1) Configure pom.xml dependency

				<!--添加MySql依赖-->
		<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <!-- 添加mybatis依赖 -->
        
        <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.2</version>
        </dependency>

(2) Configure application.properties

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/这里是数据库名?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=这里是账号
spring.datasource.password=这里是密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mappering/*.xml

(3) Create an entity class

Create the entities, services, and mapper folders, the specific locations are all under the demo folder. Create a mappering folder under resources.
Insert picture description here

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() {
    
    
		return username;
	}
	public void setUsername(String username) {
    
    
		this.username = username;
	}
	public int getPassword() {
    
    
		return password;
	}
	public void setPassword(int password) {
    
    
		this.password = password;
	}
	public loginUser(){
    
    };
	public loginUser(String name,int pwd) {
    
    
		this.password = pwd;
		this.username = name;
	}
	@Override
	public String toString() {
    
    
		return "User [username=" + username + ", password=" + password + "]";
	}
}

(4) Write the configuration file of mapper

Create UserMappring.xml in the mappering folder

<?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">
    <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>

(5) Write Mapper mapping file

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);
}

(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){
    
    
	        return usermapper.Sel(id);
	    }
}

(7) Configure Controller

Open the testController we created earlier. 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() {
    
    
	        return "Hello Spring Boot!";
	    }
	  @RequestMapping("/getUser")
	    public String GetUser(){
    
    
		  System.out.print("到这里了");
	        return userService.Sel(0).toString();
	    }
}

(8) Supplement 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);
	}

}

Guess you like

Origin blog.csdn.net/weixin_45743162/article/details/111648249