2019独角兽企业重金招聘Python工程师标准>>>
新建spring-boot项目,相关依赖
<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 连接池 引入依赖
<!--引入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配置类
也可以在配置文件中直接配置@Configuration
public class DruidConfig {//关联yml文件中的配置项@ConfigurationProperties(prefix = "spring.datasource")@Beanpublic DataSource druid() {return DruidDataSourceBuilder.create().build();}//配置druid 的监控
// 1.配置管理后台的servlet@Beanpublic 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@Beanpublic 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;}}
配置文件中配置的相关属性
# application.yml文件采用yml文件格式配置spring:datasource:
# 数据源基本配置username: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.11.72:3306/boot08
# type: com.alibaba.druid.pool.DruidDataSource
# 数据源其他配置initialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙filters: stat,wall,log4jmaxPoolPreparedStatementPerConnectionSize: 20useGlobalDataSourceStat: trueconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis相关配置可以使用配置类或者配置文件
public class MybtisConfig {//开启驼峰public ConfigurationCustomizer configurationCustomizer(){return new ConfigurationCustomizer() {@Overridepublic void customize(Configuration configuration) {configuration.setMapUnderscoreToCamelCase(true);}};}
}
mapper.class文件
//@Mapper
public interface DepartmentMapper {@Select("select * from department")public List<Department> getList();}@MapperScan(value = "com.wgm")
@SpringBootApplication
public class BootmybatisApplication {
注意,使用@Mapper注解扫描当前接口 ,或者是使用@MapperScan注解扫描包 都会添加到容器中
这种方式,sql语句可以直接写在方法上
或者使用配置文件相关方式,即传统的配置mybatis-config.xml ,/mapper/*.xml
配置文件中需要修改
mybatis:config-location: classpath:mybatis/mybatis-config.xmlmapper-locations: classpath:mybatis/mapper/*.xml
文件目录