5、springcloud整合mybatis注解方式

公众号:java乐园

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

原文地址:https://blog.51cto.com/13538361/2388503

时间: 2024-08-28 07:40:50

5、springcloud整合mybatis注解方式的相关文章

MyBatis从入门到精通(第3章): MyBatis注解方式的基本使用

MyBatis 注解方式就是将 SQL 语句直接写在DAO层的接口上. 在黑马2018年录制的双元视频课:\08 SSM整合案例[企业权限管理系统]\07.订单操作  有使用mybatis注解进行多表关联查询的案例,在下文会有使用注解的补充说明. 这种方式的优点是 , 对于需求比较简单的系统,效率较高.缺点是 ,当 SQL 有变化时都需要重新编译代码, 一般情况下不建议使用MyBatis的注解方式 . 因此, (原书)本章不会进行深入讲解. 在 MyBatis 注解 SQL 中,最基本的就是@S

springboot整合mybatis(注解)

springboot整合mybatis(注解) 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ht

mybatis注解方式和xml方式的使用

Mybatis的注解方式的使用: Mybatis的xml的使用 原文地址:https://www.cnblogs.com/xjatj/p/9277426.html

Mybatis(三)MyBatis 注解方式的基本 用法

在 MyBatis注解 SQL 中,最基本的就是@Select.@Insert.@Update 和@Delete 四种. 原文地址:https://www.cnblogs.com/cuiqq/p/11155227.html

ehcache整合spring注解方式

一.简介 在hibernate中就是用到了ehcache 充当缓存.spring对ehcache也提供了支持,使用也比较简单,只需在spring的配置文件中将ehcache的ehcache.xml文件配置进去即可.在spring中使用ehcache有两种方式,一种是使用spring提供的封装,使用注解的方式配置在某个方法上面,第一次调用该方法的时候,将该方法执行返回的数据缓存,当再次执行的时候如果时间没有变,就直接冲缓存获取,该方法不执行:另一种方式是获取到ehcache本地接口,直接使用ehc

springMVC+freemarker+slf4j整合(基于注解方式)

springmvc在项目中使用较多,本文将基于spring注解springMVC+freemarker+slf4j整合在一起,便于开发,后续还会将ibatis整合进来. 第一步:使用编程工具建立web工程,本文使用eclipse + tomcat 7.0 + jdk 1.7. 第二步:引入工程使用到的jar文件: commons-logging-1.1.3.jar.freemarker-2.3.20.jar.logback-classic-1.0.9.jar.logback-core-1.0.9

mybatis 注解方式

在spring 的配置文件中添加配置 <!--配置Mapper扫描模式,所有Mapper都继承SqlMapper接口  -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.lovo.mapper" />        <p

springboot整合dubbo注解方式

工程结构: 主pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/PO

springMVC+freemarker+slf4j整合(基于注解方式)准尊篆遵祖宗

http://www.ebay.com/cln/3t9_vznf/2015-01-29/166763640011 http://www.ebay.com/cln/lbv_dlrh/2015-01-29/166763632011 http://www.ebay.com/cln/lvx_xhlb/2015-01-29/166613270010 http://www.ebay.com/cln/l53_nfpf/2015-01-29/166613253010 http://www.ebay.com/cl