mybatis+springboot中使用mysql的實例
在軟件開發(fā)中,數(shù)據(jù)庫的引入是必不可少的,其中又屬mysql使用最為廣泛,而在springboot中,集成使用mysql的方式有很多(例如jpa),這里來展現(xiàn)一下通過mybatis框架在springboot中使用mysql。
依賴引入
首先在使用初始化工程的時候加入mybatis、mysql相關(guān)的依賴,如下所示:
<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的自動配置功能配置數(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的引入同樣可以使用自動配置(MybatisProperties)來實現(xiàn),這里選擇不全部使用自動配置屬性,而是引入mybatis的配置文件方式注入屬性。
mybatis: config-location: classpath:mybatis-config.xml mapper-locations: classpath:com/lazycece/sbac/mysql/data/dao/*/mapper/*.xml
這里給出一個mybatis的簡單配置,有關(guān)更多的配置屬性,可以參考mybatis的XML配置。
<?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> <!-- 使用jdbc的getGeneratedKEYS 獲取數(shù)據(jù)庫自增主鍵 --> <setting name="useGeneratedKeys" value="true"/> <!-- 開啟駝峰命名轉(zhuǎn)換 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--日志輸出sql語句--> <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>
案例實現(xiàn)
這里給出一個簡單的案例(用戶信息的插入和查詢)來展現(xiàn)mapper層的實現(xiàn),實體就不在這里展示了。mapper接口如下:
@Mapper public interface UserDao { void insert(User user); User findByUsername(@Param("username") String username); }
同時需要在主函數(shù)上加入注解@MapperScan來掃描我們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實現(xiàn),這里選擇用xml的方式實現(xiàn),當(dāng)然也可以選擇用注解方式(這里對應(yīng)的@Select,@Insert):
<!DOCTYPE mapper PUBLIC "-//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 id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id"> INSERT INTO user (create_time,update_time,username,password,telephone,status,editor) VALUE (#{createTime},#{updateTime},#{username},#{password},#{telephone},#{status},#{editor}); </insert> <select id="findByUsername" resultType="User"> SELECT <include refid="allColumn"/> FROM user WHERE username = #{username}; </select> </mapper>
細心者可發(fā)現(xiàn),前面配置引入中引入mapper是寫的classpath:com/lazycece/sbac/mysql/data/dao//mapper/.xml包名路徑,使用這種方式默認情況下是不會成功的,因為包路徑下文件默認只會編譯java文件,所以需要在pom文件中加入配置使得在工程編譯時將其包含進編譯后的路徑下。
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>com/lazycece/sbac/mysql/data/dao/*/mapper/*.xml</include> </includes> </resource> </resources> </build>
案例源碼
到此這篇關(guān)于mybatis+springboot中使用mysql的實例的文章就介紹到這了,更多相關(guān)mybatis springboot mysql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea2020.2卡死在reading maven projects
這篇文章主要介紹了idea2020.2卡死在reading maven projects,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Spring-AOP自動創(chuàng)建代理之BeanNameAutoProxyCreator實例
這篇文章主要介紹了Spring-AOP自動創(chuàng)建代理之BeanNameAutoProxyCreator實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07HashMap紅黑樹入門(實現(xiàn)一個簡單的紅黑樹)
紅黑樹(Red Black Tree) 是一種自平衡二叉查找樹,是在計算機科學(xué)中用到的一種數(shù)據(jù)結(jié)構(gòu),典型的用途是實現(xiàn)關(guān)聯(lián)數(shù)組。 紅黑樹發(fā)明時被稱為平衡二叉B樹,后來修改為如今的“紅黑樹”2021-06-06詳解Spring整合mybatis--Spring中的事務(wù)管理(xml形式)
這篇文章主要介紹了Spring整合mybatis--Spring中的事務(wù)管理(xml形式),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-11-11Java實現(xiàn)字符串反轉(zhuǎn)的常用方法小結(jié)
在Java中,你可以使用多種方法來反轉(zhuǎn)字符串,這篇文章主要為大家整理了幾種常見的反轉(zhuǎn)字符串的方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03Java?InheritableThreadLocal使用示例詳解
InheritableThreadLocal繼承了ThreadLocal,此類擴展了ThreadLocal以提供從父線程到子線程的值的繼承:當(dāng)創(chuàng)建子線程時,子線程接收父線程具有的所有可繼承線程局部變量的初始值。?通常子線程的值與父線程的值是一致的2022-09-09