New spring boot project, dependent
<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>
Using Druid connection pool to introduce dependency
<!--Introduce 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>
Writing the Druid configuration class
You can also configure it directly in the configuration file @Configuration public class DruidConfig { //Associate configuration items in the yml file @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druid() { return DruidDataSourceBuilder.create().build(); } //Configure druid monitoring // 1. Configure the servlet in the background of management @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", "");//Allow all access by default // map.put("deny" , "192.168.11.72"); bean.setInitParameters(map); return bean; } //2. Configure a filter for web Monitoring @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; } }
Related properties configured in the configuration file
# The application.yml file is configured in the format of YML file spring: datasource: # Basic configuration of data source 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 # Data source other configuration 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 # Configure the filters intercepted by monitoring statistics. After the filters are removed, the monitoring interface sql cannot be counted. The 'wall' is used for the firewall filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
Configuration class or configuration file can be used for mybatis related configuration
public class MybtisConfig { //Open hump public ConfigurationCustomizer configurationCustomizer(){ return new ConfigurationCustomizer() { @Override public void customize(Configuration configuration) { configuration.setMapUnderscoreToCamelCase(true); } }; } }
mapper.class file
//@Mapper public interface DepartmentMapper { @Select("select * from department") public List<Department> getList(); } @MapperScan(value = "com.wgm") @SpringBootApplication public class BootmybatisApplication {
Note that using the @ Mapper annotation to scan the current interface or using the @ MapperScan annotation scan package will be added to the container
In this way, sql statements can be written directly on Methods
Or use the configuration file related mode, i.e. traditional configuration of mybatis-config.xml, / mapper / *. XML
Configuration file needs to be modified
mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml
File directory