SpringBoot攻略六、集成mybatis实战

在【SpringBoot攻略二、Hello World实战】基础上追加…

1、pom.xml引入依赖

	<!-- spring boot mybatis -->
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
		<version>2.0.0</version>
	</dependency>
	
	<!-- mybatis 分页插件pagehelper -->
	<dependency>
	    <groupId>com.github.pagehelper</groupId>
	    <artifactId>pagehelper-spring-boot-starter</artifactId>
	    <version>1.2.5</version>
	</dependency>

	<!-- Druid database connection pool -->
	<dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>druid-spring-boot-starter</artifactId>
	    <version>1.1.9</version>
	</dependency>
	
	<!-- mysql -->
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.38</version>
	</dependency>
	
	<!-- fastjson -->
	<dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>fastjson</artifactId>
	    <version>1.2.58</version>
	</dependency>

2、application.properties配置

 	#多环境配置,指定加载的环境(开发、测试、生产),yml同理
 	#命名规则:application-{profile}.properties
    spring.profiles.active=dev
    
    
    #mybatis-------------------------------------------------------------------
    mybatis.configLocation=classpath:mybatis/mybatis-config.xml
    mybatis.mapperLocations=classpath*:/mybatis/**/*Mapper.xml
    #用类名表示此类全限定名的别名,不支持通配符*,可以修改源码支持*
    mybatis.typeAliasesPackage=com.javasgj.springboot.ssm.domain
    #mybatis分页插件pagehelper
    pagehelper.helperDialect=mysql
    pagehelper.reasonable=true
    pagehelper.supportMethodsArguments=true
    pagehelper.autoRuntimeDialect=true
    pagehelper.params=count=countSql

说明:这些配置项可以在mybatis-spring-boot-autoconfigure-2.0.0.jar中的MybatisAutoConfiguration、MybatisProperties找到,
记住:配置其他组件都是按照同样的规则去找配置项!

3、application-dev.properties配置

#开发环境


#server,内嵌tomcat配置信息
#端口,默认值8080
server.port=8080
#应用上下文,默认值/
server.servlet.contextPath=/springboot


#druid dataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/cs?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#配置初始化大小、最小、最大
spring.datasource.druid.initialSize=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=20
#配置从连接池获取连接等待超时的时间
spring.datasource.druid.maxWait=60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
#配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.druid.minEvictableIdleTimeMillis=300000
#检验连接是否有效的查询语句
spring.datasource.druid.validationQuery=select 1
#设置从连接池获取连接时是否检查连接有效性,true时,如果连接空闲时间超过minEvictableIdleTimeMillis进行检查,否则不检查;false时,不检查
spring.datasource.druid.testWhileIdle=true
#设置从连接池获取连接时是否检查连接有效性,true时,每次都检查;false时,不检查
spring.datasource.druid.testOnBorrow=false
#设置往连接池归还连接时是否检查连接有效性,true时,每次都检查;false时,不检查
spring.datasource.druid.testOnReturn=false
#配置监控统计拦截的filters
spring.datasource.druid.filters=stat,slf4j
#打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20

数据库脚本:
创建数据库

create database cs default character set utf8 collate utf8_general_ci;

创建公共附件表

create table t_c_file  (
	  id varchar(50) NOT NULL COMMENT '主键',
	  name varchar(200) NULL COMMENT '文件名称',
	  size int NULL COMMENT '文件大小',
	  path varchar(200) NULL COMMENT '文件路径',
	  type varchar(50) NULL COMMENT '文件类型',
	  yw_id varchar(50) NULL COMMENT '业务id',
	  cjr_id varchar(50) NULL COMMENT '创建人id',
	  cjr varchar(50) NULL COMMENT '创建人',
	  cj_sj datetime NULL COMMENT '创建时间',
	  gxr_id varchar(50) NULL COMMENT '更新人id',
	  gxr varchar(50) NULL COMMENT '更新人',
	  gx_sj datetime NULL COMMENT '更新时间',
	  PRIMARY KEY (id)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8;

4、mybatis-config.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>  
        <!-- 全局映射器启用缓存 -->  
        <setting name="cacheEnabled" value="true"/>  
  		
        <!-- 查询时,关闭关联对象即时加载以提高性能 -->  
        <setting name="lazyLoadingEnabled" value="false"/>  
  
        <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->  
        <setting name="multipleResultSetsEnabled" value="true"/>  
  
        <!-- 允许使用列标签代替列名 -->  
        <setting name="useColumnLabel" value="true"/>  
  
        <!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->  
        <setting name="useGeneratedKeys" value="true"/>  
  
        <!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL -->  
        <setting name="autoMappingBehavior" value="PARTIAL"/>  
  
        <!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE -->  
        <!-- <setting name="defaultExecutorType" value="BATCH" /> -->  
  
        <!-- 数据库超过25000秒仍未响应则超时 -->  
        <!-- <setting name="defaultStatementTimeout" value="25000" /> -->  
  
        <!-- Allows using RowBounds on nested statements -->  
        <setting name="safeRowBoundsEnabled" value="false"/>  
  
        <!-- 驼峰命名法映射字段和类型属性. -->  
        <setting name="mapUnderscoreToCamelCase" value="true"/>  
  
        <!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT   
            local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. -->  
        <setting name="localCacheScope" value="SESSION"/>  
  
        <!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values   
            like NULL, VARCHAR or OTHER. -->  
        <setting name="jdbcTypeForNull" value="VARCHAR"/>  
  
        <!-- Specifies which Object's methods trigger a lazy load -->  
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>  
  
        <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值