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

springboot整合mybatis

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

springboot整合mybatis

1、创建一个springboot项目spring-mybatis,并在pom.xml中添加如下依赖

	
        
            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
            2.2.0
        

        
        
        
            com.alibaba
            druid
            1.2.6
        

        
        
            mysql
            mysql-connector-java
            5.1.47
        

        
        
        
            log4j
            log4j
            1.2.17
        


    

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

        
        
            
                src/main/java
                
                    ***.yml
                    ***.xml
                
            
        

    

2、在resources下创建application-db.yml用来配置数据源

spring:
  datasource:
    username: root
    password: 123456
    #?serverTimezone=UTC解决时区的报错
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECt 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

3、创建项目包
dao,pojo,service,controller
4、在pojo包中创建User实体类,我这里用的User,不一定需要和我的一样,可以随便测试

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {
    private Integer id;
    private String username;
    private String password;
}

5、在dao包中创建UserMapper接口,这也是根据自己的实体类来定,随便测试

@Mapper
@Repository
public interface UserMapper {
    int insertUser(User user);
    int deleteUser(int id);
    int updateUser(User user);
    List selectUser();
}

6、在dao包中创建UserMapper.xml,也可以不用创建,可以使用注解






    
    
        
        
        
    

    
    
        insert into t_user(id,name,pwd)values(#{id},#{username},#{password});
    

    
    
        delete * from t_user where id = #{id};
    

    
    
        update t_user set name = #{username},pwd = #{password}where id = #{id};
    

    
    



7、在application.properties中编写相关配置

#端口
server.port=8081

#添加application-db.yml
spring.profiles.active=db

#为实体类设置别名
mybatis.type-aliases-package=com.itlhc.pojo

#绑定mapper.xml
mybatis.mapper-locations=com/itlhc/dao/*.xml

8、在测试类下测试crud看看能不能用

package com.itlhc;

import com.itlhc.dao.UserMapper;
import com.itlhc.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class SpringMybatisApplicationTests {

    @Autowired
    UserMapper userMapper;

    @Test
    void contextLoads() {
        User user = new User();
        user.setId(2);
        user.setUsername("123456");
        user.setPassword("123456");

        int i = userMapper.insertUser(user);
        System.out.println(i+"添加成功");
    }

    @Test
    void contextLoads1() {
        int deleteUser = userMapper.deleteUser(2);
        System.out.println(deleteUser+"删除成功");
    }

    @Test
    void contextLoads2() {
        User user = new User();
        user.setId(2);
        user.setUsername("1234567");
        user.setPassword("1234567");
        int i = userMapper.updateUser(user);
        System.out.println(i+"更改成功");

    }

    @Test
    void contextLoads3() {
        List users = userMapper.selectUser();
        for (User user : users) {
            System.out.println(user);
        }
    }

}

9、在service包下创建Uservice接口

public interface UserService {

    int insertUser(User user);
    int deleteUser(int id);
    int updateUser(User user);
    List selectUser();
}

10、在service包下创建UserviceImpl实现类实现UserService接口

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public int deleteUser(int id) {
        return userMapper.deleteUser(id);
    }

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

    @Override
    public List selectUser() {
        return userMapper.selectUser();
    }
}

11、在controller包下创建UserController类,测试一下看看能不能在客户端查出数据来

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/selectUser")
    public List selectUser(){
        List users = userService.selectUser();
        return users;
    }
}

完工

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

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

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