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

SpringBoot笔记:SpringBoot集成MyBatis实战

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

SpringBoot笔记:SpringBoot集成MyBatis实战

文章目录
  • 1、前言
  • 2、代码实战
    • 2.1、pom.xml 配置
    • 2.2、yml 配置
    • 2.3、整体的代码层次结构
    • 2.4、Application 配置
    • 2.5、mysql 表结构
    • 2.6、Model
    • 2.7、Mapper
    • 2.8、Service
    • 2.9、Controller

1、前言

    因时间有限,简单说明一下 springboot 如何集成 mybatis的。详细的代码库地址:

https://gitee.com/leo825/springboot-learning-parents.git
2、代码实战 2.1、pom.xml 配置

本地开发使用 jdk8,mysql 版本 8.0.28



	
		org.example
		springboot-learning-parents
		1.0-SNAPSHOT
	

	4.0.0
	springboot-demo
	springboot-mybatis
	1.0-SNAPSHOT
	springboot-mybatis
	springboot 集成 mybatis
	jar

	https://gitee.com/leo825/springboot-learning-parents.git

	
		com.demo.SpringbootMybatisApplication
		UTF-8
		UTF-8
		1.8
		1.3.2
		8.0.28
	


	
		
			org.springframework.boot
			spring-boot-starter
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.projectlombok
			lombok
			true
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			${mybatis.version}
		

		
		
			mysql
			mysql-connector-java
			${mysql.version}
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

2.2、yml 配置

springboot 配置文件配置如下:

# springboot多环境配置
#端口,项目上下文
server:
  port: 8080
  servlet:
    context-path: /springboot-mybatis


spring:
  # mysql 数据库连接信息,本地使用 mysql 服务版本为:8.0.28
  datasource:
    username: root
    password: 6tojyh*A3eQ6
    url: jdbc:mysql://localhost:3306/local_test?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
    driver-class-name: com.mysql.cj.jdbc.Driver

# mybatis 配置
mybatis:
  type-aliases-package: com.demo.model
  mapper-locations: classpath:mapper
    void addUser(User user);

    
    void deleteUserById(Integer id);

    
    void updateUser(User user);

    
    List getUsersById(List ids);
}

mapper.xml







    
    
        
        
        
        
        
        
        
        
    


    
    
		select * from t_user
	
    
    
        
        
            select LAST_INSERT_ID()
        
        insert into t_user
            (id,username,pwd,create_time,update_time)
        values(#{id},#{userName},#{pwd},#{createTime},#{updateTime})
    

    
    
        update t_user
        
            
                username = #{userName},
            
            
                pwd = #{pwd},
            
            update_time = #{updateTime}
        
        where id = #{id}
    

    
    
        delete from t_user where id = #{id}
    

    
    

2.8、Service
package com.demo.service;

import com.demo.model.User;

import java.util.List;

public interface UserService {
    
    void addUser(User user);

    
    void deleteUserById(Integer id);

    
    void updateUser(User user);

    
    List getUsersById(List ids);
}

impl 实现类

package com.demo.service.impl;

import com.demo.mapper.UserMapper;
import com.demo.model.User;
import com.demo.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public void deleteUserById(Integer id) {
        userMapper.deleteUserById(id);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public List getUsersById(List ids) {
        return userMapper.getUsersById(ids);
    }
}
2.9、Controller
package com.demo.controller;

import com.demo.model.User;
import com.demo.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.rmi.server.InactiveGroupException;

import javax.annotation.Resource;
import java.util.*;

@RestController
public class UserController {

    @Resource
    UserService userService;

    
    @RequestMapping("/add")
    private String getUserById(String username,String password){
        User user = new User();
        user.setUserName(username);
        user.setPwd(password);
        user.setCreateTime(new Date());
        userService.addUser(user);
        return "success";
    }

    
    @RequestMapping("/delete")
    private String deleteUserById(String userId){
        Integer id = Integer.parseInt(userId);
        userService.deleteUserById(id);
        return "success";
    }


    
    @RequestMapping("/update")
    private String updateUser(String id, String username,String password){
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setUserName(username);
        user.setPwd(password);
        user.setUpdateTime(new Date());
        userService.updateUser(user);
        return "success";
    }


    
    @RequestMapping("/getUserList")
    private String getUsersById(String userId){
        String[] idsStr = userId.split(",");
        List ids = new ArrayList<>();
        for(String str : idsStr){
            ids.add(Integer.parseInt(str));
        }
        List users = userService.getUsersById(ids);
        return users.toString();
    }
}

通过 controller 可以进行新增、修改、查询、删除等操作了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1006030.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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