Spring Boot + Mybatis 全注解與XML

IDEA 創(chuàng)建項目


創(chuàng)建項目.PNG

全注解方式

Mybatis maper的全注解形式允粤,不用dao接口寺惫,不用mapper的xml文件睬棚,都在一個類里面

/**
 * mybatis的dao接口和mapper配置文件的合體
 */
@Mapper
@Repository
public interface CustomerMapper {
    @Select("SELECT * FROM customers")
    List<Customer> findAll();
}

application.properties

server.port=8080
server.servlet.context-path=/boot

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

# mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/shpun?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

測試通過

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestspringbootApplicationTests {

    @Autowired
    private CustomerMapper customerMapper;

    @Test
    public void testCustomerMapper(){
        System.out.println(customerMapper.findAll());
    }

}

xml方式

mybatis-config.xml mybatis的配置文件

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <!-- 全局映射器啟用緩存 -->
        <setting name="cacheEnabled" value="true"/>
        <!-- 允許JDBC支持生成的鍵 -->
        <setting name="useGeneratedKeys" value="true"/>
        <!-- 配置默認執(zhí)行器 Reuse 重用預處理語句 -->
        <setting name="defaultExecutorType" value="REUSE"/>
        <!-- 全局啟用延遲加載 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 設置超時時間表制,決定驅(qū)動等待一個數(shù)據(jù)庫相應的時間 -->
        <setting name="defaultStatementTimeout" value="2500"/>
    </settings>

</configuration>

CustomerMapper.xml mapper的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.shpun.dao.CustomerDao">
    <select id="selectAllCustomers" resultType="customer">
        select * from customers
    </select>
</mapper>

application.properties

server.port=8888
server.servlet.context-path=/yml

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

# mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/shpun?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

# mybatis
mybatis.type-aliases-package=com.shpun.bean
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml

dao 接口

@Mapper
@Repository
public interface CustomerDao{
    List<Customer> selectAllCustomers();
}

測試通過

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestspringbootApplicationTests {

    @Autowired
    private CustomerDao customerDao;

    @Test
    public void testCustomerDao(){

        System.out.println(customerDao.selectAllCustomers());

    }
}

ps:

先前用xml的時候崎场,提示錯誤找不到dao的bean,在dao上添加

@Mapper

如果是多個dao接口的話砂竖,每個都加比較麻煩真椿,可以在SpringBootApplication的java類中添加

@MapperScan("com.shpun.dao")

參考 如何優(yōu)雅的使用mybatis

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市乎澄,隨后出現(xiàn)的幾起案子突硝,更是在濱河造成了極大的恐慌,老刑警劉巖置济,帶你破解...
    沈念sama閱讀 222,865評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件解恰,死亡現(xiàn)場離奇詭異,居然都是意外死亡浙于,警方通過查閱死者的電腦和手機护盈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,296評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來羞酗,“玉大人腐宋,你說我怎么就攤上這事。” “怎么了胸竞?”我有些...
    開封第一講書人閱讀 169,631評論 0 364
  • 文/不壞的土叔 我叫張陵欺嗤,是天一觀的道長。 經(jīng)常有香客問我撤师,道長剂府,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,199評論 1 300
  • 正文 為了忘掉前任剃盾,我火速辦了婚禮,結(jié)果婚禮上淤袜,老公的妹妹穿的比我還像新娘痒谴。我一直安慰自己,他們只是感情好铡羡,可當我...
    茶點故事閱讀 69,196評論 6 398
  • 文/花漫 我一把揭開白布积蔚。 她就那樣靜靜地躺著,像睡著了一般烦周。 火紅的嫁衣襯著肌膚如雪尽爆。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,793評論 1 314
  • 那天读慎,我揣著相機與錄音漱贱,去河邊找鬼。 笑死夭委,一個胖子當著我的面吹牛幅狮,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播株灸,決...
    沈念sama閱讀 41,221評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼崇摄,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了慌烧?” 一聲冷哼從身側(cè)響起逐抑,我...
    開封第一講書人閱讀 40,174評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎屹蚊,沒想到半個月后厕氨,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,699評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡淑翼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,770評論 3 343
  • 正文 我和宋清朗相戀三年腐巢,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片玄括。...
    茶點故事閱讀 40,918評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡冯丙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情胃惜,我是刑警寧澤泞莉,帶...
    沈念sama閱讀 36,573評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站船殉,受9級特大地震影響鲫趁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜利虫,卻給世界環(huán)境...
    茶點故事閱讀 42,255評論 3 336
  • 文/蒙蒙 一挨厚、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧糠惫,春花似錦疫剃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,749評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至固阁,卻和暖如春壤躲,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背备燃。 一陣腳步聲響...
    開封第一講書人閱讀 33,862評論 1 274
  • 我被黑心中介騙來泰國打工碉克, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人赚爵。 一個月前我還...
    沈念sama閱讀 49,364評論 3 379
  • 正文 我出身青樓棉胀,卻偏偏與公主長得像,于是被迫代替她去往敵國和親冀膝。 傳聞我的和親對象是個殘疾皇子唁奢,可洞房花燭夜當晚...
    茶點故事閱讀 45,926評論 2 361