欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot項(xiàng)目整合mybatis的方法步驟與實(shí)例

 更新時(shí)間:2019年03月13日 11:22:06   作者:雙斜杠少年  
今天小編就為大家分享一篇關(guān)于SpringBoot項(xiàng)目整合mybatis的方法步驟與實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

1. 導(dǎo)入依賴的jar包

springboot項(xiàng)目整合mybatis之前首先要導(dǎo)入依賴的jar包,配置pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.u</groupId>
  <artifactId>springboot-mybatis</artifactId>
  <version>1.0-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RELEASE</version>
  </parent>
  <properties>
    <start-class>com.us.Application</start-class>
    <mybatis.version>3.2.7</mybatis.version>
    <mybatis-spring.version>1.2.2</mybatis-spring.version>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
  </properties>
  <dependencies>
    <!--springboot-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--db-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>6.0.5</version>
    </dependency>
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
      <exclusions>
        <exclusion>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!--mybatis-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${mybatis-spring.version}</version>
    </dependency>
    <!--util-->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.4</version>
    </dependency>
  </dependencies>
</project>

2. 配置數(shù)據(jù)源

pom.xml配置完畢后需要配置數(shù)據(jù)源了。新建DBConfig類配置數(shù)據(jù)源,代碼如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.base.Preconditions;
@Configuration
public class DBConfig {
  @Autowired
  private Environment env;
  @Bean(name = "dataSource")
  public DruidDataSource dataSource() {
    final String url = Preconditions.checkNotNull(env.getProperty("ms.db.url"));
    final String username = Preconditions.checkNotNull(env.getProperty("ms.db.username"));
    final String password = env.getProperty("ms.db.password");
    final int maxActive = Integer.parseInt(env.getProperty("ms.db.maxActive", "200"));
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    dataSource.setMaxActive(maxActive);
    return dataSource;
  }
}

3. 添加數(shù)據(jù)庫連接信息

在配置文件application.properties中添加數(shù)據(jù)庫連接信息如下:

ms.db.url=jdbc:mysql://localhost:3306/dev?prepStmtCacheSize=517&cachePrepStmts=true&autoReconnect=true&characterEncoding=utf-8&allowMultiQueries=true
ms.db.username=root
ms.db.password=admin
ms.db.maxActive=500

4. 配置mybatis的SqlSessionFactoryBean

數(shù)據(jù)源配置完以后要配置mybatis的SqlSessionFactoryBean進(jìn)行掃描mapper,新建MyBatisConfig類代碼如下(classpath*:mapper/*.xml為mapper.xml文件路徑):

import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisConfig {
  @Autowired
  private DataSource dataSource;
  @Bean(name = "sqlSessionFactory")
  public SqlSessionFactoryBean sqlSessionFactory(ApplicationContext applicationContext) throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.setMapperLocations(applicationContext.getResources("classpath*:mapper/*.xml"));
    return sessionFactory;
  }
}

5. 配置MapperScannerConfigurer掃描dao層

然后配置MapperScannerConfigurer掃描dao層,新建類MyBatisScannerConfig代碼如下(注意與MyBatisConfig不要寫在一個(gè)類里):

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisScannerConfig {
  @Bean
  public MapperScannerConfigurer MapperScannerConfigurer() {
    MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
    mapperScannerConfigurer.setBasePackage("com.example.*.dao");
    mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
    return mapperScannerConfigurer;
  }
}

6. 開啟數(shù)據(jù)庫事務(wù)(必須)代碼如下

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
@Configuration
public class TransactionConfig implements TransactionManagementConfigurer{
  @Autowired
  private DataSource dataSource;
  @Bean(name = "transactionManager")
  @Override
  public PlatformTransactionManager annotationDrivenTransactionManager() {
    return new DataSourceTransactionManager(dataSource);
  }
}

7. 實(shí)戰(zhàn)

配置大致就是如此,然后就是新建java bean(省略,文章底部有源碼地址)

新建mapper.xml文件(省略,文章底部有源碼地址,關(guān)于mapper.xml 文件編寫的疑問可以看我以前的springmvc+mybatis 系列文章)

新建dao層。代碼如下:

import java.util.List;
import java.util.Map;
import com.example.base.model.User;
import com.example.config.MyBatisRepository;
public interface UserDao {
  public List<User> getList(Map<String,Object> map);
}

service層要在實(shí)現(xiàn)類上添加@service注解,代碼如下:

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.base.dao.UserDao;
import com.example.base.service.UserService;
@Service
public class UserServiceImpl implements UserService {
  @Autowired
  private UserDao userDao;
  public Object getList(Map<String, Object> map) {
    return userDao.getList(map);
  }
}

controller層也要加@controller注解代碼如下:

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.base.service.UserService;
import com.example.base.util.CommonUtil;
import com.example.demo.ServiceEmail;
@Controller
@RequestMapping(value = "/users")
public class UserController {
  @Autowired
  private UserService userService;
  @Autowired
  private ServiceEmail serviceEmail;
  /***
   * api :localhost:8099/users?id=99 localhost:8099/users
   * 
   * @param request
   * @return
   */
  @RequestMapping(method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
  @ResponseBody
  public ResponseEntity<?> list(HttpServletRequest request) {
    Map<String, Object> map = CommonUtil.getParameterMap(request);
    return new ResponseEntity<Object>(userService.getList(map), HttpStatus.OK);
  }
  }

然后在啟動(dòng)入口類中掃描定義的這些配置累(配置包名可卻省只寫部分包名)如下:

import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
@ComponentScan(basePackages ="com.example")
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
  }
  public static void main(String[] args) throws Exception {
    ApplicationContext ctx = SpringApplication.run(Application.class, args);
    String[] beanNames = ctx.getBeanDefinitionNames();
    Arrays.sort(beanNames);
    for (String beanName : beanNames) {
      System.out.println(beanName);
    }
  }
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

  • obix協(xié)議在java中的配置和使用詳解

    obix協(xié)議在java中的配置和使用詳解

    這篇文章主要給大家介紹了關(guān)于obix協(xié)議在java中的配置和使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • Java中實(shí)現(xiàn)List分隔成子List詳解

    Java中實(shí)現(xiàn)List分隔成子List詳解

    大家好,本篇文章主要講的是Java中實(shí)現(xiàn)List分隔成子List詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • MyBatisCodeHelperPro最新激活方法(有效方法)

    MyBatisCodeHelperPro最新激活方法(有效方法)

    這篇文章主要介紹了MyBatisCodeHelperPro最新激活方法親測(cè)有效,非常好用,小編今天以idea2021.2.1為例給大家詳細(xì)講解,需要的朋友可以參考下
    2022-08-08
  • ant打包jar文件腳本分享

    ant打包jar文件腳本分享

    本文介紹的ant腳本是用來打包jar文件,做完JAVA應(yīng)用一定會(huì)用到這個(gè),需要的朋友可以參考下
    2014-03-03
  • 根據(jù)URL下載圖片至客戶端、服務(wù)器的簡(jiǎn)單實(shí)例

    根據(jù)URL下載圖片至客戶端、服務(wù)器的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)硪黄鶕?jù)URL下載圖片至客戶端、服務(wù)器的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • 自定義@RequestBody注解如何獲取JSON數(shù)據(jù)

    自定義@RequestBody注解如何獲取JSON數(shù)據(jù)

    這篇文章主要介紹了自定義@RequestBody注解如何獲取JSON數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • java二維數(shù)組指定不同長度實(shí)例方法

    java二維數(shù)組指定不同長度實(shí)例方法

    在本篇內(nèi)容里小編給大家分享了一篇關(guān)于java二維數(shù)組指定不同長度實(shí)例方法,有興趣的朋友們可以學(xué)習(xí)下。
    2021-03-03
  • MyBatis加載映射文件和動(dòng)態(tài)代理的實(shí)現(xiàn)

    MyBatis加載映射文件和動(dòng)態(tài)代理的實(shí)現(xiàn)

    本文主要介紹了MyBatis加載映射文件和動(dòng)態(tài)代理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • IDEA中的.iml文件和.idea文件夾

    IDEA中的.iml文件和.idea文件夾

    這篇文章主要介紹了IDEA中的.iml文件和.idea文件夾,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Spring Boot實(shí)現(xiàn)對(duì)文件進(jìn)行壓縮下載功能

    Spring Boot實(shí)現(xiàn)對(duì)文件進(jìn)行壓縮下載功能

    在Web應(yīng)用中,文件下載功能是一個(gè)常見的需求,特別是當(dāng)你需要提供用戶下載各種類型的文件時(shí),本文將演示如何使用Spring Boot框架來實(shí)現(xiàn)一個(gè)簡(jiǎn)單而強(qiáng)大的文件下載功能,需要的朋友跟隨小編一起學(xué)習(xí)吧
    2023-09-09

最新評(píng)論