mybatis提供了ORM功能,相比于其余ORM框架,其须要编写更多的sql,也给了咱们编写特殊/复杂sql和进行sql优化的机会。java
Druid是阿里巴巴开发的号称为监控而生的数据库链接池,Druid是目前最好的数据库链接池。 在功能、性能、扩展性方面,都超过其余数据库链接池,同时加入了日志监控, 能够很好的监控DB池链接和SQL的执行状况。mysql
tk.mybatis是github上一个开源的组件,封装mybatis,提供通用Mapper封装了普通的增删改查和example操做,使咱们不用再每一个业务单独开发增删改查等的样板代码。git
<!-- jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--tk.mybatis 封装了mybatis--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.1.2</version> </dependency> <!--pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.11</version> </dependency>
spring: application: name: monocase-framework # 应用名称 jackson: date-format: yyyy-MM-dd HH:mm:ss # 日期格式 datasource: druid: # 链接池别名 url: jdbc:mysql://${MYSQL_HOST:192.168.1.200}:${MYSQL_PORT:3306}/zhya-monocase-framework?useUnicode=true&characterEncoding=utf8 username: root password: root@123 type: com.alibaba.druid.pool.DruidDataSource # 链接池类型 driver-class-name: com.mysql.jdbc.Driver poolPreparedStatements: true maxOpenPreparedStatements: 100 maxActive: 100 maxWait: 5000 mybatis: basepackage: com.zhya.mapper xmlLocation: classpath:mapper/**/*.xml mapper-locations: "classpath*:mapper/*.xml" # config-location: classpath:mybatis-config.xml configuration: map-underscore-to-camel-case: true # 下划线转驼峰 # 分页配置 pagehelper: helper-dialect: mysql reasonable: true support-methods-arguments: true params: count=countSql logging: level: # tk.mybatis: DEBUG com.zhya: DEBUG # 输出sql日志
package com.zhya.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; /** * druidl链接池配置 * * @author: zhangyang * @create: 2018/11/21 9:48 **/ @Configuration public class DataSourceConfig { @Primary @Bean(name = "myDataSource") @ConfigurationProperties("spring.datasource.druid") public DruidDataSource dataSource() { return new DruidDataSource(); } }
package com.zhya; import lombok.extern.slf4j.Slf4j; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * 启动类 * * @Author zhangyang * @Date 下午 8:23 2018/11/20 0020 **/ @Slf4j
// 开启注解驱动的事务管理功能 @EnableTransactionManagement(proxyTargetClass = true)
// 扫描mapper @MapperScan(basePackages = "com.zhya.mapper")
// 使用druid data source,排除默认的datasource @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class MonocaseFrameworkApplication { public static void main(String[] args) { SpringApplication.run(MonocaseFrameworkApplication.class, args); log.info("MonocaseFrameworkApplication started......"); } }
如上图:github
BaseEntity是基础实体,提供通用的字段如id,add_date等;spring
SysUser是普通业务实体,继承BaseEntity,扩展业务字段;sql
IBaseService使用继承自BaseEntity的泛型,来规定通用的增删改查接口;数据库
AbstractBaseService继承IBaseService接口,并使用Mapper根据Entity类型操做对应的表;mybatis
SysUserService实现ISysUserService接口,继承AbstractBaseService抽象类,得到通用数据库操做的功能;架构
BaseController经过实现了IBaseService的接口和Entity类型,调用业务逻辑。app