欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot集成mybatis实例代码

程序员文章站 2024-02-29 18:53:52
springboot如何配置web项目请参考前一章,在此基础上集成mybatis。 在pom文件中添加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 'x'
    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,启动成功后访问:http://localhost:9000/demo/getusers,输出内容如下:

复制代码 代码如下:

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

至此,springboot已完成对mybatis的集成。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。