Jhipster集成mybatis

1、Jhipster 是一个基于SpringBoot和Angular的快速Web应用和SpringCloud微服务的脚手架。
2、Jhipster内置JPA的ORM框架,但面对复杂的多表关联查询还是选择mybatis比较方便。
3、集成步骤如下:

  • 在pom.xml中添加相关依赖
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
  • 配置文件中指定对象包和mapper.xml的位置
mybatis:
    #指定*Mapper.xml中使用的对象的包的位置(同时可以指定别名)
    type-aliases-package: com.mumusoo.entity
    #指定*Mapper.xml的位置
    mapper-locations: classpath:mappers/*.xml
  • 利用mybatis-generator自动生成实体代码

  • 目录如下:
    这里写图片描述

  • UserInfoMapper.java 同时使用XML及注解方式。
    –selectByPrimaryKey() 使用xml配置
    –getAllUsers() 使用注解配置
import com.mumusoo.entity.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component(value = "userDao")
public interface UserInfoMapper {
    int deleteByPrimaryKey(Long id);

    int insert(UserInfo record);

    int insertSelective(UserInfo record);

    UserInfo selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(UserInfo record);

    int updateByPrimaryKey(UserInfo record);

    @Select("select * from jhi_user")
    public List<UserInfo> getAllUsers();
}
  • UserInfoServiceImpl.java 分别测试两种方式。
import com.mumusoo.dao.UserInfoMapper;
import com.mumusoo.entity.UserInfo;
import com.mumusoo.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userDao;

    @Override
    public UserInfo get(long id) {
        return userDao.selectByPrimaryKey(id);
    }

    @Override
    public List<UserInfo> getAll() {
        return userDao.getAllUsers();
    }
}
  • UserInfoController.java
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mumusoo.entity.UserInfo;
import com.mumusoo.service.UserInfoService;
import com.mumusoo.service.dto.UserDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("test")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("getAll")
    public String getAll() throws IOException{
        List<UserInfo> userInfos = userInfoService.getAll();
        List<UserDTO> userDtos=new ArrayList<>();
        for (UserInfo user :userInfos
            ) {
            UserDTO userDto=new UserDTO();
            BeanUtils.copyProperties(user,userDto);
            userDtos.add(userDto);
        }
        return new ObjectMapper().writeValueAsString(userDtos);
    }

    @RequestMapping("get")
    public String get() throws IOException {
        Long id=1L;
        UserInfo userInfo = userInfoService.get(id);
        UserDTO userDto=new UserDTO();
        BeanUtils.copyProperties(userInfo,userDto);
        return new ObjectMapper().writeValueAsString(userDto);
    }

}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值