mybatis 配置
经过多次摸索探寻出几种实现方式
maven 配置
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
第一种在 yml 文件中配置
这种的配置方式用于普通数据连接,即没有使用c3p0等数据源进行连接数据库,主要是因为c3p0需要一些详细的参数,仅仅通过spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource这种无法指定其他的详细参数,当然如果只是简单配置一下,通过type也可以指定c3p0
数据库配置
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Drive
mybatis 配置
mybatis:
type-aliases-package: com.jzyl.entity
mapper-locations: classpath*:Mapperxml/*.xml
这样配置完成后还缺少一个注解,在启动的类上面
@SpringBootApplication
//扫描mapper接口
@MapperScan("com.demo.mapper")
public class demoApplication {
public static void main(String[] args) {
SpringApplication.run(demoApplication .class, args);
}
}
这样第一种方式以及配置完成
第二种配和c3p0进行配置
一般的项目肯定得采用这种的方式进行配置
maven 配置
<!-- c3p0 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
首先配置 c3p0,在.yml文件中添加c3p0的参数
c3p0:
driverClass: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
user: root
password: root
minPoolSize: 5
maxPoolSize: 500
initialPoolSize: 10
maxIdleTime: 60
acquireIncrement: 5
maxStatements: 0
idleConnectionTestPeriod: 60
acquireRetryAttempts: 30
breakAfterAcquireFailure: false
testConnectionOnCheckout: false
通过spring Bean的方式配置 c3p0 并且配置详细参数
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
/**
* c3p0 配置
*/
@Configuration
public class DataSourceConfiguration {
/**
* @Primary 自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
* @Qualifier 注解的用处:当一个接口有多个实现的时候,为了指名具体调用哪个类的实现
* @Bean 配置Bean
* @ConfigurationProperties
*/
@Bean(name = "dataSource")
@Qualifier(value = "dataSource")
@Primary
@ConfigurationProperties(prefix = "c3p0")
public DataSource dataSource()
{
return DataSourceBuilder.create().type(ComboPooledDataSource.class).build();
}
}
配置完c3p0后即配置 mybatis,也是通过Spring Bean 的方式进行配置
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
/**
* mybatis 配置
*/
@Configuration
@MapperScan(basePackages = {"com.demo.mapper"})
public class MybatisConfig {
private final DataSource dataSource;
@Autowired
public MybatisConfig(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactoryBean sqlSessionFactory(ApplicationContext applicationContext) throws IOException {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage("com.demo.entity");
Resource[] resources = new PathMatchingResourcePatternResolver()
.getResources("classpath*:Mapperxml/*Mapper.xml");
sessionFactory.setMapperLocations(resources);
return sessionFactory;
}
}
这样及配置完成
Activiti 配置
maven 配置 版本采用统一配置为5.22.0
<properties>
<activiti.version>5.22.0</activiti.version>
</properties>
<!--acitivit工作流 strat-->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-layout</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-common-rest</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-crystalball</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-diagram-rest</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-explorer</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-image-generator</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-json-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-modeler</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-process-validation</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-simple-workflow</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>${activiti.version}</version>
</dependency>
<!--acitivit工作流 end-->
Activiti Bean 配置
import org.activiti.engine.*;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
/**
* Activiti 配置
*/
@Configuration
public class ActivitiConfiguration {
private final DataSource dataSource;
private final PlatformTransactionManager platformTransactionManager;
@Autowired
public ActivitiConfiguration(DataSource dataSource, PlatformTransactionManager platformTransactionManager) {
this.dataSource = dataSource;
this.platformTransactionManager = platformTransactionManager;
}
@Bean
public SpringProcessEngineConfiguration springProcessEngineConfiguration() {
SpringProcessEngineConfiguration spec = new SpringProcessEngineConfiguration();
spec.setDataSource(dataSource);
spec.setTransactionManager(platformTransactionManager);
spec.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
spec.setActivityFontName("宋体");
spec.setLabelFontName("宋体");
return spec;
}
@Bean
public ProcessEngineFactoryBean processEngine() {
ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
processEngineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration());
return processEngineFactoryBean;
}
@Bean
public RepositoryService repositoryService() throws Exception{
return processEngine().getObject().getRepositoryService();
}
@Bean
public RuntimeService runtimeService() throws Exception{
return processEngine().getObject().getRuntimeService();
}
@Bean
public TaskService taskService() throws Exception{
return processEngine().getObject().getTaskService();
}
@Bean
public ManagementService managementService() throws Exception{
return processEngine().getObject().getManagementService();
}
@Bean
public FormService formService() throws Exception{
return processEngine().getObject().getFormService();
}
@Bean
public HistoryService historyService() throws Exception{
return processEngine().getObject().getHistoryService();
}
}
将以上配置完成后,activiti 即配置完毕,可以使用了,但是还会有一些其他的小问题
这些问题这里一并解决了,可以参考一下
首先是如果成功启动后可能会发现所有的接口都会跳转到一个登录界面
这个是因为:
- SpringBoot会根据引入的Jar包而自动配置相应的功能。
- ActivitiModeler中引用了Spring Security的Jar。(是一个安全或者说权限的框架)
springboot发现项目的Lib包中含有spring security的Jar包,启动时自动将安全认证配置到了项目上。那么访问项目时,就需要输入用户名和密码才能访问项目
那么这也有好几种方式
首先第一种 在 pom 文件中进行排除
这里的话因为每个人采用的框架都有所不同,所以依赖也可能不尽相同,有可能有排除不干净的情况
这里教给大家一个好办法,首先你的开发工具得是IDEA,如果不是,则直接跳过这个方法,看下一个吧。
首先点开maven 工具栏
然后点击这个按钮,这时会打开一个思维导图,内容则是项目框架的所有依赖信息
按住CTRL键可以进行放大缩小,我们要找的就是这几个依赖,将其排除掉就可以了。
可以从图中看出,主要的依赖来源是 activiti-modeler 和 activiti-diagram-rest 这两个依赖下的
双击其中的一个
从这里可以看出来 activiti-diagram-rest 是依赖这三个jar的。
将上述的三个依赖排除
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-diagram-rest</artifactId>
<version>${activiti.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</exclusion>
</exclusions>
</dependency>
这个张思维导图是实时更新的,可以看到 activiti-diagram-rest 的 org.springframework.security 依赖已经排除干净了。
接下来同样的方式将 activiti-modeler 的 org.springframework.security 的依赖也排除完
图我就不上了,直接上代码
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-modeler</artifactId>
<version>${activiti.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</exclusion>
</exclusions>
</dependency>
再次查看思维导图,如果图上找不到 org.springframework.security 则证明该依赖已经排除干净了。
这时候启动去访问项目就不会有登录验证了
第二种方法 在启动类上面添加排除依赖
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
/**
* 排除依赖,安全框架,会让人进行登录验证
*/
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
这样也可以将依赖排除掉
第三种方式 找到账号和密码进行登录
在控制台上会打印登录的密码,类似于这样:
Using generated security password: 4fd8bab1-17f1-4d26-b04e-a2a2a753df48,
用户名默认为user。输入就可以登录了。
或者我们可以在代码中去配置。
新建一个类SecurityConfig 继承WebSecurityConfigurerAdapter类,
然后重写父类中的configure(HttpSecurity http) 方法。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().permitAll().and().logout().permitAll();//配置不需要登录验证
}
}
这样也可以
另外 如果项目中采用的如果是log4j 的话,也会导致警告的产生,这个警告在开发中没有影响,但是如果是以jar包的形式发布的话就会启动不起来
原因还是jar冲突,排除slf4j-log4j12依赖
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-explorer</artifactId>
<version>${activiti.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
添加 slf4j-nop
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.25</version>
</dependency>
FastJson 配置(最好别用,最近爆出的fastJson的BUG挺严重的,请自行百度)
Maven 配置
<!--fastJson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
Spring Bean 配置
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class FastJsonConfiguration implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
/*
* 先移除MappingJackson2HttpMessageConverter转换器,再加入fastjson转换器
* 为什么要这么做呢:
* 第一种办法:先把JackSon的消息转换器删除.
* 备注: (1)源码分析可知,返回json的过程为:
* Controller调用结束后返回一个数据对象,for循环遍历conventers,找到支持application/json的HttpMessageConverter,然后将返回的数据序列化成json。
具体参考org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor的writeWithMessageConverters方法
(2)由于是list结构,我们添加的fastjson在最后。因此必须要将jackson的转换器删除,不然会先匹配上jackson,导致没使用fastjson
还有一种办法: 添加的时候index设置为0就好了,converters.add(0, converter);
*
*/
converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//自定义配置...
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteMapNullValue
,SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullNumberAsZero);
fastJsonHttpMessageConverter.setFastJsonConfig(config);
//解决返回的json数据中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
//将转换器添加到转换列表
converters.add(fastJsonHttpMessageConverter);
}
}
这样就配置完成。