在前面学习的博文中,对于springboot整合mybatis+PageHelper都已经有了。现在是多加了通用Mapper和jersey的整合。既然要整合通用Mapper和jersey说明大家都知道他们干嘛用的了,在这就不多说了,直接贴jersey和通用Mapper配置相关的代码,mybatis+PageHelper的就补贴了,前面的博文都有了或者直接到github上完整例子代码。
例子项目结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.salt.api.reportmanage</groupId>
<artifactId>reportmanage</artifactId>
<packaging>jar</packaging>
<version>0.0.0-SNAPSHOT</version>
<name>reportmanage</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<!-- 依赖仓库 设置从aliyun仓库下载-->
<repositories>
<repository>
<id>alimaven</id>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<!-- 插件依赖仓库 -->
<pluginRepositories>
<pluginRepository>
<id>alimaven</id>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- REST -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
mybatis+pageHelper的配置之前的学习贴过来,这里就补贴了,贴下通用mapper的配置,很简单
MyBatisMapperScannerConfig.java
package com.salt.api.reportmanage.db;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
@Configuration
@AutoConfigureAfter(MybatisConfiguration.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.salt.api.reportmanage.dao");//每张表对应的XXMapper.java interface类型的Java文件
mapperScannerConfigurer.setMarkerInterface(IMysqlBaseMapper.class);
return mapperScannerConfigurer;
}
}
jersey的配置
写个需要向外提供方法的provider接口和类(不要接口,直接写类,注解写在类方法上也行)
IMonthReportProvider.java
package com.salt.api.reportmanage.provider;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
@Path("/monthreport")
public interface IMonthReportProvider {
@POST
@Path("/queryPage")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces({MediaType.APPLICATION_JSON+";charset=UTF-8"})
Response queryPage(@FormParam("areaCode") String areaCode,@FormParam("startYearMonth") String startYearMonth,@FormParam("endYearMonth") String endYearMonth,@FormParam("pageNo") String pageNo,@FormParam("pageSize") String pageSize,@FormParam("state") String state);
}
实现类
MonthReportProvider.java
/**
* Project Name:report-manage
* File Name:MonthReportProvider.java
* Package Name:com.salt.api.reportmanage
* Date:2017年6月21日下午3:11:38
*
*/
package com.salt.api.reportmanage.provider;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.salt.api.reportmanage.domain.MonthReport;
import com.salt.api.reportmanage.service.MonthReportService;
import com.salt.api.reportmanage.util.StringUtil;
import com.salt.api.reportmanage.vo.RestVo;
/**
* @author Jfei
* @version
* @see
*/
@Service
public class MonthReportProvider implements IMonthReportProvider{
private static Logger logger = LoggerFactory.getLogger(MonthReportProvider.class.getName());
@Autowired
private MonthReportService monthReportService;
public Response queryPage(String areaCode, String startYearMonth, String endYearMonth,
String pageNo, String pageSize,String state) {
RestVo vo = new RestVo();
try {
PageInfo<MonthReport> page = this.monthReportService.queryPage(areaCode,
StringUtil.isNotBlank(startYearMonth) ? Integer.valueOf(startYearMonth) : null,
StringUtil.isNotBlank(endYearMonth) ? Integer.valueOf(endYearMonth) : null,
Integer.valueOf(pageNo), Integer.valueOf(pageSize),
StringUtil.isNotBlank(state) ? Integer.valueOf(state) : null);
JSONObject r = new JSONObject();
r.put("pages", page.getPages());
r.put("count", page.getTotal());
r.put("dataList", page.getList());
vo.setSuccess(r);
} catch (Exception e) {
logger.error("分页查询,系统出错...",e);
vo.setFail(e.getMessage());
}
return Response.ok(vo).build();
}
}
写个jersey的配置
JerseyConfig.java
package com.springboot;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(RequestContextFilter.class);
packages("com.salt.api.reportmanage.provider");
}
}
启动类
JerseyApplication.java
package com.springboot;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan(basePackages = { "com.springboot" ,"com.salt.api"})
public class JerseyApplication {
public static void main(String[] args) {
SpringApplication.run(JerseyApplication.class, args);
System.out.println("启动成功");
}
/**
* 注册jerseyServlet,拦截/1.0/*所有url
* Date:2017年6月22日上午10:38:34
* @author Jfei
* @return
*/
@Bean
public ServletRegistrationBean jerseyServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/1.0/*");
registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
return registration;
}
}
以上就是例子项目中jersey的相关配置。 完整源码
如果把这架子迁移到其他项目进行使用,要注意一些注意事项。
1.com.salt.api.reportmanage.db下的所有类都需要复制迁移
2.MybatisConfiguration.java上有个@MapperScan("com.salt.api.reportmanage.dao"),需要根据实际请修改
3.JerseyConfig.java中的扫描注册所有jersey类的包路劲
4.JerseyApplication 添加jerseyServlet()方法,里面有个url,根据实际情况修改,比如/1.0/*,则实际请求时http://localhost/1.0/monthreport/xxx这样了
5.application.yml中的mybatis开头那几个参数,也要修改
6.jdk必须是1.8