13 Spring Boot:html
01.spring boot 介紹&&02.spring boot 入門java
04.spring boot 配置文件mysql
SpringBoot基礎 spring
MySQL數據庫版本: 8.0.17sql
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
一、添加Mybatis的起步依賴 數據庫
二、添加數據庫驅動座標
apache
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--SpingBoot集成junit測試的起步依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--mybatis起步依賴--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <!-- MySQL鏈接驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
三、添加數據庫鏈接信息
在 application.properties 中添加數據量的鏈接信息 springboot
project Encodingmybatis
爲了不.properties文件中文亂碼,請按參考資料配置編碼格式爲UTF-8
#數據庫鏈接信息 spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root
在itheima數據庫中建立user表
使用Navicat圖形化界面鏈接軟件
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) DEFAULT NULL, `password` varchar(50) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
插入測試數據
-- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '張三'); INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');
四、 在domain包:建立實體類
package com.itheima.domain; public class User { private Long id; private String username; private String password; private String name; @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", name='" + name + '\'' + '}'; } //此處省略getter和setter方法
五、 編寫DAO層的接口類
IUserDao
package com.itheima.mapper; import com.itheima.domain.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface IUserDao { public List<User> queryUserList(); }
六、配置Mapper映射文件
在src/main/resources/com/itheima/mapper路徑下加入UserMapper.xml配置文件
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.itheima.mapper.IUserDao"> <select id="queryUserList" resultType="user"> select * from user </select> </mapper>
七、在application.properties中添加mybatis的信息
#配置mybatis的信息 #spring集成Mybatis環境 #pojo別名掃描包 mybatis.type-aliases-package=com.itheima.domain #加載Mybatis映射文件 mybatis.mapper-locations=classpath:com/itheima/mapper/*Mapper.xml
經過測試證實:spring boot 集成mybatis框架【DAO層接口+XML配置文件】, 在接口的public的關鍵字上一行要加註解 @Mapper
接口的文件名不必定必須和XML映射配置文件名一致,由於spring boot的 application*.properties裏面有掃描 *Mapper.xml文件包的配置信息。
記錄一個springboot 中集成 mybatis 的完整使用過程
5.2 SpringBoot整合Junit
一、添加Junit的起步依賴
<!--springboot測試的起步依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
二、編寫測試類
@RunWith(SpringRunner.class) @SpringBootTest(classes = MySpringBootApplication.class) public class MapperTest { @Autowired private UserMapper userMapper; @Test public void test() { List<User> users = userMapper.queryUserList(); System.out.println(users); } }
其中,SpringRunner繼承自SpringJUnit4ClassRunner,使用哪個Spring提供的測試測試引擎均可以
public final class SpringRunner extends SpringJUnit4ClassRunner
@SpringBootTest的屬性指定的是spring引導類(啓動類)的字節碼對象
===================
參考資料:
end