Spring Boot +mysql+mybatis搭建 微服务入门篇(第三章)

1、在pom.xml中增加mybatis、mysql相关依赖。

    
	
		org.mybatis.spring.boot
		mybatis-spring-boot-starter
		1.3.1
	
	
	
		mysql
		mysql-connector-java
		5.1.21
	
    
    
		org.projectlombok
		lombok
		1.16.10
	

2、在数据库中新建一张UserInfo表添加若干数据。 并在项目中新建model、controller、mapper、server层。

(1)model层新建UserInfo类

@Data
public class UserInfo {
    private String id;
    private String name;
    private String age;
    private String sex;
}

(2)mapper层新建UserInfoMapper类,并写上一个查询接口。

public interface UserInfoMapper {
    List findUserInfo();
}

(3)server层新建UserInfoServer、UserInfoServerImpl类。

   UserInfoServer类:

public interface UserInfoService {
    List findUserInfo();
}

  UserInfoServerImpl类:调用UserInfoMapper类写的接口方法。

@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public List findUserInfo() {
        return userInfoMapper.findUserInfo();
    }
}

(4)controller层定义UserInfoController类

@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;


    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public String getList() {
        List userList =  userInfoService.findUserInfo();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userList", userList);
        return jsonObject.toString();
    }

}

(5) 在resources文件下新建mybatis配置文件信息。

/resources/mybatis/config/mybatis-config.xml





    
    
        
    
    
        
    

/resources/mybatis/mapper/UserInfoMapper.xml





    


(6)在pox.xml中配置mysql、mybatis信息。

server:
  port: 8086
spring:
  datasource:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      password: 123456
mybatis:
  config-location: classpath:mybatis/config/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: com.example.demo.userInfo.model

注:到这里我们还需要配置一个扫描UserInfoMapper类的注解。

@MapperScan("com.example.demo.userInfo.mapper")

(1)在当前UserInfoServiceImpl类添加。

(2)在DemoApplication中添加。

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.example.demo.userInfo.mapper")
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {

		SpringApplication.run(DemoApplication.class, args);
	}

}

数据库中已经添加好的数据。

Spring Boot +mysql+mybatis搭建 微服务入门篇(第三章)_第1张图片

重启 Spring Boot服务 访问我们的地址:http://localhost:8086/getList   

Spring Boot +mysql+mybatis搭建 微服务入门篇(第三章)_第2张图片

整体的目录结构

Spring Boot +mysql+mybatis搭建 微服务入门篇(第三章)_第3张图片

到这里我们这篇内容就到这里了。

学如逆水行舟,不进则退。心似平原跑马,易放难收。全栈工程师是指掌握多种技能,并能利用多种技能独立完成产品的人。 也叫全端工程师(同时具备前端和后台能力),英文Full Stack engineer。【人工智能】【区块链】【系统/网络/运维】【云计算/大数据】【数据库】【移动开发】【后端开发】【游戏开发】【UI设计】【微服务】【爬虫】【Java】【Go】【C++】【PHP】【Python】【Android/IOS】【HTML/CSS】【JavaScript】【Node】。。。

欢迎各位大神萌新一起专研分享各行各业技术!

IT全栈工程师技术交流群:593674370

Spring Boot +mysql+mybatis搭建 微服务入门篇(第三章)_第4张图片

 

你可能感兴趣的:(Spring,Boot,微服务)