免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
在Spring Boot中集成Mybatis

    之前我們講過在Spring Boot中集成JPA“在spring Boot中使用Spring-data-jpa操作數(shù)據(jù)庫”,本篇介紹另外一個(gè)ORM框架Mybatis在Spring Boot中的集成使用。

    最近一直在用JPA,其實(shí)還是蠻喜歡JPA的風(fēng)格,最近需要開一個(gè)新項(xiàng)目,還沒定下具體用什么ORM框架,不過Mybatis的幾率會(huì)大一些,本篇就相當(dāng)于一個(gè)溫習(xí)回顧吧,話不多說了,大家看下面步驟吧。

在Spring Boot中集成Mybatis

  • 在POM中添加Mybatis的相關(guān)起步依賴,Spring Boot的項(xiàng)目構(gòu)建大家可以參考我的這篇文章“Spring Boot快速入門
dependency> groupId>org.mybatis.spring.bootgroupId> artifactId>mybatis-spring-boot-starterartifactId> version>1.1.1version>dependency>dependency> groupId>mysqlgroupId> artifactId>mysql-connector-javaartifactId>dependency>
  •  配置數(shù)據(jù)庫連接,我們?cè)赼pplication.yml文件中配置一下內(nèi)容
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/restful?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: root initialize: true
  • 在數(shù)據(jù)庫中創(chuàng)建user表,字段為ID(INT),NAME(VARCHAR),HOBBY(VARCHAR),并創(chuàng)建user實(shí)體Bean,如下:
public class User { private Integer id; private String name; private String hobby; //省去get、set方法 //之前文章我提過一個(gè)lombok的框架,可以省去get set方法,大家可以參考一下 //https://my.oschina.net/wangxincj/blog/811611 @Override public String toString() { return 'User{' + 'id=' + id + ', name='' + name + '\'' + ', hobby='' + hobby + '\'' + '}'; }}
  • user對(duì)象創(chuàng)建好后我們編寫UserMapper,此處有兩種方法,大家可以自行選擇:

    1、第一種方法如下:

  • 編寫Mapper
import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import java.util.List;@Mapperpublic interface UserMapper { int insert(@Param('pojo') User pojo); int insertList(@Param('pojos') List pojo); int update(@Param('pojo') User pojo);}
  • 在resources文件夾下面創(chuàng)建mapper文件夾,存放mapper.xml文件 
  •  編寫UserMapper.xml
<>xml version='1.0' encoding='UTF-8' ?>mapper namespace='com.wang.mybatis.UserMapper'> resultMap id='AllColumnMap' type='com.wang.mybatis.User'> result column='id' property='id'/> result column='name' property='name'/> result column='hobby' property='hobby'/> resultMap> sql id='all_column'> `id`, `name`, `hobby` sql> insert id='insert'> INSERT INTO `user` (`id`,`name`,`hobby`) VALUES (#{pojo.id},#{pojo.name},#{pojo.hobby}) insert> insert id='insertList'> INSERT INTO `user`( include refid='all_column'/> )VALUES foreach collection='pojos' item='pojo' index='index' separator=','> ( #{pojo.id}, #{pojo.name}, #{pojo.hobby} ) foreach> insert> update id='update'> UPDATE `user` set> if test='pojo.id != null'> `id` = #{pojo.id}, if> if test='pojo.name != null'> `name` = #{pojo.name}, if> if test='pojo.hobby != null'> `pwd` = #{pojo.hobby} if> set> WHERE `id` = #{pojo.id} update>mapper>
  •  在application.yml中添加Mybatis的相關(guān)配置
mybatis: mapper-locations: classpath*:mapper/*Mapper.xml type-aliases-package: com.wang.mybatis

    2、第二種方法如下

  • 編寫Mapper對(duì)象
@Mapperpublic interface UserMapper { @Insert('INSERT INTO user(name, hobby) VALUES(#{name}, #{hobby})') int insert(@Param('name') String name, @Param('hobby') String hobby); @Update('UPDATE user SET hobby=#{hobby} WHERE name=#{name}') void update(User user); @Delete('DELETE FROM user WHERE id =#{id}') void delete(Long id);}

    不難看出第一種方法是比較傳統(tǒng)的xml方式配置,而第二種方式則是去除了xml將sql配置到了注解中,簡化了配置,也更符合Spring Boot的初衷。兩種方法大家看自己的情況而定吧。

    至此,Mybatis的相關(guān)配置完成。

ps:在此給大家安利一個(gè)Mybatis的自動(dòng)化插件MyBatisCodeHelper,使用這個(gè)插件,大家只要寫好實(shí)體bean,插件即可自動(dòng)創(chuàng)建相應(yīng)的sql腳本、Mapper.xml、Mapper層代碼、service層代碼,在Mapper層中大家還可以根據(jù)一定的規(guī)則編寫方法名稱,插件自動(dòng)根據(jù)方法名稱在xml生成相應(yīng)的配置。

插件地址:https://www.oschina.net/news/81912/mybatiscodehelper-1-2

    本篇主要介紹Mybatis的配置,之后會(huì)有進(jìn)階篇,主要介紹事物、多數(shù)據(jù)源等等。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Mybatis的CRUD
最簡單的SpringBoot整合MyBatis教程
MyBatis使用注解開發(fā)
Mybatis 插入與批量插入以及多參數(shù)批量刪除
MyBatis 中 @Param 注解的四種使用場景,最后一種經(jīng)常被人忽略!
SpringBoot 集成 MyBatis 框架 【SpringBoot系列2】
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服