您好,登錄后才能下訂單哦!
序言:
此前,我們主要通過(guò)XML來(lái)書寫SQL和填補(bǔ)對(duì)象映射關(guān)系。在SpringBoot中我們可以通過(guò)注解來(lái)快速編寫SQL并實(shí)現(xiàn)數(shù)據(jù)訪問(wèn)。(僅需配置:mybatis.configuration.map-underscore-to-camel-case=true)。為了方便大家,本案例提供較完整的層次邏輯SpringBoot+MyBatis+Annotation。
具體步驟
1. 引入依賴
在pom.xml 引入ORM框架(Mybaits-Starter)和數(shù)據(jù)庫(kù)驅(qū)動(dòng)(MySQL-Conn)的依賴。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--繼承信息 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.M4</version> <relativePath/> </parent> <!--依賴管理 --> <dependencies> <dependency> <!--添加Web依賴 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <!--添加Mybatis依賴 --> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency><!--添加MySQL驅(qū)動(dòng)依賴 --> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency><!--添加Test依賴 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
2. 添加數(shù)據(jù)源
在application.yml 添加數(shù)據(jù)源,以及開(kāi)啟Mybaits的駝峰映射功能。
spring: datasource: url: jdbc:mysql://localhost:3306/socks?useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver mybatis: configuration: map-underscore-to-camel-case: true #開(kāi)啟駝峰映射
3. 編寫數(shù)據(jù)層代碼
// POJO類如下: public class User { private String userId; private String username; private String password; // Getters & Setters .. } // 數(shù)據(jù)層代碼如下: public interface UserMapper { @Select("select * from t_user where 1=1") List<User> list(); @Select("select * from t_user where username like #{username}") List<User> findByUsername(String username); @Select("select * from t_user where user_id like #{userId}") User getOne(String userId); @Delete("delete from t_user where user_id like #{userId}") int delete(String userId); }
4. 添加數(shù)據(jù)庫(kù)記錄
在Navicat 連接本地?cái)?shù)據(jù)庫(kù),隨便打開(kāi)查詢窗口,復(fù)制下面這段腳本,點(diǎn)擊執(zhí)行即可。
DROP DATABASE IF EXISTS `socks`; CREATE DATABASE `socks`; USE `SOCKS`; DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `USER_ID` varchar(50) , `USERNAME` varchar(50) , `PASSWORD` varchar(50) ) ; INSERT INTO `t_user` VALUES ('1', 'admin', 'admin'); INSERT INTO `t_user` VALUES ('2', 'yizhiwazi', '123456');
5. 啟動(dòng)項(xiàng)目
@SpringBootApplication @MapperScan("com.hehe.mapper") //掃描Mapper接口 public class MybatisApplication { public static void main(String[] args) { SpringApplication.run(MybatisApplication.class, args); } }
6. 單元測(cè)試
import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest public class MybatisApplicationTest { @SuppressWarnings("all") @Autowired UserMapper userMapper; @Test public void test_db() { //開(kāi)始進(jìn)行測(cè)試 assertThat(userMapper.list().size()).isGreaterThan(1); assertThat(userMapper.getOne("1")).isNotEqualTo(null); assertThat(userMapper.getOne("xxx")).isEqualTo(null); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。