新建spring-boot項目,相關依賴css
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
使用Druid 鏈接池 引入依賴java
<!--引入druid--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
編寫Druid配置類mysql
也能夠在配置文件中直接配置 @Configuration public class DruidConfig { //關聯yml文件中的配置項 @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druid() { return DruidDataSourceBuilder.create().build(); } //配置druid 的監控 // 1.配置管理後臺的servlet @Bean public ServletRegistrationBean startViewServlet() { ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); Map<String, String> map = new HashMap<>(); map.put("loginUsername", "admin"); map.put("loginPassword", "123456"); map.put("allow", "");//默認容許全部訪問 // map.put("deny" , "192.168.11.72"); bean.setInitParameters(map); return bean; } //2.配置一個web監控的filter @Bean public FilterRegistrationBean webStatFilter() { FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); Map<String, String> map = new HashMap<>(); map.put("exclusions", "*.js,*.css,/druid/*"); bean.setInitParameters(map); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }
配置文件中配置的相關屬性web
# application.yml文件採用yml文件格式配置 spring: datasource: # 數據源基本配置 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.11.72:3306/boot08 # type: com.alibaba.druid.pool.DruidDataSource # 數據源其餘配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true # 配置監控統計攔截的filters,去掉後監控界面sql沒法統計,'wall'用於防火牆 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis相關配置可使用配置類或者配置文件spring
public class MybtisConfig { //開啓駝峯 public ConfigurationCustomizer configurationCustomizer(){ return new ConfigurationCustomizer() { @Override public void customize(Configuration configuration) { configuration.setMapUnderscoreToCamelCase(true); } }; } }
mapper.class文件sql
//@Mapper public interface DepartmentMapper { @Select("select * from department") public List<Department> getList(); } @MapperScan(value = "com.wgm") @SpringBootApplication public class BootmybatisApplication {
注意,使用@Mapper註解掃描當前接口 ,或者是使用@MapperScan註解掃描包 都會添加到容器中mybatis
這種方式,sql語句能夠直接寫在方法上app
或者使用配置文件相關方式,即傳統的配置mybatis-config.xml ,/mapper/*.xmlide
配置文件中須要修改spring-boot
mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml
文件目錄