Spring Boot Mybatis-Plusを使用します.

5544 ワード

Spring Boot Mybatis-Plusを使用します.
長所
Mybatis-Plus(MPと略称する)はMybatisの強化ツールであり、Mybatisの基礎の上では強化だけをして変えず、開発を簡略化し、効率を向上させるために生まれます.
統合
pom
	
            com.baomidou
            mybatis-plus-boot-starter
            2.3
        
        
            mysql
            mysql-connector-java
            runtime
        

        
            mysql
            mysql-connector-java
            8.0.20
        

   		
            com.alibaba
            druid
            1.1.6
        
lomokを入れて、beanを書きやすいです.
 	
            org.projectlombok
            lombok
            true
        
appication.properties
server.port=8080

#mysql
spring.datasource.url=jdbc:mysql://192.168.0.106:3306/db1?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#mybatis-plus
#mybatis-plus.mapper-locations=classpath:com/mht/springbootmybatisplus/mapper/xml/*.xml
mybatis-plus.type-aliases-package=com.bxc.eurekaprovide.Entity
mybatis-plus.configuration.map-underscore-to-camel-case: true
改ページの設定
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}
エンティティクラス
@Data
@TableName(value = "feedback")  //  
public class FeedBack implements Serializable {
    @TableId(value = "id",type = IdType.AUTO) //  id   
    private int id;
    @TableField(value = "text")    //  
    private String text;
    @TableField(value = "date")
    private Date date;
    @TableField(value = "comform")
    private String comform;
    @TableField(exist = false)  //      
    private String info;
}

Mapper
@Mapper
@Component
public interface FeedBackMapper extends BaseMapper {
    @Select("select * from feedback")
    List selectAll(Pagination page);
}

サービス
public interface FeedBackService {
    boolean AddDate(FeedBack feedBack);

    Page findAllInfo(Page page);

    List findAllInfoList(Page page);

    List findAllInfoByText(Page page, String text);

    List findAllInfoById(Page page,int id);

    List findAllInfoWhereId(Page page,int id1,int id2);

    boolean updateData(FeedBack back);

    boolean deleteData(FeedBack back);
}

Impl
@Service
public class FeedBackServiceImpl  implements FeedBackService {
    @Autowired
    FeedBackMapper feedBackMapper;

    @Override
    public boolean AddDate(FeedBack feedBack) {
        int count = feedBackMapper.insert(feedBack);

        System.out.println("    ");
        System.out.println(feedBack);
        if (count>0){
            return true;
        }
        return false;
    }

    @Override
    public Page findAllInfo(Page page) {
        page.setRecords(feedBackMapper.selectAll(page));
        return page;
    }

    @Override
    public List findAllInfoList(Page page) {
        List list = feedBackMapper.selectPage(page,null);
        return list;
    }

    @Override
    public List findAllInfoByText(Page page, String text) {
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("text",text);
        List list = feedBackMapper.selectPage(page,wrapper);
        return list;
    }

    @Override
    public List findAllInfoById(Page page, int id) {
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("id",id);
        List list = feedBackMapper.selectPage(page,wrapper);
        return list;
    }

    @Override
    public List findAllInfoWhereId(Page page, int id1, int id2) {
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.between("id",id1,id2);

        List list = feedBackMapper.selectPage(page,wrapper);
        return list;
    }

    @Override
    public boolean updateData(FeedBack back) {
        int count = feedBackMapper.updateById(back);
        if (count>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteData(FeedBack back) {
        int count = feedBackMapper.deleteById(back);
        if (count>0){
            return true;
        }
        return false;
    }
}