5、springcloud整合mybatis注解方式
1、上一篇学习了服务提供者provider,但是并不是单单就学习了服务提供者。中间还穿插使用了Hikari数据源和spring cloud整合mybatis。但是上篇使用mybatis时还是沿用了老的方式,需要配置mapper对应的xml文件。先来看看上篇使用mybatis的主要步骤
一、 pom.xml文件引用
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
二、application.yml配置文件加入mybtias配置项
mybatis:
mapperLocations: classpath:sc/provider/dao/*.xml
#configLocation: classpath:mybatis-config.xml
三、编写mapper文件user-mapper.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="sc.provider.dao.UserDao" >
<select id="getUser" parameterType="java.lang.Long" resultType="sc.provider.model.User">
select id, userName, age, position from t_user where id = #{id,jdbcType=INTEGER}
</select>
<select id="listUser" resultType="sc.provider.model.User">
select id, userName, age, position from t_user
</select>
<insert id="addUser" parameterType="sc.provider.model.User">
insert into t_user (
id, userName, age, position
) values (
#{id,jdbcType=INTEGER},
#{userName,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
#{position,jdbcType=VARCHAR}
)
</insert>
<update id="updateUser" parameterType="sc.provider.model.User">
update t_user set
userName = #{userName,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
position = #{position,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<delete id="deleteUser" parameterType="java.lang.Long">
delete from t_user
where id = #{id,jdbcType=INTEGER}
</delete>
</mapper>
四、编写UserDao.java
package sc.provider.dao;
import java.util.List;
import sc.provider.model.User;
public interface UserDao {
User getUser(Long id);
List<User> listUser();
int addUser(User user);
int updateUser(User user);
int deleteUser(Long id);
}
五、 在ProviderApplication.java添加@MapperScan(basePackages="sc.provider.dao")
经过上面五个步骤才能使用mybatis。本篇将和大家看看不能简化spring cloud 整合mybatis的步骤(在sc-eureka-client-provider工程上改造)
一、 依赖必不可少
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
二、 删除application.yml关于mybatis的配置
三、 删除mapper文件user-mapper.xml文件
四、改造UserDao.java类
package sc.provider.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import sc.provider.model.User;
@Mapper
public interface UserDao {
@Select(value="select id, userName, age, position from t_user where id = #{id,jdbcType=INTEGER}")
User getUser(Long id);
@Select(value="select id, userName, age, position from t_user")
List<User> listUser();
@Insert(value="insert into t_user (id, userName, age, position) values ( #{id,jdbcType=INTEGER},#{userName,jdbcType=VARCHAR},#{age,jdbcType=INTEGER},#{position,jdbcType=VARCHAR})")
int addUser(User user);
@Update(value="update t_user set userName = #{userName,jdbcType=VARCHAR},age = #{age,jdbcType=INTEGER},position = #{position,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER}")
int updateUser(User user);
@Delete(value=" delete from t_user where id = #{id,jdbcType=INTEGER}")
int deleteUser(Long id);
}
五、 @MapperScan注解必不可少
package sc.provider;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages="sc.provider.dao")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
经过以上步骤就把使用xml方式的mybatis改造成使用annotation方式的mybatis了。
2、启动注册中心sc-eureka-server,启动sc-eureka-client-provider-annotation(使用sc-eureka-client-provider项目改造),验证是否改造成功
方式一:
方式二:
圈住的名字是在application.yml配置的
3、使用postman方法相应restful接口,这里就不一一访问了,可以参考上一篇文章的访问方式
添加:
http://127.0.0.1:8300/user/addUser
查询:
http://127.0.0.1:8300/user/getUser/4
列表:
http://127.0.0.1:8300/user/listUser
更新:
http://127.0.0.1:8300/user/updateUser
删除:
http://127.0.0.1:8300/user/deleteUser/2
5、springcloud整合mybatis注解方式的更多相关文章
- Spring Boot整合Mybatis(注解方式和XML方式)
其实对我个人而言还是不够熟悉JPA.hibernate,所以觉得这两种框架使用起来好麻烦啊. 一直用的Mybatis作为持久层框架, JPA(Hibernate)主张所有的SQL都用Java代码生成, ...
- SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]
SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...
- MyBatis从入门到精通(第3章):MyBatis注解方式的基本使用
MyBatis 注解方式就是将 SQL 语句直接写在DAO层的接口上. 在黑马录制的2018年双元视频课:\08 SSM整合案例[企业权限管理系统]\07.订单操作 有使用MyBatis注解进行多表 ...
- SpringBoot数据访问之整合mybatis注解版
SpringBoot数据访问之整合mybatis注解版 mybatis注解版: 贴心链接:Github 在网页下方,找到快速开始文档 上述链接方便读者查找. 通过快速开始文档,搭建环境: 创建数据库: ...
- mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类
相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...
- springboot整合mybatis(注解)
springboot整合mybatis(注解) 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- SpringBoot 2.x (9):整合Mybatis注解实战
SSM框架再熟悉不过了,不过以前通常是使用XML写SQL语句 这里用SpringBoot整合Mybatis并且使用注解进行开发 依赖: <!-- Mybatis --> <depen ...
- MyBatis从入门到精通(五):MyBatis 注解方式的基本用法
最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 1. @Select 注解 1.1 使 ...
随机推荐
- go 复合数据类型
数组 数组是一个由固定长度的特定类型元素组成的序列,一个数组可以由零个或多个元素组成.因为数组的长度是固定的,因此在Go语言中很少直接使用数组. 数组声明方式: #第一种 ] int balance ...
- 帮助_NOI导刊2010提高(03)
题目描述 Bubu的书架乱成一团了!帮他一下吧! 他的书架上一共有n本书.我们定义混乱值是连续相同高度书本的段数.例如,如果书的高度是30,30,31,31,32,那么混乱值为3,30,32,32,3 ...
- rpm相关
rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐受到其他发行版的采用.RPM ...
- js 页面跳转 新窗口打开
页面跳转:Window.showModalDialog(url,width,height); 弹出一个html文档的模式对话框Parent.window.document.location.href ...
- centos7下的Firewalld
一.介绍 防火墙守护 firewalld 服务引入了一个信任级别的概念来管理与之相关联的连接与接口.它支持 ipv4 与 ipv6,并支持网桥,采用 firewall-cmd (command) 或 ...
- 转载:tomcat过程原理
基于Java的Web 应用程序是 servlet.JSP 页面.静态页面.类和其他资源的集合,它们可以用标准方式打包,并运行在来自多个供应商的多个容器.Web 应用程序存在于结构化层次结构的目录中,该 ...
- 强大的性能监控pidstat
前言 pidstat 可以监控单个任务.比如CPU.内存.IO.上下文切换.详细参考 man pidstat 安装 yum install sysstat 使用 1.监控所有活动进程 pidstat ...
- tomcat如何正确的开启远程调试功能(转)
转自:http://blog.csdn.net/mhmyqn/article/details/49209541 版权声明:本文为博主原创文章,未经博主允许不得转载. 在日常开发中,有时需要对远程服务器 ...
- python中map的排序以及取出map中取最大最小值
map排序: 1.按key排序: items=dict.items() items.sort() sorted(dict.items(),key=lambda x:x[0],reverse=False ...
- 数据库索引(BTree索引和Hash索引)
索引 索引是为了方便查找我们所需要的数据. mysql支持的索引数据类型 B-Tree索引的特点 B-Tree索引以B+Tree(树)的结构存储数据. B-Tree索引能够加快数据的查询速度: B-T ...