标签:ace 文件 manager 数据 data mapper ram def .data
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
dbcp2:
driver-class-name: com.mysql.cj.jdbc.Driver
password: ***
url: jdbc:mysql://127.0.0.1:3306/pzx?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
username: app
max-total: 1024
Config文件
@Component
@ConfigurationProperties("spring.datasource.dbcp2")
class MySqlConfig {
var driverClassName = "com.mysql.jdbc.Driver"
var username = ""
var password = ""
var url = ""
var maxTotal = 0;
var maxIdel = 0;
var maxWaitMillis = 0L
@Bean
fun dataSource(): BasicDataSource {
println("BasicDataSource inited: ${url}")
val dataSource = BasicDataSource()
dataSource.driverClassName = driverClassName
dataSource.url = url
dataSource.username = username
dataSource.password = password
dataSource.maxTotal = maxTotal
dataSource.maxIdle = maxIdel
dataSource.maxWaitMillis = maxWaitMillis
dataSource.setValidationQuery("SELECT 1")
dataSource.testOnBorrow = true
return dataSource
}
}
@Component
@AutoConfigureAfter(MySqlConfig::class)
class MyBatisSessionConfig {
@Bean
fun mapperScannerConfigurer(): MapperScannerConfigurer {
val mapperScannerConfigurer = MapperScannerConfigurer()
//获取之前注入的beanName为sqlSessionFactory的对象
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory")
//指定xml配置文件的路径
mapperScannerConfigurer.setBasePackage("pzx.db.mybatis.mapper")
return mapperScannerConfigurer
}
}
@Configuration
//加上这个注解,使得支持事务
@EnableTransactionManagement
class MyBatisConfig : TransactionManagementConfigurer {
@Autowired
private var dataSource: DataSource? = null
override fun annotationDrivenTransactionManager(): PlatformTransactionManager {
return DataSourceTransactionManager(dataSource!!)
}
@Bean(name = arrayOf("sqlSessionFactory"))
fun sqlSessionFactoryBean(): SqlSessionFactory? {
val bean = SqlSessionFactoryBean()
bean.setDataSource(dataSource)
try {
return bean.`object`
} catch (e: Exception) {
e.printStackTrace()
throw RuntimeException(e)
}
}
@Bean
fun sqlSessionTemplate(sqlSessionFactory: SqlSessionFactory): SqlSessionTemplate {
return SqlSessionTemplate(sqlSessionFactory)
}
}
Mapper文件
@Mapper
interface CityMapper {
@Select("select * from s_city where code = #{code}")
@Results(value = arrayOf(Result(column = "password", property = "password")))
fun findByCode(@Param("code") code: String): SysCity?
}
调用
@Autowired
lateinit var ds : CityMapper
@GetMapping("/testMySql")
fun testMySql(request: HttpServletRequest): String {
var e = ds.findByCode("110")
return request.UserId;
}
依次执行:
到这里, Configuration 出现了。
出现在Sql及CacheKey: 383339099:1114119213:pzx.db.mybatis.mapper.CityMapper.findByCode:0:2147483647:select * from s_city where code = ?:110:SqlSessionFactoryBean
分为以下部分: hashcode:checksum:各个部分。 前面的 hashcode:checksum 可以表示唯一了, 添加后面的部分, 是为了描述元数据。个人感觉后面部分可以简化为: 排序关联表(主键的唯一值) 的方式。唯一值仅在关联表是一个,且根据主键查询的情况。
可惜 MappedStatement.getCache 返回了空。没走缓存。
标签:ace 文件 manager 数据 data mapper ram def .data
原文地址:https://www.cnblogs.com/newsea/p/8997606.html