Spring+Mybatis+Druid
创建项目
选择Spring Initializr创建项目,选择jdk版本和默认的Service URL
填写项目的Group,Artifact信息
选择Web中的Spring Web和Sql中的Spring Data JDBC,Mybatis,MySQL四个依赖
然后填写项目信息点击Finish
导入依赖
添加Druid依赖
1 2 3 4 5
| <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.23</version> </dependency>
|
修改配置文件
修改datasource.properties文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <!-- mysql8.0以上版本 --> spring.datasource.url=jdbc:mysql://localhost:3306/mysql?useUnicode=ture\ &characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize: 5 spring.datasource.minIdle: 5 spring.datasource.maxActive: 20 spring.datasource.maxWait: 60000 spring.datasource.timeBetweenEvictionRunsMillis: 60000 spring.datasource.minEvictableIdleTimeMillis: 300000 spring.datasource.validationQuery: SELECT 1 FROM DUAL spring.datasource.testWhileIdle: true spring.datasource.testOnBorrow: false spring.datasource.testOnReturn: false spring.datasource.poolPreparedStatements: true
spring.datasource.filters: stat,wall,slf4j spring.datasource.maxPoolPreparedStatementPerConnectionSize: 20 spring.datasource.useGlobalDataSourceStat: true spring.datasource.connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis.mapper-locations=classpath:mybatis/*/*.xml mybatis.type-aliases-package=com/sx/sc/mapper/* mybatis.configuration.map-underscore-to-camel-case=true mybatis.configuration.lazy-loading-enabled=true
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
创建conifg文件
创建config包,创建DruidConfig文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druidDataSource() { return new DruidDataSource(); }
@Bean public ServletRegistrationBean<?> statViewServletDemo() { ServletRegistrationBean<StatViewServlet> srb = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); srb.addInitParameter("loginUsername", "admin"); srb.addInitParameter("loginPassword", "123456"); srb.addInitParameter("resetEnable", "false"); return srb; }
@Bean public FilterRegistrationBean statFilterDemo() { FilterRegistrationBean frb = new FilterRegistrationBean(new WebStatFilter()); frb.addUrlPatterns("/*"); frb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,"); return frb; } }
|
启动Application进入localhost:8080/druid
就可以进入druid管理页面,帐户密码就是DruidConfig内的数据
开发
然后按照ssm开发步骤创建controller,service,mapper.java,mapper.xml文件即可完成对数据库的访问
注意
controll
注解@RestController
(返回json格式)
service
注解@Service
mapper.java
注解@Repository,@Mapper
@Repository
使@Aotuwired不报错
@Mapper
使Spring能扫描到mapper
Application
@SpringBootApplication
@SpringBootApplication
相当于@Configuration,@EnableAutoConfiguration和@ComponentScan
并具有他们的默认属性值
@ComponentScan("*.*")
定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中
一般情况下不需要这个注解,因为一般情况下Application启动时会加载Application父包下的所有子包及其文件。
但在模块打包为jar包,不能扫描第三方jar包时,需要加上 @ComponentScan()
使Spring自动装配
@Configuration
设为配置类
@EnableAsync
Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程。使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor.在开发中实现异步任务,我们可以在配置类中添加@EnableAsync开始对异步任务的支持,并在相应的方法中使用@Async注解来声明一个异步任务。
@EnableTransactionManagement
事务管理
@EnableAutoConfiguration
开启自动配置
@EnableSwagger2
启动swagger注解
@ServletComponentScan
在Application上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。
@EnableAspectJAutoProxy(exposeProxy = true)
开启AOP
SpringCloud