使用springboot如何實(shí)現(xiàn)對(duì)mybatis集成

本篇文章為大家展示了使用springboot如何實(shí)現(xiàn)對(duì)mybatis集成,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶(hù)提供川西大數(shù)據(jù)中心 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。

在pom文件中添加mybatis的依賴(lài):

  <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.2.0</version>
  </dependency>

添加MySQL驅(qū)動(dòng):

  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
  </dependency>

添加druid和fastjson依賴(lài),使用阿里巴巴druid連接池

  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.28</version>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.30</version>
  </dependency>

配置數(shù)據(jù)源,在application.yml中:

spring:
  datasource:
    name: test
    url: jdbc:mysql://127.0.0.1:3306/test
    username: root
    password: 111111
    # 使用druid數(shù)據(jù)源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

設(shè)置mybatis的mapper和model掃描路徑:

mybatis:
  mapperLocations: classpath:mapper/*.xml
  typeAliasesPackage: com.yingxinhuitong.demo.model
#更多配置請(qǐng)參見(jiàn):http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

接下來(lái)我們新建userMapper.xml,UserEntity以及UserDao:

UserEntity.class

package com.yingxinhuitong.demo.model;

/**
 * Created by jack on 2017/4/20.
 */
public class UserEntity {
 private Long id;
 private String username;
 private String password;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }
}

UserDao

package com.yingxinhuitong.demo.dao;

import com.yingxinhuitong.demo.model.UserEntity;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * Created by jack on 2017/4/20.
 */
@Mapper
public interface UserDao {
 List<UserEntity> searchAll();
}

UserMapper.xml

<&#63;xml version="1.0" encoding="UTF-8" &#63;>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yingxinhuitong.demo.dao.UserDao" >
 <!-- 字段與實(shí)體的映射 -->
 <resultMap id="BaseResultMap" type="com.yingxinhuitong.demo.model.UserEntity">
  <id column="id" property="id" jdbcType="BIGINT" />
  <result column="username" property="username" jdbcType="VARCHAR" />
  <result column="password" property="password" jdbcType="VARCHAR" />
 </resultMap>
 <!-- 根據(jù)條件查詢(xún),全部 -->
 <select id="searchAll" resultMap="BaseResultMap">
  select * from tab_user
 </select>

</mapper>

創(chuàng)建一個(gè)控制器,注入U(xiǎn)serDao,測(cè)試一下可不可以查詢(xún)數(shù)據(jù)了:

@RestController
public class TestController {

 @Resource
 UserDao userDao;

 @RequestMapping("/getusers")
 public String test() {
  List<UserEntity> users = userDao.searchAll();
  String usersJson = JSON.toJSONString(users);
  return usersJson;
 }
}

運(yùn)行Application.class,啟動(dòng)成功后訪問(wèn):http://localhost:9000/demo/getusers,輸出內(nèi)容如下:

復(fù)制代碼 代碼如下:

[{"id":1,"password":"000000","username":"test"},{"id":2,"password":"111111","username":"test1"},{"id":3,"password":"222222","username":"test2"}]

至此,springboot已完成對(duì)mybatis的集成。

上述內(nèi)容就是使用springboot如何實(shí)現(xiàn)對(duì)mybatis集成,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站題目:使用springboot如何實(shí)現(xiàn)對(duì)mybatis集成
分享網(wǎng)址:http://muchs.cn/article22/jcphcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站維護(hù)靜態(tài)網(wǎng)站、網(wǎng)站收錄、外貿(mào)建站域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)