亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

mybatis+springboot中使用mysql的實(shí)例

瀏覽:3日期:2023-02-05 14:28:06
目錄依賴(lài)引入配置引入案例實(shí)現(xiàn)案例源碼

在軟件開(kāi)發(fā)中,數(shù)據(jù)庫(kù)的引入是必不可少的,其中又屬mysql使用最為廣泛,而在springboot中,集成使用mysql的方式有很多(例如jpa),這里來(lái)展現(xiàn)一下通過(guò)mybatis框架在springboot中使用mysql。

依賴(lài)引入

首先在使用初始化工程的時(shí)候加入mybatis、mysql相關(guān)的依賴(lài),如下所示:

<dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId></dependency><dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope></dependency><dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version></dependency> </dependencies>配置引入

在配置方面,使用springboot的自動(dòng)配置功能配置數(shù)據(jù)源,如下所示:

spring: datasource: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://127.0.0.1:3306/sbac_master?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true username: root password: 1234 driver-class-name: com.mysql.cj.jdbc.Driver

配置了數(shù)據(jù)源,剩下的就是引入mybatis了。mybatis的引入同樣可以使用自動(dòng)配置(MybatisProperties)來(lái)實(shí)現(xiàn),這里選擇不全部使用自動(dòng)配置屬性,而是引入mybatis的配置文件方式注入屬性。

mybatis: config-location: classpath:mybatis-config.xml mapper-locations: classpath:com/lazycece/sbac/mysql/data/dao/*/mapper/*.xml

這里給出一個(gè)mybatis的簡(jiǎn)單配置,有關(guān)更多的配置屬性,可以參考mybatis的XML配置。

<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE configurationPUBLIC '-//mybatis.org//DTD Config 3.0//EN''http://mybatis.org/dtd/mybatis-3-config.dtd'><configuration> <settings><!-- 使用jdbc的getGeneratedKEYS 獲取數(shù)據(jù)庫(kù)自增主鍵 --><setting name='useGeneratedKeys' value='true'/><!-- 開(kāi)啟駝峰命名轉(zhuǎn)換 --><setting name='mapUnderscoreToCamelCase' value='true'/><!--日志輸出sql語(yǔ)句--><setting name='logImpl' value='STDOUT_LOGGING'/> </settings> <typeAliases><package name='com.lazycece.sbac.mysql.data.domain'/> </typeAliases> <typeHandlers><typeHandler handler='org.apache.ibatis.type.EnumOrdinalTypeHandler' javaType='com.lazycece.sbac.mysql.data.domain.Status'/> </typeHandlers></configuration>案例實(shí)現(xiàn)

這里給出一個(gè)簡(jiǎn)單的案例(用戶信息的插入和查詢)來(lái)展現(xiàn)mapper層的實(shí)現(xiàn),實(shí)體就不在這里展示了。mapper接口如下:

@Mapperpublic interface UserDao { void insert(User user); User findByUsername(@Param('username') String username);}

同時(shí)需要在主函數(shù)上加入注解@MapperScan來(lái)掃描我們mapper:

@SpringBootApplication@MapperScan(basePackages = {'com.lazycece.sbac.mysql.data.dao'})public class SpringbootAcMysqlSimpleApplication { public static void main(String[] args) {SpringApplication.run(SpringbootAcMysqlSimpleApplication.class, args); }}

mapper的sql實(shí)現(xiàn),這里選擇用xml的方式實(shí)現(xiàn),當(dāng)然也可以選擇用注解方式(這里對(duì)應(yīng)的@Select,@Insert):

<!DOCTYPE mapperPUBLIC '-//mybatis.org//DTD Mapper 3.0//EN''http://mybatis.org/dtd/mybatis-3-mapper.dtd'><mapper namespace='com.lazycece.sbac.mysql.data.dao.master.UserDao'> <sql id='allColumn'>id,create_time,update_time,username,password,telephone,status,editor </sql> <insert parameterType='User' useGeneratedKeys='true' keyProperty='id'>INSERT INTOuser (create_time,update_time,username,password,telephone,status,editor)VALUE (#{createTime},#{updateTime},#{username},#{password},#{telephone},#{status},#{editor}); </insert> <select resultType='User'>SELECT<include refid='allColumn'/>FROM userWHERE username = #{username}; </select></mapper>

細(xì)心者可發(fā)現(xiàn),前面配置引入中引入mapper是寫(xiě)的classpath:com/lazycece/sbac/mysql/data/dao//mapper/.xml包名路徑,使用這種方式默認(rèn)情況下是不會(huì)成功的,因?yàn)榘窂较挛募J(rèn)只會(huì)編譯java文件,所以需要在pom文件中加入配置使得在工程編譯時(shí)將其包含進(jìn)編譯后的路徑下。

<build><resources> <resource><directory>src/main/java</directory><includes> <include>com/lazycece/sbac/mysql/data/dao/*/mapper/*.xml</include></includes> </resource></resources> </build>案例源碼

案例源碼地址: https://github.com/lazycece/springboot-actual-combat/tree/master/springboot-ac-mysql/springboot-ac-mysql-simple

到此這篇關(guān)于mybatis+springboot中使用mysql的實(shí)例的文章就介紹到這了,更多相關(guān)mybatis springboot mysql內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产免费破外女真实出血视频 | 黄色免费网址在线观看 | 亚洲精品中文字幕无乱码 | 亚洲精品入口一区二区乱成人 | 欧美成人免费观看 | 色综合久久精品中文字幕 | 国产婷婷成人久久av免费高清 | 亚洲尹人香蕉网在线视颅 | 大伊香蕉在线观看视频 wap | 久久精品7| 日本一级特黄毛片高清视频 | 香蕉福利视频 | 国产一级特黄aa大片软件 | 国产亚洲欧洲日韩综合v | 69国产成人精品午夜福中文 | 国产一级二级三级毛片 | 国产精品久久久久久久久免费hd | 欧美一区二区放荡人妇 | 久久综合噜噜激激的五月天 | 五月婷婷亚洲综合 | 视频在线观看rrr在线观看 | 亚洲人与牲动交xxxxbbbb | 国产成人精品视频免费大全 | 日产国产欧美韩国在线 | 国产日韩欧美 | 欧美亚洲国产另类在线观看 | 日本aa在线 | 日本一二三四区免费视频 | 九九热精品视频在线 | 婷婷综合久久狠狠色99h | 一级特大黄色片 | 91福利社在线观看 | 久久亚洲精品人成综合网 | 尤物在线| 国产手机在线小视频免费观看 | 男女爱爱视频在线观看 | 精品一区二区三区影片 | 久久精品免看国产成 | 黄色片免费在线播放 | 国产福利视频在线播放 | 成人精品一区二区久久 |