SpringBoot-基礎配置與資料庫訪問技術
阿新 • • 發佈:2018-12-21
本文的核心內容:SpringBoot的基礎配置、SpringBoot資料庫訪問技術。
SpringBoot的配置在resources目錄下,application.properties。
SpringBoot的配置可以分為兩種,一種為基礎配置如伺服器資訊、日誌等;另一種為整合第三方框架或工具的配置。
一:SpringBoot的基礎配置
①:伺服器配置
我們知道SpringBoot內建了Tomcat,同時還支援Jetty、Undertow作為Web伺服器。如果使用這些伺服器只需要引入相應的starter,,併除去Tomcat依賴。
#------配置伺服器配置Start----# #配置伺服器的埠 server.port=8080 #配置伺服器的請求上下文 預設為/ server.servlet.context-path=/myWeb #配置伺服器出錯後的處理路徑 預設/error server.error.path=/error #------配置伺服器配置End---#
Tomcat的相關配置
#------配置TomCat配置Start----# #開啟Tomcat訪問日誌 預設關閉 server.tomcat.accesslog.enabled=true #訪問日誌所在的目錄 server.tomcat.accesslog.directory=logs #允許Http請求快取到佇列的最大個數, 預設不限制 server.tomcat.accept-count=100 #允許的最大連線數 預設不限制 server.tomcat.max-connections=10000 #最大工作執行緒數 server.tomcat.max-threads=200 #------配置TomCat配置End----#
②:日誌配置
日誌級別:ERROR,WAR,INFO,DEBUG和TRACE
#-----日誌配置Start----#
#配置日誌級別
logging.level.root=info
#Controller包下的日誌級別為warn
logging.level.com.gzx.demo.controller=warn
#配置日誌輸出到my.log檔案中 預設不輸出到檔案
logging.file=my.log
#日誌檔案輸出路徑 d:/log
logging.path=d:/log
#------日誌配置End----#
二:SpringBoot資料庫訪問
訪問資料庫的方式有兩個流派,一個是以SQL為中心,例如MyBatis;另一個以Java Entity為中心,例如JPA。
本文涉及的資料庫訪問技術是流行的MyBatis。
①引入MyBatis、Mysql資料庫驅動和連線池的 Maven依賴
<!--MyBatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 資料來源包 druid dbcp2 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.2</version>
</dependency>
②配置SpringBoot的配置檔案
#------資料庫配置start----#
spring.datasource.url=jdbc:mysql://192.168.174.130:3306/article?characterEncoding=utf-8&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#配置連線池的型別
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#------資料庫配置End----#
#------MyBatis配置Start----#
##指向mapper的xml檔案位置
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
##指向實體類的位置
mybatis.type-aliases-package=com.gzx.demo.entity
#------MyBatis配置End----#
③建立實體類------資料庫對應的庫表
public class User implements Serializable {
private Integer userId;
private String userName;
//省略構造方法和get/set
}
④建立Dao介面------->新增@Mapper註解
@Mapper
public interface UserDao {
List<User> selectAllUser();
}
⑤新增UserDao介面的實現類 /resources/mapper/*Mapper.xml
<mapper namespace="com.gzx.demo.dao.UserDao">
<select id="selectAllUser" resultType="User">
select * from user
</select>
</mapper>
⑥建立UserServivce介面 、UserService的實現類 及對應的UserController 【與之前的配置一樣】
UserServivce介面
public interface UserService {
List<User> queryAllUser();
}
UserServivce介面實現類
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public List<User> queryAllUser() {
return userDao.selectAllUser();
}
}
UserController
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getAll")
public @ResponseBody List<User> getAllUser(){
List<User> users = userService.queryAllUser();
return users;
}
}
⑦ SpringBoot專案入口類新增兩個註解
@EnableTransactionManagement() //如果mybatis中service實現類中加入事務註解,需要此處新增該註解
@MapperScan("com.gzx.demo.dao.*") //掃描dao介面包
啟動專案測試