栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合myBatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合myBatis

文章目录
  • pom.xml文件
    • 创建application.yml文件
      • 在service包中新建实现类UserService.java
        • 在controller包中新建访问类UserController.java

pom.xml文件


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.7.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
 
    
        UTF-8
        UTF-8
        1.8
    
 
    
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.0
        
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
 
    
        
            
                src/main/resources
                
                    ***.xml
                    **/*.yml
                
                true
            
 
        
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
 
    
 

创建application.yml文件
server:
  port: 8080
 
spring:
  datasource:
    username: root
    password: 082112
    #url中database为对应的数据库名称
    url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
 
mybatis:
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: com.example.demo.entity
 
#showSql
logging:
  level:
    com.example.demo.mapper: debug
在service包中新建实现类UserService.java
package com.example.demo.service;
 
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
 
    public User getUserInfo(int id) {
        return userMapper.getUserInfo(id);
    }
 
 
    public int deleteById(int id) {
        return userMapper.deleteById(id);
    }
 
    public int Update(User user) {
        return userMapper.update(user);
    }
 
    public User save(User user) {
        int save = userMapper.save(user);
        return user;
    }
 
    public List selectAll() {
        return userMapper.selectAll();
    }
}
在controller包中新建访问类UserController.java
package com.example.demo.controller;
 
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
 
@RestController
@RequestMapping("/testBoot")
public class UserController {
 
    @Autowired
    private UserService userService;
    @RequestMapping(value = "getUser/{id}", method = RequestMethod.GET)
    public String GetUser(@PathVariable int id) {
        return userService.getUserInfo(id).toString();
    }
 
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(int id) {
        int result = userService.deleteById(id);
        if (result >= 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(User user) {
        int result = userService.Update(user);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }
   
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public User insert(User user) {
        return userService.save(user);
    }
   
    @RequestMapping("/selectAll")
    @ResponseBody
    public List ListUser() {
        return userService.selectAll();
    }
 
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1004020.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号