微架構(gòu) springcloud-05. springboot-整合mybaties

springboot 整合Mybaites

上一節(jié)講到springboot 如何使用druid數(shù)據(jù)源連接mysql并使用JPA進行CRUD操作,這一節(jié)繼續(xù)關(guān)注持久化層,看看springboot 是如何整合Mybatise

00 新建一個maven java 項目,pom.xml 初始導(dǎo)入以下依賴:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
</parent>

<dependencies>
    
    <!--springboot-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--springboot 熱部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

    <!--thymeleaf 模板引擎-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!--阿里數(shù)據(jù)源-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>

    <!--mysql 依賴-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.7</version>
    </dependency>

</dependencies>

01 以上是前一節(jié)中所使用到的依賴斯碌,pom.xml 導(dǎo)入mybaties-springboot依賴:

<!--mybaties-springboot 依賴-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>

02 通過mybaties逆向工程生成實體類炒嘲、dao亏钩,并移植到項目中:

person.jack
    bean
        Dept
        DeptMapper.xml
        Emp
        EmpMapper.xml
    dao
        DeptMapper
        EmpMapper
        

03 配置resources/application.properties,配置數(shù)據(jù)源

spring.thymeleaf.cache=false
logging.level.root=info

#### 配置默認數(shù)據(jù)源厉熟,并使用 阿里連接池 ###
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=true
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

######JPA 配置#####
# 聲明數(shù)據(jù)庫
spring.jpa.database=mysql
# 是否顯示SQL語句
spring.jpa.show-sql = true
# Hibernate 自動DDL 操作
# create 每次加載hibernate時都會刪除上一次的生成的表
# create-drop 每次加載hibernate時根據(jù)model類生成表汽抚,但是sessionFactory一關(guān)閉,表就自動刪除
# update 最常用的屬性儒士,第一次加載hibernate時根據(jù)model類會自動建立起表的結(jié)構(gòu)(前提是先建立好數(shù)據(jù)庫)
spring.jpa.hibernate.ddl-auto=update
#配置方言
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect

04 在application.properties 配置mybatis *Mapper.xml 文件的位置:

### mybaties 配置 ###
#*Mapper.xml文件位置
mybatis.mapper-locations=classpath:/person/jack/bean/*.xml

05 在啟動類中使用@MapperScan 配置Mapper接口的位置:

@SpringBootApplication

/**配置 Mapper 接口的位置*/
@MapperScan("person.jack.dao")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

06 添加測試依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

07 編寫測試類:

@RunWith(SpringJUnit4ClassRunner.class) /*添加SpringJUnit支持,引入Spring-Test框架*/
@SpringBootTest(classes = App.class) /*指定Springboot啟動類啟動*/
public class TestMybatis {

    @Autowired
    private EmpMapper empMapper;

    @Test
    public void test(){
        Emp emp = empMapper.selectByPrimaryKey(7788);
        System.out.println(emp);
    }
}

08 重寫實體類 Emp toString() 方法:

@Override
public String toString() {
    return "Emp{" +
        "empno=" + empno +
        ", ename='" + ename + '\'' +
        ", job='" + job + '\'' +
        ", mgr=" + mgr +
        ", hiredate=" + hiredate +
        ", sal=" + sal +
        ", comm=" + comm +
        ", deptno=" + deptno +
        '}';
}

09 運行測試方法test():

#后臺打印
Emp{empno=7788, ename='SCOTT', job='ANALYST', mgr=7566, hiredate=Sun Apr 19 00:00:00 CDT 1987, sal=3000, comm=null, deptno=20}

# 主鍵為 7788 的員工成功查詢,SpringBoot 成功整合Mybatis

10 Controller 測試花鹅,在EmpMapper.xml 中添加查詢,對應(yīng)添加到Dao:

#EmpMapper.xml
<select id="selectAll" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from emp
</select>

#person/jack/dao/EmpMapper.java 添加抽象方法
List<Emp> selectAll();

11 編寫 person/jack/controller/EmpController

@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpMapper empMapper;

    @RequestMapping("/empList")
    public String empList(Map map){
        map.put("empList", empMapper.selectAll());
        return "empList";
    }
}

12 編寫empList.html 模板枫浙,使用th:each 迭代刨肃,#dates.format格式化輸出日期:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>員工列表</title>
</head>
<body>
    <table border="1" cellspacing="0" cellpadding="5">
        <tr>
            <td>序號</td>
            <td>編號</td>
            <td>姓名</td>
            <td>工作</td>
            <td>工資</td>
            <td>獎金</td>
            <td>入職日期</td>
        </tr>
        <tr th:each="emp,status:${empList}">
            <td th:text="${status.count}"/>
            <td th:text="${emp.empno} "/>
            <td th:text="${emp.ename}"/>
            <td th:text="${emp.job}"/>
            <td th:text="${emp.sal}"/>
            <td th:text="${emp.comm}"/>
            <td th:text="${#dates.format(emp.hiredate, 'yyyy-MM-dd')}"/>
        </tr>
    </table>
</body>
</html>

13 瀏覽器訪問:http://localhost:8080/emp/empList,頁面打印

序號 編號 姓名 工作 工資 獎金 入職日期
1 7369 SMITH CLERK 800 1980-12-17
2 7499 ALLEN SALESMAN 1600 300 1981-02-20
3 7521 WARD SALESMAN 1250 500 1981-02-22
4 7566 JONES MANAGER 2975 1981-04-02
5 7654 MARTIN SALESMAN 1250 1400 1981-09-28
6 7698 BLAKE MANAGER 2850 1981-05-01
7 7782 CLARK MANAGER 2450 1981-06-09
8 7788 SCOTT ANALYST 3000 1987-04-19
9 7839 KING PRESIDENT 5000 1981-11-17
10 7844 TURNER SALESMAN 1500 0 1981-09-08
11 7876 ADAMS CLERK 1100 1987-05-23
12 7900 JAMES CLERK 950 1981-12-03
13 7902 FORD ANALYST 3000 1981-12-03
14 7934 MILLER CLERK 1300 1982-01-23

14 測試成功箩帚!

總結(jié)Springboot 整合mybaties 可分為以下幾步:

  1. 導(dǎo)入依賴 mybatis-spring-boot-starter
  2. application.properties 配置 application.properties
  3. 在啟動類中添加 @MapperScan 注解掃描Dao
  4. 注入Dao真友,使用!
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末紧帕,一起剝皮案震驚了整個濱河市盔然,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌是嗜,老刑警劉巖愈案,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異鹅搪,居然都是意外死亡站绪,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門涩嚣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來崇众,“玉大人,你說我怎么就攤上這事航厚∏旮瑁” “怎么了?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵幔睬,是天一觀的道長眯漩。 經(jīng)常有香客問我,道長麻顶,這世上最難降的妖魔是什么赦抖? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮辅肾,結(jié)果婚禮上队萤,老公的妹妹穿的比我還像新娘。我一直安慰自己矫钓,他們只是感情好要尔,可當(dāng)我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布舍杜。 她就那樣靜靜地躺著,像睡著了一般赵辕。 火紅的嫁衣襯著肌膚如雪既绩。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天还惠,我揣著相機與錄音饲握,去河邊找鬼。 笑死蚕键,一個胖子當(dāng)著我的面吹牛救欧,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播嚎幸,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼颜矿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了嫉晶?” 一聲冷哼從身側(cè)響起骑疆,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎替废,沒想到半個月后箍铭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡椎镣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年诈火,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片状答。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡冷守,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出惊科,到底是詐尸還是另有隱情拍摇,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布馆截,位于F島的核電站充活,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏蜡娶。R本人自食惡果不足惜混卵,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望窖张。 院中可真熱鬧幕随,春花似錦、人聲如沸宿接。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽澄阳。三九已至拥知,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間碎赢,已是汗流浹背低剔。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留肮塞,地道東北人襟齿。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像枕赵,于是被迫代替她去往敵國和親猜欺。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理开皿,服務(wù)發(fā)現(xiàn),斷路器篮昧,智...
    卡卡羅2017閱讀 134,657評論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,822評論 6 342
  • 一、何為落款 落款躏惋,是在書畫作品主體內(nèi)容完成后幽污,作者的簽名其掂、簽印、年月款熬、軒號等深寥,以示作品的完整性贤牛。 落款的分類 1...
    管文寧閱讀 1,097評論 0 1
  • 這是一個真實的夢。韓星加盟的腦洞劇情殉簸,謝謝捧場沽讹。 那段時間我正在看卷福版的福爾摩斯,心里有個偵探結(jié)武鲁。 1,道觀戲臺...
    銀河歆閱讀 485評論 0 2