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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM