• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot集成mybatis实例代码

springboot 搞代码 4年前 (2022-01-05) 162次浏览 已收录 0个评论

本篇文章主要介绍了springboot集成mybatis实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

  org.mybatis.spring.bootmybatis-spring-boot-starter1.2.0

添加mysql驱动:

  mysqlmysql-connector-java

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

  com.alibabadruid1.0.28 com.alibabafastjson1.2.30

配置数据源,在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 searchAll(); } 

UserMapper.xml

   <!-- 字段与实体的映射 --> <!-- 根据条件查询,全部 --> select * from tab_user 

创建一个控制器,注入UserDao,测试一下可不可以查询数据了:

 @RestController public class TestController { @Resource UserDao userDao; @RequestMapping("/getusers") public String test() { List 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”来源gaodai#ma#com搞*!代#%^码$网:”111111″,”username”:”test1″},{“id”:3,”password”:”222222″,”username”:”test2″}]

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

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

以上就是springboot集成mybatis实例代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot集成mybatis实例代码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址