Springboot集成MyBatis进行开发
- 引入相关的依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--引⼊springboot的web⽀持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入MySQL的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--引入mybatis整合springboot所使用的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!--springboot连接数据库的驱动jar-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
2.配置application.yml
# spring整合MyBatis配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource #连接所使用的数据源
driver-class-name: com.mysql.jdbc.Driver #所使用的驱动类
url: jdbc:mysql://locahost:3306/mybatis1?charsetEncoding=UTF-8
username: root
password: root # 配置mapper文件所在的包
mybatis:
mapper-locations: classpath:com/wd/mapper/*.xml
application.java
//声明当前文件是一个springboot应用的入口类
@SpringBootApplication //修饰范围:用在类上 标识是springboot的入口类 这个注释只能出现一次
@MapperScan("com.wd.dao") //用来指定Dao接口所在的位置
public class Application {
public static void main(String[] args) {
//运行springboot的核心方式
SpringApplication.run(Application.class,args);
}
}
public interface Userdao {
List<user> findAllUser();
user findUserById(@Param("id") int id);
}
mapper文件:编写方法的实现
<mapper namespace="com.wd.dao.Userdao">
<select id="findAllUser" resultType="com.wd.entity.user">
select * from t_user
</select>
<select id="findUserById" resultType="com.wd.entity.user">
select * from t_user where id=#{id}
</select>
</mapper>
public interface UserService {
List<user> findAllUser();
user findUserById(int id);
}
serviceImpl:service接口的实现类,实现接口
@Service //代表在工厂中创建有个service对象
@Transactional //当前类是支持事务的
public class UserServiceImpl implements UserService{
@Resource
private Userdao userDao;
@Override
public List<user> findAllUser() {
return userDao.findAllUser();
}
@Override
public user findUserById(int id) {
return userDao.findUserById(id);
}
}
@RestController
@RequestMapping("/user")
public class usercontroller {
@Resource
private UserService userService;
@RequestMapping("/users")
public List<user> findUsers(){
System.out.println("************");
return userService.findAllUser();
}
/**
* 查询单个用户方法
* @return:单个用户对象
* @PathVaribale:代表接收路径中所包含的参数
*/
@RequestMapping("/users/{id}")
public user findUserById(@PathVariable("id") int id){
System.out.println("id="+ id);
return userService.findUserById(id);
}
}
@RequestMapping("/users/{id}") public user findUserById(@PathVariable("id") int id){ }
Springboot集成MyBatis进行开发的更多相关文章
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- SpringBoot集成MyBatis的Bean配置方式
SpringBoot集成MyBatis的Bean配置方式 SpringBoot是一款轻量级开发的框架,简化了很多原先的xml文件配置方式,接下来就介绍一下如何不适用XML来配置Mybatis spri ...
- 0120 springboot集成Mybatis和代码生成器
在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...
- SpringBoot集成MyBatis底层原理及简易实现
MyBatis是可以说是目前最主流的Spring持久层框架了,本文主要探讨SpringBoot集成MyBatis的底层原理.完整代码可移步Github. 如何使用MyBatis 一般情况下,我们在Sp ...
- SpringBoot集成MyBatis小记
SpringBoot集成MyBatis小记 参考MyBatis官网 1. 添加maven依赖 添加到pom.xml <dependency> <groupId>org.myba ...
- Dataway与SpringBoot集成干掉后台开发
Dataway与SpringBoot集成干掉后台开发 Dataway让SpringBoot不在需要Controller.Service.DAO.Mapper了. 第一步:引入相关依赖 <depe ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
- SpringBoot 集成Mybatis 连接Mysql数据库
记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- SpringBoot集成Mybatis并具有分页功能PageHelper
SpringBoot集成Mybatis并具有分页功能PageHelper 环境:IDEA编译工具 第一步:生成测试的数据库表和数据 SET FOREIGN_KEY_CHECKS=0; ...
随机推荐
- 个人PSP(四则运算)升级
源代码管理平台Gitbee地址:https://gitee.com/chen-haijin/ 1.题目要求:能自动生成小学四则运算题目,且每一道题目的运算结果不能为负.除了支持整数运算外,还要支持真分 ...
- Windows下安装多个Redis实例
1.在Redis 目录下复制redis.windows-service.conf文件,建议命名规则redis.windows-service-port.conf,我们以6380端口为例. 2.打开re ...
- cesium 學習計劃
上篇已经将cesium环境搭建完成,并且服务启动完成,进去后主要浏览 documents 文档和Sandcastle 示例. 学习计划:沙盒示例学习一遍,每个示例中的查看对应代码接口. 学习cesiu ...
- [Leetcode 787]中转K站内最便宜机票
题目 n个城市,想求从src到dist的最廉价机票 有中转站数K的限制,即如果k=5,中转10次机票1000,中转5次机票2000,最后返回2000 There are n cities connec ...
- Mysql5.7的安装与卸载与数据迁移
Mysql5.7的安装与卸载 安装: 1.安装的时候 只选择安装 server即可 2.安装过程中,一定要重新选择安装路径和各个日志的路径,将来会非常大,不适合存在系统盘下面 3.安装时,取消MySQ ...
- 初学,Markdown的使用
Markdown学习 一级标题:"#"+空格+"标题" 二级标题 二级标题:"##"+空格+"标题" 三级标题 三级标题 ...
- JS中报错处理 try catch finally的使用
JS中标准报错处理通过 try catch finally ,使用格式 try { } catch (err) { } finally { } 代码1: try { console.log('顺序 1 ...
- Python项目案例开发从入门到实战-1.4Python图形界面设计
Python提供了多个图形开发界面的库,常用的Python GUI库如下. -Tkinter:Python内置模块 -wxPython -Jython 1.4.1创建Windows窗口 import ...
- SQL中通过表字段名称查询对应表名称
select * from sys.objects as a where a.object_id in(select [OBJECT_ID] from sys.all_columns where na ...
- win10,在桌面点击右键:显示设置和个性化,出现“该文件没有与之关联的应用来执行该操作,请安装应用,若已经安装应用,请在默认应用设置页面中创建关联”
参考:https://zhidao.baidu.com/question/2076100681854702028.html 1. WIN + R 打开运行,并输入 regedit,点击确定,进入注册表 ...