mybatisPlus依赖
<dependencies>
<!-- mybatis plus start-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- mybatis plus end-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<!-- springboot整合mybatis(核心就这一个) -->
<!-- 注意顺序,这个一定要放在最下面 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
生成代码:
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class GenerateMybatisPlus {
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir("d:");
gc.setFileOverride(true);
gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
// gc.setAuthor("Walle");// 作者
// 自定义文件命名,注意 %s 会自动填充表实体属性!
// gc.setControllerName("%sAction");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sDao");
gc.setXmlName("%sMapper");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test_db");
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// strategy.setTablePrefix(new String[]{"itcast_"});// 此处可以修改为您的表前缀
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setInclude(new String[]{"spider_site"}); // 需要生成的表
strategy.setSuperServiceClass(null);
strategy.setSuperServiceImplClass(null);
strategy.setSuperMapperClass(null);
mpg.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.zhm");
// pc.setController("action");
pc.setService("service");
pc.setServiceImpl("serviceImpl");
pc.setMapper("dao");
pc.setEntity("entity");
pc.setXml("xml");
mpg.setPackageInfo(pc);
// 执行生成
mpg.execute();
}
}
dao层接口添加注解@Mapper
@Mapper
public interface SpiderSiteDao extends BaseMapper<SpiderSite> {
}
启动类添加注解@MapperScan("com.fcc.springboot2Hik.domain.dao")
@SpringBootApplication(scanBasePackages = {"com.fcc.springboot2Hik.springmvc.controller",
"com.fcc.springboot2Hik.domain"})
@MapperScan("com.fcc.springboot2Hik.domain.dao")
public class SpringMvcApp {
public static void main(String[] args){
SpringApplication.run(SpringMvcApp.class,args);
}
}
maven将java目录下的*Mapper.xml文件打包进去
<build>
<!--将java目录下的xml文件打包进去-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*Mapper.xml</include>
</includes>
<!-- 是否替换资源中的属性-->
<filtering>false</filtering>
</resource>
</resources>
</build>
application.yml中配置mybatis mapper的位置
#mybatis
mybatis-plus:
mapper-locations: classpath:com/fcc/springboot2Hik/domain/dao/*Mapper.xml
数据源配置
#mysql
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=utf8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
#连接池初始化,最小,最大
initialSize: 3
minIdle: 5
maxActive: 20
#配置获取连接等待超时的时间
maxWait: 50000
#配置间隔多久进行一次检测,检测需要关闭的空闲连接,单位毫秒
timeBetweenEvictionRunsMillis: 60000
#配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 30000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
#配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,slf4j
#通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#合并多个DruidDataSource的监控数据
#useGlobalDataSourceStat:true
#mybatis
mybatis-plus:
mapper-locations: classpath:com/fcc/springboot2Hik/domain/dao/*Mapper.xml
# 配置slq打印日志(只在开发环境用)
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* @description
* @auther feicongcong
* @date 2019/1/16
*/
@Slf4j
public class DataSourceConfig {
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.initialSize}")
private int initialSize;
@Value("${spring.datasource.minIdle}")
private int minIdle;
@Value("${spring.datasource.maxActive}")
private int maxActive;
@Value("${spring.datasource.maxWait}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.filters}")
private String filters;
@Value("{spring.datasource.connectionProperties}")
private String connectionProperties;
@Bean
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setDriverClassName(driverClassName);
datasource.setUrl(dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
/** configuration */
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
log.error("druid configuration initialization filter", e);
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
}
<select id="getPageByCondition" parameterType="map" resultMap="carEventOutDto">
select
t.C_ID,
t.C_ENTRANCE_NAME,
t.C_EVENT_TIME,
t.C_PIC_URL,
t.C_PLATE_NO
from
<include refid="tableNameSql"/>
where 1=1
<if test="plateNo != null and plateNo !=''">
and t.c_plate_no like CONCAT('%',#{plateNo},'%')
</if>
<if test="eventTimeStart != null">
and t.c_event_time <![CDATA[ >= ]]> #{eventTimeStart, jdbcType=TIMESTAMP}
</if>
<if test="eventTimeEnd != null">
and t.c_event_time <![CDATA[ <= ]]> #{eventTimeEnd, jdbcType=TIMESTAMP}
</if>
<if test="roadwayResourcesIdList != null">
and t.c_roadway_resources_id IN
<foreach collection="roadwayResourcesIdList" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
ORDER BY t.c_event_time DESC
</select>