用Eclipse创建一个spring boot(连接mysql,简单使用mybatis)

1、连接mysql(用的mybaits)

(1)配置pom.xml依赖

				<!--添加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)配置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)创建实体类

创建entities、services、mapper文件夹,具体位置都在demo文件夹下。在resources下建mappering文件夹。
在这里插入图片描述

在entities文件夹内新建一个loginUser文件:

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)写mapper的配置文件

在mappering文件夹里创建UserMappring.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">
    <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)写Mapper映射文件

在mapper文件夹里创建一个UserMapper.class文件

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)写服务

在services文件夹里创建一个UserService文件夹

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)配置Controller

打开我们之前创建的testController。连接

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)补充DidididiApplication文件

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

}

猜你喜欢

转载自blog.csdn.net/weixin_45743162/article/details/111648249