gpt4 book ai didi

springboot集成mybatis实例代码

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章springboot集成mybatis实例代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

springboot如何配置web项目请参考前一章,在此基础上集成mybatis.

在pom文件中添加mybatis的依赖:

?
1
2
3
4
5
< dependency >
  < groupId >org.mybatis.spring.boot</ groupId >
  < artifactId >mybatis-spring-boot-starter</ artifactId >
  < version >1.2.0</ version >
</ dependency >

添加mysql驱动:

?
1
2
3
4
< dependency >
  < groupId >mysql</ groupId >
  < artifactId >mysql-connector-java</ artifactId >
</ dependency >

添加druid和fastjson依赖,使用阿里巴巴druid连接池 。

?
1
2
3
4
5
6
7
8
9
10
< 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中:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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扫描路径:

?
1
2
3
4
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 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<? 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,测试一下可不可以查询数据了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@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的集成.

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

原文链接:http://www.cnblogs.com/jiangkuan/p/6737131.html 。

最后此篇关于springboot集成mybatis实例代码的文章就讲到这里了,如果你想了解更多关于springboot集成mybatis实例代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com