SpringBoot+MyBatis连接数据库
SpringBoot通过MyBatis连接数据库有2种方法:
1.注解
2.XML文件
1.注解
1.构建项目
2.添加依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入starter-->
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MySQL的JDBC驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 引入第三方数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
</dependencies>
3.配置属性文件:
#mybatis.type-aliases-package=net.xdclass.base_project.domain
#可以自动识别
#spring.datasource.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/movie?useUnicode=true&characterEncoding=utf-8
spring.datasource.username =root
spring.datasource.password =password
#如果不使用默认的数据源 (com.zaxxer.hikari.HikariDataSource)
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
#控制台打印sql
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
4.启动类(SpringApplication)添加mapper扫描
@MapperScan("net.xdclass.base_project.mapper")
5.开发Mapper,Mapper类是访问数据库的接口
public interface UserMapper{
@Insert("Insert into user(name,phone,create_time,age) values(#{name},#{phone},#{createTime},#{age})")
int insert(User user);
}
添加@Options(userGeneratedKeys=true,keyProperty="id",keyColumn="id") //key Property java对象的属性,key Column表示数据库的字段
6.添加User用户(domain)
public class User {
private int id;
private String name;
private String phone;
private int age;
private Date createTime;
xxx(相应的get/set方法)
}
7.添加service层
UserService:
public interface UserService {
public int add(User user);
}
UserServiceImpl:
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public int add(User user) {
userMapper.insert(user);
int id = user.getId();
return id;
}
}
8.添加Controller层
@RestController
@RequestMapping("/api/v1/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("add")
public Object add(){
User user = new User();
user.setAge(11);
user.setCreateTime(new Date());
user.setName("xdclass");
user.setPhone("10010000");
int id = userService.add(user);
return JsonData.buildSuccess(id);
}
}
9.select,update, delete的使用
1.从数据转成对象
public interface userMapper{
@Select("select * from user")
@Results({
@Result(column="数据库字段1",property="对象的字段1"),
@Result(column="数据库字段2",property="对象的字段2")
})
List<User> getAll();
}
调用的时候(直接在controller给调用了):
@GetMapping("findAll")
public Object findAll(){
return JsonData.buildSuccess(userMapper.getAll());
}
JsonData类在同层目录下
2.根据id找对象(id怎么传进去-》findById调用的时候传入id)
public interface userMapper{
@Select("Select * from user where id=#{id}")
@Results({
@Result(column="数据库字段1",property="对象的字段1"),
@Result(column="数据库字段2",property="对象的字段2")
})
User findById(Long id);
}
3.更新数据库对象
public interface userMapper{
@Update("UPDATE user SET name=#{name} WHERE id =#{id}")
void update(User user);
}
4.删除数据库对象
public interface userMapper{
@Delete("DELETE FROM user WHERE id =#{userId}")
void delete(Long userId);
}
2.XML文件
https://blog.csdn.net/lr131425/article/details/76269236(这里的model类文件写错了)
主要区别在于怎么样实现Mapper层
XML的代码在https://github.com/Winster-cheng/SpringBoot-JDBC
SpringBoot+MyBatis连接数据库的更多相关文章
- 带着新人学springboot的应用01(springboot+mybatis+缓存 上)
上一篇结束,第一次做一个这么长的系列,很多东西我也是没有说到,也许是还没有想到,哈哈哈,不过基本的东西还是说的差不多了的.假如以后碰到了不会的,随便查查资料配置一下就ok. 咳,还有大家如果把我前面的 ...
- 3分钟搞定SpringBoot+Mybatis+druid多数据源和分布式事务
文章来自: https://blog.csdn.net/qq_29242877/article/details/79033287 在一些复杂的应用开发中,一个应用可能会涉及到连接多个数据源,所谓多数据 ...
- DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量
DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybati ...
- 第五章 springboot + mybatis(转载)
本编博客转发自:http://www.cnblogs.com/java-zhao/p/5350021.html springboot集成了springJDBC与JPA,但是没有集成mybatis,所以 ...
- 第九章 springboot + mybatis + 多数据源 (AOP实现)
在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...
- 第五章 springboot + mybatis
springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成.集成方式相当简单. 1.项目结构 2.pom.xml <!-- 与数 ...
- 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建
基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...
- Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦
个人博客 对于技术人员来说,拥有自己的个人博客应该是一件令人向往的事情,可以记录和分享自己的观点,想到这件事就觉得有意思,但是刚开始写博客的时候脑海中是没有搭建个人博客这一想法的,因为刚起步的时候连我 ...
- springboot mybatis 事务管理
本文主要讲述springboot提供的声明式的事务管理机制. 一.一些概念 声明式的事务管理是基于AOP的,在springboot中可以通过@Transactional注解的方式获得支持,这种方式的优 ...
随机推荐
- linux svn 自启动
.svn服务自启动脚本 把脚本放在/etc/init.d/下 vi /etc/rc.d/init.d/svn svn脚本内容: #!/bin/bash # chkconfig: - # descrip ...
- 解决Code First因_migrationHistory表与代码不一致的问题
我们在测试环境多人开发时,由于会存在多个测试.开发环境,但是大家共用一个数据库. 这时候会碰到一个问题,一旦有某个人通过Migration更新了数据库,其他环境在首次查询数据库的时候都会收到Dbcon ...
- typeof null 为什么等于 object?
之前只知道typeof null = object,但是却从来不知道是为什么.最新查阅资料的时候,看到了这个原理,记录下来,方便自己以后查看. 原理是这样的,不同的对象在底层都表示为二进制,在 Jav ...
- Struts2配置文件struts.xml的编辑自动提示代码功能
第一步:复制struts.xml头部地址 第二步:Window --->Preferences 第三步:XML--->XML Catalog--->Add 第四步:在Key中粘贴复制 ...
- C学习笔记(2)--指针
一.多文件结构总结 1.子源文件里面包含自己对应的头文件 2.无论是何源文件调用库函数,都需要包含该库函数的声明所在的头文件 3.头文件又叫接口文件,.c对数据和函数进行封装和包含, .h就是.c对外 ...
- easyUI-textbox回车获取不到正确的textbox值问题
//要先给文本框手动赋值之后才可以获取正确的文本框内容 var t = $('#tt'); t.textbox('textbox').bind('keydown', function(e){ if ( ...
- Service生命周期以及应用
Service概念及用途: Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行 ...
- 泛型通配符extends与super的区别
<? extends T>限定参数类型的上界:参数类型必须是T或T的子类型 <? super T> 限定参数类型的下界:参数类型必须是T或T的超类型 总结为: <? ex ...
- 大话JVM(一):垃圾收集算法
系列介绍|本系列主要是记录学习jvm过程中觉得重要的内容,方便以后复习 在说垃圾收集算法之前,先要说一下垃圾收集,从大的讲,垃圾收集需要考虑三件事情: 1.哪些内存需要回收 2.什么时候回收 3.如 ...
- xshell复制粘贴
用户看到这个标题肯定会觉得小编脑子坏掉了,复制粘贴不就是Ctrl+C,Ctrl+V嘛,但是在xshell却不尽然. 现象: 在xshell界面中需要用到之前的一段代码,自然是选中,熟练的键入Ctrl+ ...