日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

spring boot 1.5.4 整合 mybatis(十二)

發(fā)布時(shí)間:2025/6/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot 1.5.4 整合 mybatis(十二) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

上一篇:spring boot 1.5.4 整合log4j2(十一)

?

Spring Boot集成Mybatis

更多更詳細(xì)的配置參考文件:application.propertiesSpringBootapplication配置詳解》(新版本新增屬性缺失)? 或參考官網(wǎng)http://projects.spring.io/spring-boot/

?

Spring Boot集成Mybatis有兩種方式:

?

方式一:傳統(tǒng)的引入外部資源配置的方式,方便對(duì)mybatis的控制;

方式二:mybatis官方提供spring-boot整合的方式。

?

這里,還是使用UserMapper類和userMapper.xml文件分離的做法。關(guān)于mapper.xmlsql語(yǔ)句可以直接集成到Mapper接口中。詳見第4章節(jié):將SQL語(yǔ)句集成到UserMapper接口類中

?

1??????方式一:整合mybatis資源

1.1????新建spring-boot-mybatis項(xiàng)目

spring-boot-mybatis項(xiàng)目源碼地址:

spring-boot相關(guān)項(xiàng)目源碼,

碼云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址https://github.com/wyait/spring-boot-1.5.4.git


項(xiàng)目整體結(jié)構(gòu):

1.2????pom.xml??

<projectxmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

?? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

?? <modelVersion>4.0.0</modelVersion>

?? <parent>

????? <!--spring boot項(xiàng)目的parent -->

????? <groupId>org.springframework.boot</groupId>

????? <artifactId>spring-boot-starter-parent</artifactId>

????? <version>1.5.4.RELEASE</version>

?? </parent>

?? <groupId>com.wyait.boot</groupId>

?? <artifactId>spring-boot-mybatis</artifactId>

?? <version>0.0.1-SNAPSHOT</version>

?? <packaging>war</packaging>

?? <dependencies>

????? <dependency>

??????? <!--spring boot 引入Web模塊。自動(dòng)配置:tomcatspringmvc、jackson -->

??????? <groupId>org.springframework.boot</groupId>

??????? <artifactId>spring-boot-starter-web</artifactId>

??????? <exclusions>

?????????? <exclusion>

????????????? <artifactId>spring-boot-starter-logging</artifactId>

????????????? <groupId>org.springframework.boot</groupId>

?????????? </exclusion>

??????? </exclusions>

????? </dependency>

????? <dependency>

??????? <!--spring-boot整合mybatis-->

??????? <groupId>org.mybatis.spring.boot</groupId>

??????? <artifactId>mybatis-spring-boot-starter</artifactId>

??????? <version>1.1.1</version>

????? </dependency>

????? <dependency>

??????? <groupId>mysql</groupId>

??????? <artifactId>mysql-connector-java</artifactId>

????? </dependency>

????? <!--MyBatis提供了攔截器接口,我們可以實(shí)現(xiàn)自己的攔截器,將其作為一個(gè)plugin裝入到SqlSessionFactory中。 Github上有位開發(fā)者寫了一個(gè)分頁(yè)插件,我覺得使用起來還可以,挺方便的。

??????? Github項(xiàng)目地址:https://github.com/pagehelper/Mybatis-PageHelper -->

????? <dependency>

??????? <groupId>com.github.pagehelper</groupId>

??????? <artifactId>pagehelper</artifactId>

??????? <version>4.1.0</version>

????? </dependency>

????? <!--tomcat 的支持. -->

????? <dependency>

??????? <groupId>org.springframework.boot</groupId>

??????? <artifactId>spring-boot-starter-tomcat</artifactId>

??????? <!--添加<scope>provided</scope>,因?yàn)?/span>provided表明該包只在編譯和測(cè)試的時(shí)候用 -->

??????? <scope>provided</scope>

????? </dependency>

????? <dependency>

??????? <!--jsp頁(yè)面支持 -->

??????? <groupId>org.apache.tomcat.embed</groupId>

??????? <artifactId>tomcat-embed-jasper</artifactId>

??????? <scope>provided</scope>

????? </dependency>

????? <dependency>

??????? <groupId>javax.servlet</groupId>

??????? <artifactId>jstl</artifactId>

????? </dependency>

????? <dependency>

??????? <groupId>org.springframework.boot</groupId>

??????? <artifactId>spring-boot-starter-log4j</artifactId>

??????? <!--這里需要指定版本,否則報(bào)錯(cuò)【有可能是spring-boot-1.5.4.RELEASE版本沒有管理log4j版本的原因】 -->

??????? <version>1.3.2.RELEASE</version>

????? </dependency>

?

????? <!--spring boot集成Swagger2-->

????? <dependency>

??????? <groupId>io.springfox</groupId>

??????? <artifactId>springfox-swagger2</artifactId>

??????? <version>2.6.1</version>

????? </dependency>

????? <dependency>

??????? <groupId>io.springfox</groupId>

??????? <artifactId>springfox-swagger-ui</artifactId>

??????? <version>2.6.1</version>

????? </dependency>

????? <!--devtools可以實(shí)現(xiàn)頁(yè)面熱部署(即頁(yè)面修改后會(huì)立即生效,這個(gè)可以直接在application.properties文件中配置spring.thymeleaf.cache=false或者視圖解析器設(shè)置緩存為false來實(shí)現(xiàn)),

??????? 實(shí)現(xiàn)類文件熱部署(類文件修改后不會(huì)立即生效,待編譯后生效),實(shí)現(xiàn)對(duì)屬性文件的熱部署。devtools會(huì)監(jiān)聽classpath下的文件變動(dòng),并且會(huì)立即重啟應(yīng)用(發(fā)生在保存時(shí)機(jī)),注意:因?yàn)槠洳捎玫奶摂M機(jī)機(jī)制,該項(xiàng)重啟是很快的 -->

????? <dependency>

??????? <groupId>org.springframework.boot</groupId>

??????? <artifactId>spring-boot-devtools</artifactId>

??????? <optional>true</optional>

??????? <!--optional=true,依賴不會(huì)傳遞,該項(xiàng)目依賴devtools;之后依賴SpringBoot1項(xiàng)目的項(xiàng)目如果想要使用devtools,需要重新引入 -->

????? </dependency>

?? </dependencies>

?? <build>

????? <plugins>

??????? <plugin>

?????????? <!--配置spring bootmaven插件 -->

?????????? <groupId>org.springframework.boot</groupId>

?????????? <artifactId>spring-boot-maven-plugin</artifactId>

?????????? <!--<configuration> fork : 如果沒有該項(xiàng)配置,devtools不會(huì)起作用,即應(yīng)用不會(huì)restart 【實(shí)測(cè):可以不配置】

????????????? <fork>true</fork></configuration> -->

??????? </plugin>

????? </plugins>

?? </build>

</project>

1.3????Application.java

// 這是一個(gè)配置Spring的配置類

@Configuration

// @SpringBootApplicationSpring Boot項(xiàng)目的核心注解,主要目的是開啟自動(dòng)配置,自動(dòng)掃描該類同級(jí)包以及子包。

@SpringBootApplication

public class Application {

?

?? publicstatic void main(String[] args) {

????? //啟動(dòng)spring boot應(yīng)用

????? SpringApplicationsa = new SpringApplication(Application.class);

????? //禁用devTools熱部署

????? System.setProperty("spring.devtools.restart.enabled","false");

????? //禁用命令行更改application.properties屬性

????? sa.setAddCommandLineProperties(false);

????? sa.run(args);

?? }

}

1.4????mybatis相關(guān)配置類:集成pageHelper分頁(yè)插件,并開啟事務(wù)

?

@Configuration

@EnableTransactionManagement

// 開啟注解事務(wù)支持

public class MybatisConfigimplements TransactionManagementConfigurer {

?? //spring容器管理,可以直接注入使用

?? @Autowired

?? DataSourcedataSource;

?

?? @Bean(name= "sqlSessionFactory")

?? publicSqlSessionFactory sqlSessionFactoryBean() {

????? SqlSessionFactoryBeanbean = new SqlSessionFactoryBean();

????? bean.setDataSource(dataSource);

????? bean.setTypeAliasesPackage("com.wyait.boot.pojo");

?

????? //分頁(yè)插件

????? PageHelperpageHelper = new PageHelper();

????? Propertiesproperties = new Properties();

????? properties.setProperty("reasonable","true");

????? properties.setProperty("supportMethodsArguments","true");

????? properties.setProperty("returnPageInfo","check");

????? properties.setProperty("params","count=countSql");

????? pageHelper.setProperties(properties);

?

????? //添加插件

????? bean.setPlugins(newInterceptor[] { pageHelper });

?

????? //添加XML目錄

????? ResourcePatternResolverresolver = new PathMatchingResourcePatternResolver();

????? try{

??????? bean.setMapperLocations(resolver

????????????? .getResources("classpath:mybatis/*.xml"));

??????? returnbean.getObject();

????? }catch (Exception e) {

??????? e.printStackTrace();

??????? thrownew RuntimeException(e);

????? }

?? }

?

?? @Bean

?? publicSqlSessionTemplate sqlSessionTemplate(

??????? SqlSessionFactorysqlSessionFactory) {

????? returnnew SqlSessionTemplate(sqlSessionFactory);

?? }

?

?? //開啟注解事務(wù)

?? @Bean

?? @Override

?? publicPlatformTransactionManager annotationDrivenTransactionManager() {

????? returnnew DataSourceTransactionManager(dataSource);

?? }

}

1.5????TODO 編寫實(shí)體類、service、mapper、mapper.xml

1.6??? 啟動(dòng),測(cè)試

TODO


?

2??????方式二:mybatis整合spring-boot

mybatis-spring-boot項(xiàng)目源碼地址

spring-boot相關(guān)項(xiàng)目源碼,

碼云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址https://github.com/wyait/spring-boot-1.5.4.git

2.1????新建mybatis-spring-boot工程

項(xiàng)目結(jié)構(gòu):

Application.java

// 這是一個(gè)配置Spring的配置類

@Configuration

// @SpringBootApplicationSpring Boot項(xiàng)目的核心注解,主要目的是開啟自動(dòng)配置,自動(dòng)掃描該類同級(jí)包以及子包。

@SpringBootApplication

//@MapperScan(basePackages ="com.wyait.boot.dao")

public class Application {

?

?? publicstatic void main(String[] args) {

????? //啟動(dòng)spring boot應(yīng)用

????? SpringApplicationsa = new SpringApplication(Application.class);

????? //禁用devTools熱部署

????? System.setProperty("spring.devtools.restart.enabled","false");

????? //禁用命令行更改application.properties屬性

????? sa.setAddCommandLineProperties(false);

????? sa.run(args);

?? }

}

?

2.2????pom.xml

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

?? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

?? <modelVersion>4.0.0</modelVersion>

?? <parent>

????? <!--spring boot項(xiàng)目的parent -->

????? <groupId>org.springframework.boot</groupId>

????? <artifactId>spring-boot-starter-parent</artifactId>

????? <version>1.5.4.RELEASE</version>

?? </parent>

?? <groupId>com.wyait.mybatis</groupId>

?? <artifactId>mybatis-spring-boot</artifactId>

?? <version>0.0.1-SNAPSHOT</version>

?? <packaging>war</packaging>

?? <dependencies>

????? <dependency>

??????? <!--spring boot 引入Web模塊。自動(dòng)配置:tomcatspringmvc、jackson -->

?? ????? <groupId>org.springframework.boot</groupId>

??????? <artifactId>spring-boot-starter-web</artifactId>

??????? <exclusions>

?????????? <exclusion>

????????????? <artifactId>spring-boot-starter-logging</artifactId>

????????????? <groupId>org.springframework.boot</groupId>

?????????? </exclusion>

????? ?? </exclusions>

????? </dependency>

????? <dependency>

??????? <!--spring-boot整合mybatis-->

??????? <groupId>org.mybatis.spring.boot</groupId>

??????? <artifactId>mybatis-spring-boot-starter</artifactId>

??????? <version>1.1.1</version>

????? </dependency>

????? <dependency>

??????? <groupId>mysql</groupId>

??????? <artifactId>mysql-connector-java</artifactId>

????? </dependency>

????? <dependency>

????? <!--pageHelper分頁(yè)插件 -->

??????? <groupId>com.github.pagehelper</groupId>

??????? <artifactId>pagehelper-spring-boot-starter</artifactId>

??????? <version>1.1.1</version>

????? </dependency>

????? <!-- MyBatis提供了攔截器接口,我們可以實(shí)現(xiàn)自己的攔截器,將其作為一個(gè)plugin裝入到SqlSessionFactory中。 Github上有位開發(fā)者寫了一個(gè)分頁(yè)插件,我覺得使用起來還可以,挺方便的。

??????? Github項(xiàng)目地址:https://github.com/pagehelper/Mybatis-PageHelper -->

????? <!--<dependency>

??????? <groupId>com.github.pagehelper</groupId>

??????? <artifactId>pagehelper</artifactId>

??????? <version>4.1.0</version>

????? </dependency>-->

????? <!--tomcat 的支持. -->

????? <dependency>

??????? <groupId>org.springframework.boot</groupId>

??????? <artifactId>spring-boot-starter-tomcat</artifactId>

??????? <!--添加<scope>provided</scope>,因?yàn)?/span>provided表明該包只在編譯和測(cè)試的時(shí)候用 -->

??????? <scope>provided</scope>

????? </dependency>

????? <dependency>

??????? <!--jsp頁(yè)面支持 -->

??????? <groupId>org.apache.tomcat.embed</groupId>

??????? <artifactId>tomcat-embed-jasper</artifactId>

??????? <scope>provided</scope>

????? </dependency>

????? <dependency>

??????? <groupId>javax.servlet</groupId>

??????? <artifactId>jstl</artifactId>

????? </dependency>

????? <dependency>

??????? <groupId>org.springframework.boot</groupId>

??????? <artifactId>spring-boot-starter-log4j</artifactId>

??????? <!--這里需要指定版本,否則報(bào)錯(cuò)【有可能是spring-boot-1.5.4.RELEASE版本沒有管理log4j版本的原因】 -->

??????? <version>1.3.2.RELEASE</version>

????? </dependency>

?

????? <!--spring boot集成Swagger2-->

????? <dependency>

??????? <groupId>io.springfox</groupId>

??????? <artifactId>springfox-swagger2</artifactId>

??????? <version>2.6.1</version>

????? </dependency>

????? <dependency>

??????? <groupId>io.springfox</groupId>

??????? <artifactId>springfox-swagger-ui</artifactId>

??????? <version>2.6.1</version>

????? </dependency>

????? <!--devtools可以實(shí)現(xiàn)頁(yè)面熱部署(即頁(yè)面修改后會(huì)立即生效,這個(gè)可以直接在application.properties文件中配置spring.thymeleaf.cache=false或者視圖解析器設(shè)置緩存為false來實(shí)現(xiàn)),

??????? 實(shí)現(xiàn)類文件熱部署(類文件修改后不會(huì)立即生效,待編譯后生效),實(shí)現(xiàn)對(duì)屬性文件的熱部署。devtools會(huì)監(jiān)聽classpath下的文件變動(dòng),并且會(huì)立即重啟應(yīng)用(發(fā)生在保存時(shí)機(jī)),注意:因?yàn)槠洳捎玫奶摂M機(jī)機(jī)制,該項(xiàng)重啟是很快的 -->

????? <dependency>

??????? <groupId>org.springframework.boot</groupId>

??????? <artifactId>spring-boot-devtools</artifactId>

??????? <optional>true</optional>

??????? <!--optional=true,依賴不會(huì)傳遞,該項(xiàng)目依賴devtools;之后依賴SpringBoot1項(xiàng)目的項(xiàng)目如果想要使用devtools,需要重新引入 -->

????? </dependency>

?? </dependencies>

?? <build>

????? <plugins>

??????? <plugin>

?????????? <!--配置spring bootmaven插件 -->

?????????? <groupId>org.springframework.boot</groupId>

?? ??????? <artifactId>spring-boot-maven-plugin</artifactId>

?????????? <!--<configuration> fork : 如果沒有該項(xiàng)配置,devtools不會(huì)起作用,即應(yīng)用不會(huì)restart 【實(shí)測(cè):可以不配置】

????????????? <fork>true</fork></configuration> -->

??????? </plugin>

????? </plugins>

?? </build>

</project>

2.3????application.properties配置:集成pageHelper,指定mapper.xml路徑

# mysql

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# 使用druid連接池? 需要注意的是:spring.datasource.type舊的spring boot版本是不能識(shí)別的。

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

# mybatis

mybatis.type-aliases-package=com.wyait.boot.pojo

mybatis.mapper-locations=classpath:mapper/*.xml

# 通用mapper配置

#mapper.mappers=com.wyait.boot.dao

#mapper.not-empty=false

#mapper.identity=MYSQL

# pagehelper

pagehelper.helperDialect=mysql

pagehelper.reasonable=true

pagehelper.supportMethodsArguments=true

pagehelper.returnPageInfo=check

pagehelper.params=count=countSql

2.4????UserMapper

TODO 詳見項(xiàng)目

mybatis-spring-boot項(xiàng)目源碼地址:https://git.oschina.net/wyait/springboot1.5.4.git


在接口上添加@Mapper注解即可或者在Application上添加掃描:@MapperScan(basePackages = "com.wyait.boot.dao")

2.5????啟動(dòng),測(cè)試結(jié)果

3????? SQL語(yǔ)句集成到UserMapperXML接口類中

寫法:

@Mapper

public interface UserMapperXML {

? @Select("SELECT * FROM USERWHERE NAME = #{name}")

? public UserfindUser(@Param("name") String name);

?

? @Select("SELECT * FROMUSER")

? public List<User>findAllUser();

?

? /**

? ?*

? ?* @描述:更新用戶信息

? ?* @創(chuàng)建人:wyait

? ?* @創(chuàng)建時(shí)間:2017629下午1:33:09

? ?* @param user

? ?* @return

? ?*/

? @Update("update user setage=#{age} where id=#{id}")

? public int update(User user);

}

?

更多用法可進(jìn)行百度。

4 ???? 總結(jié)

項(xiàng)目:mybatis-spring-boot整合了Mapper接口分離Sqlxml中的寫法和注解sql寫法。詳見項(xiàng)目源碼。


spring-boot相關(guān)項(xiàng)目源碼,

碼云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址https://github.com/wyait/spring-boot-1.5.4.git

?

spring boot系列文章:

spring boot 1.5.4 概述(一)

spring boot 1.5.4 入門和原理(二)

spring boot 1.5.4 之web開發(fā)(三)

spring boot 1.5.4 整合JSP(四)

spring boot 1.5.4 集成devTools(五)

spring boot 1.5.4 集成JdbcTemplate(六)

spring boot 1.5.4 集成spring-Data-JPA(七)

spring boot 1.5.4 配置文件詳解(八)

spring boot 1.5.4 統(tǒng)一異常處理(九)

spring boot 1.5.4 定時(shí)任務(wù)和異步調(diào)用(十)

spring boot 1.5.4 整合log4j2(十一)

spring boot 1.5.4 整合 mybatis(十二)

spring boot 1.5.4 整合 druid(十三)

spring boot 1.5.4 之監(jiān)控Actuator(十四)

spring boot 1.5.4 整合webService(十五)

spring boot 1.5.4 整合redis、攔截器、過濾器、監(jiān)聽器、靜態(tài)資源配置(十六)

spring boot 1.5.4 整合rabbitMQ(十七)

spring boot 1.5.4 集成Swagger2構(gòu)建Restful API(十八)

spring boot 1.5.9 整合redis(十九



轉(zhuǎn)載于:https://blog.51cto.com/wyait/1969626

總結(jié)

以上是生活随笔為你收集整理的spring boot 1.5.4 整合 mybatis(十二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。