Druid的數據源配置:https://www.cnblogs.com/KuroNJQ/p/11171263.html
1.導入依賴
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency>
2.我有一個application-dev.yml和一個applicaion.yml
分別是:
spring:
datasource:
url: jdbc:mysql://localhost:3306/wrist?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password:
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
#初始化大小
initialSize: 5
#最小值
minIdle: 5
#最大值
maxActive: 20
#最大等待時間,配置獲取連接等待超時,時間單位都是毫秒ms
maxWait: 60000
#配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接
timeBetweenEvictionRunsMillis: 60000
#配置一個連接在池中最小生存的時間
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置監控統計攔截的filters,去掉后監控界面sql無法統計,
#'wall'用於防火牆,SpringBoot中沒有log4j,我改成了log4j2
filters: stat,wall,log4j2
#最大PSCache連接
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# 配置StatFilter
web-stat-filter:
#默認為false,設置為true啟動
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
#配置StatViewServlet
stat-view-servlet:
url-pattern: "/druid/*"
#允許那些ip
allow: 127.0.0.1
login-username: admin
login-password: 123456
#禁止那些ip
deny: 192.168.1.102
#是否可以重置
reset-enable: true
#啟用
enabled: true
mybatis:
# model的包
type-aliases-package: ssm.app.model
configuration:
# 開啟駝峰uName自動映射到u_name
map-underscore-to-camel-case: true
# 打印sql
logging:
level:
ssm.app.mapper : debug
spring:
profiles:
active: dev
3.mybatis已經可以用了,下面是mapper接口
@Mapper public interface CountryMapper { @Select("select * from Country") public List<Country> selectAll(); @Select("select * from Country where id = #{id}") public Country selectCountryById(long id); }
service:
@Service public class CountryServiceImpl implements CountryService { @Autowired private CountryMapper countryMapper; @Override public List<Country> getCountriesFromDb() { return countryMapper.selectAll(); } @Override public Country getCountryFromDbById(long id) { return countryMapper.selectCountryById(id); } }
controller:
@RestController public class CountryController extends BaseController { private final CountryService countryServiceImpl; @Autowired public BaseController(CountryService countryServiceImpl) { this.countryServiceImpl = countryServiceImpl; } @RequestMapping("/getAllCountries") public List<Country> getAllCountries() { return countryServiceImpl.getCountriesFromDb(); } }
ok!還有就是記得注意yml的層次關系,別寫錯了,QAQ!