Springboot集成mybatis-puls

一、项目目录结构

 

 

 

Springboot集成mybatis-puls_第1张图片

 

 

 

二、系统的jar包依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.7.RELEASE
         
    
    com.gpdi
    springboot-mabatisplus-demo
    0.0.1-SNAPSHOT
    springboot-mabatisplus-demo
    Demo project for Spring Boot

    
        1.8
    

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

        
            mysql
            mysql-connector-java
            runtime
        


        
            com.baomidou
            mybatis-plus-boot-starter
            3.1.2
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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


二、配置文件采用properties文

 

#端口设置
server.port=8081

#server.servlet.context-path = /dcjk

#durid配置(http://localhost:8087/dcjk/druid/index.html)
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.druid.url = jdbc:mysql://localhost:3306/foshan?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.druid.username=root
spring.datasource.druid.password=root

# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.druid.initialSize=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.druid.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
#spring.datasource.druid.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
spring.datasource.druid.useGlobalDataSourceStat=true


#mybatis设置

##配置myBatis映射
mybatis-plus.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis-plus.typeAliasesPackage=com.gpdi.entity

# 配置mybatis-plus sql操作打印日志
#mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#配置mybatis驼峰
mybatis-plus.configuration.map-underscore-to-camel-case: true















 

四、service层

             

package com.gpdi.service;

import com.gpdi.entity.User;
import java.util.List;
public interface UserService {

    public int addUser(User user);
    /**
     * @description:增加一条用户信息
     */

    /**
     * @description:删除一条用户信息
     */
    public int deleteUserById(int id);

    /**
     * @descpription:通过id查询用户信息
     */
    public User getUserById(int id);

    /**
     * @description:增加一条用户信息
     */

    public int updateUser(User user);


    /**
     * @descrpition:分页查询
     */
    public List getUserList(String place);

    /**
     *
     * @description:通过sql查询用户信息
     */
    public List getUserListBySql(String sql);


}

五、impl层

      

package com.gpdi.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpdi.mapper.UserMapper;
import com.gpdi.entity.User;
import com.gpdi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserImpl implements UserService {
    @Override
    public User getUserById(int id) {
        return userMapper.selectById(id);
    }

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

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

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

    @Override
    public List getUserList(String place) {
        QueryWrapperwrapper=new QueryWrapper<>();
        wrapper.eq("place",place);
        return userMapper.selectList(wrapper);
    }

    public List getUserListBySql(String sql) {

        return this.userMapper.getUserListBySql(sql);

    }


    @Autowired
    UserMapper userMapper;


}

六、Mapper/Dao(操作数据库的),继承了 BaseMapper,提供了很多直接操作数据库表的api(增删查改)

package com.gpdi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gpdi.entity.User;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface UserMapper extends BaseMapper {
    
    public List getUserListBySql(String sql);
}

七、Controller层

         

package com.gpdi.controller;

import com.gpdi.entity.User;
import com.gpdi.service.UserService;
import com.gpdi.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : whs
 * @description:用户信息管理
 * @date 2019-08-18
 */

@RestController
public class UserController {

    /**
     * @descrition: 根据用户id查询用户信息
     */
    @RequestMapping(value = "getUserById")
    public ResponseData getUserById(int id) {
        return new ResponseData(200, "请求成功", userService.getUserById(id));
    }

    /**
     * @descrition: 增加用户信息
     */
    @RequestMapping(value = "addUser")
    public ResponseData addUser(User user) {
        return new ResponseData(200, "添加用户成功", userService.addUser(user));

    }

    /**
     * @descrition: 根据用户id修改用户信息
     */
    @RequestMapping(value = "updateUser")
    public ResponseData updateUser(User user) {
        return new ResponseData(200, "修改用户成功", userService.updateUser(user));

    }

    /**
     * @descrition: 根据用户id删除用户信息
     */
    @RequestMapping(value = "delUserById")
    public ResponseData updateUser(int id) {
        return new ResponseData(200, "删除用户成功", userService.deleteUserById(id));
    }

    /**
     * @descrition: 根据用户区域查询用户信息
     */
    @RequestMapping(value = "getUserList")
    public ResponseData getUserList(String place) {
        return new ResponseData(200, "查询用户成功", userService.getUserList(place));
    }


    @Autowired
    UserService userService;
}

八、实体类(POJO/VO/ENTITY)

package com.gpdi.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
/**
 * @description : 这里如果不加TableName,默认查询当前类User的对应user表
 *
 */
@TableName("sys_user")
public class User implements Serializable {
    private int id;
    private String name;
    private int state;
    private String username;
    private String place;
    private String tel;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }
}

九、配置MyBatis xml文件




   
    


十、配置Mybatis文件




    

    

 

你可能感兴趣的:(spring-boot)