application.propertieshtml
application.ymljava
推薦第二種,遞進關係明顯,有層次感,配置寫得少mysql
mybatis的依賴,官方地址 https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starterweb
添加mybatis的依賴 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
修改application.yml的文件spring
server:
port: 8086
spring:
datasource:
diver-class-name: com.test
url: jdbc:mysql://localhost:3306/mysql
username: root
password: root
mybatis:
type-aliases-package: com.test
mapper-locations: classpath:com/test/*.xml
建立實體類sql
建立service接口和實現類數據庫
@Component
public class Dept{ private Long deptno; private String dname; private String loc; // get set 方法 }
建立Mapper接口mybatis
public interface DeptService{ List<Dept> selectAll(); }
建立Mapper映射文件app
public interface DeptService{ List<Dept> selectAll(); } @Service public class DeptServiceImpl implements DeptService{ @Autowired private DeptMapper deptMapper; public void setDeptMapper(DeptMapper deptMapper){ this.deptMapper = deptMapper; } // 查詢功能暫時不須要 @ransactional public List<Dept> selectAll(){ return deptMapper.selectAll(); } }
建立Controller處理器
@RestController public class DeptController{ @Autowired private DeptService public void setDeptService(DeptService deptService){ this.deptService = deptService; } @RequeestMapping("/show") public List<Dept> Show(){ return deptService.selectAll(); } }
添加掃描註解
@SpringBootApplication @MapperScan("com.test.mapper") public class LianmaiApplication { public static void main(String[] args) { SpringApplication.run(LianmaiApplication.class, args); } }
添加依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependenyc>
修改application.yml文件
# 鏈接池配置信息
# 初始化大小: 最大, 最小
druid:
initial-size: 5
min-idle: 5
max-active: 20
# 配置獲取鏈接等待超時的時間
max-wait: 60000
# 配置間隔多久才進行一次檢測, 檢測須要關閉的空閒鏈接, 單位是ms
time-between-eviction-runs-millis: 60000
# 配置一個鏈接在池子中最小生存的時間, 單位是ms
min-evictable-idle-time-millis: 3000000
validation-query: select 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
# 打開PSCache, 而且指定每一個鏈接上PSCache的大小
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
filter: satat,wall,slf4j
connection-properties: druid.stat.mergesql\=true;druid.stat.slowsqlmillis\=5000
# 配置DruidStatFilter
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
# 配置DruidStatViewServlet
stat-view-servlet:
url-pattern: "/druid/*"
# IP白名單
allow: 127.0.0.1,192.168.163.1
# IP黑名單(同時存在於白名單時, deny 優先於 allow)
deny: 192.168.1.188
reset-enable: false
# 登陸名
login-username: admin
# 登陸密碼
login-password: 123456
mybatis:
mapper-locations: classpath:com/test/*.xml
運行期間: 若是拋出錯誤,java.lang.NoClassDefFoundErrror: org/springframework/boot/bind/RelaxedPropertyResolver
解決方案,改成使用繼承的方式實現SpringBoot啓動器的依賴,再也不使用依賴的方式
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent>
druid的登錄頁面 http://localhost:8080/druid
logback是log4j的改良版本,取代緣由:實現更快,更充分的測試,更充分的文檔
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!--設置變量LOG_HOME,用於指定log日誌文件存放的目錄--> <property name="LOG_NAME" value="${catalina.base}/logs/"> <!--控制檯輸出--> <appender name="Stdout" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern> </layout> </appender> <appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBaseRollingPolicy"> <fileNamePattern>${LOG_HOME}/server.%d{yyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <patterm>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern> </layout> <triggeingPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> </property> <root level="info"> <appender-ref ref="Stdout"/> <appender-ref ref="RollingFile"/> </root> <logger name="com.test.mapper" level="debug"></logger> <!-- 日誌異步到數據庫 --> <appender name="DB" class="ch.qos.logback.classic.db.DBAppender"> <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource"> <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource"> <driverClasss>com.mysql.jdbc.Driver</driverClass> <url>jdbc:mysql://127.0.0.1:3306/test</url> <user>root</user> <password>root</password> </dataSource> </connectionSource> </appender> </configuration>
SpringBoot整合thymeleaf
thymelea的效率高於jsp
引入依賴,編寫模板