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);
    }

}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐