首頁  >  文章  >  Java  >  詳解springboot整合mybatis的程式碼實例

詳解springboot整合mybatis的程式碼實例

Y2J
Y2J原創
2017-04-28 10:00:571586瀏覽

這篇文章主要介紹了springboot整合mybatis實例程式碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧

springboot如何設定web專案請參考前一章,在此基礎上整合mybatis。

在pom檔案中加入mybatis的依賴:

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

新增mysql驅動程式:

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

新增druid和fastjson依賴,使用阿里巴巴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>

設定資料來源,在application.yml:

spring:
  datasource:
    name: test
    url: jdbc:mysql://127.0.0.1:3306/test
    username: root
    password: 111111
    # 使用druid数据源
    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 &#39;x&#39;
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

設定mybatis的mapper和model掃描路徑:

mybatis:
  mapperLocations: classpath:mapper/*.xml
  typeAliasesPackage: com.yingxinhuitong.demo.model
#更多配置请参见:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

接下來我們新建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

<?xml version="1.0" encoding="UTF-8" ?>
<!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" >
 <!-- 字段与实体的映射 -->
 <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>
 <!-- 根据条件查询,全部 -->
 <select id="searchAll" resultMap="BaseResultMap">
  select * from tab_user
 </select>

</mapper>

建立一個控制器,注入UserDao,測試一下可不可以查詢資料了:

@RestController
public class TestController {

 @Resource
 UserDao userDao;

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

執行Application.class,啟動成功後存取:localhost:9000/demo/getusers,輸出內容如下:

 程式碼如下:

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

#至此,springboot已完成對mybatis的整合。

以上是詳解springboot整合mybatis的程式碼實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn