springboot/springcloud整合mybatis(mysql)
生活随笔
收集整理的這篇文章主要介紹了
springboot/springcloud整合mybatis(mysql)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、導入相關jar包
<!-- mysql客戶端 --> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version> </dependency> <!-- 阿里的數據庫連接池 --> <dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version> </dependency> <!-- spring boot對mybatis的支持 --> <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatis-springboot.version}</version> </dependency>2、配置application.yml文件
# config port server:port: 9009#config mysql spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localht:3306/test?useSSL=falseusername: rootpassword: rootapplication:name: demo-service-core#config mybatis mybatis:mapper-locations: classpath:mapper/*.xml # mybatis配置文件所在路徑config-location: classpath:mybatis.cfg.xmleureka:client:serviceUrl:defaultZone: http://${spring.cloud.client.ipAddress}:1111/eureka/,http://${spring.cloud.client.ipAddress}:1112/eureka/ ? ?# 注冊中心地址 ?EUREKA_HOMEinstance:hostname: ${spring.cloud.client.ipAddress}prefer-ip-address: true ? ? # 在地址欄上使用IP地址進行顯示instance-id: ${spring.cloud.client.ipAddress}:${server.port}3、編寫dao接口
/*** FileName: UserDao* Date: ? ? 2018/12/4 17:53* Description: 用戶數據庫操作接接口* History:* <author> ? ? ? ? ?<time> ? ? ? ? ?<version> ? ? ? ? ?<desc>* 作者姓名 ? ? ? ? ? 修改時間 ? ? ? ? ? 版本號 ? ? ? ? ? ? ?描述*/ package com.XXXX.demo.core.dao;import com.XXXX.demo.api.pojo.UserDo; import com.XXXX.demo.api.vo.UserVo; import com.XXXX.demo.common.interfaces.BaseDao; import org.springframework.stereotype.Repository;/*** 〈一句話功能簡述〉<br>?* @create 2018/12/4* @since 1.0.0*/ @Repository public interface UserDao extends BaseDao<UserDo, UserVo> {/*** 根據用戶名查詢用戶信息* @prama name 用戶名* @return UserVo 用戶實體Vo* @date 2018/12/5 16:47*/public UserVo findByName(String name);}
4、編寫dao.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="com.cnbi.demo.core.dao.UserDao"><!--userMap映射關系--><resultMap id="userMap" type="userVo"><id property="id" column="id"/><result property="name" column="name"/><result property="roleName" column="role_name"/><result property="password" column="password"/></resultMap><!--插入--><insert id="save" parameterType="userDo" useGeneratedKeys="true" keyProperty="id">INSERT INTOuser(id,name,password,role_name)VALUES(NULL,#{name},#{password},#{roleName})</insert><!--根據id查詢用戶信息--><select id="findById" resultMap="userMap" parameterType="long">SELECTid,role_name,name,passwordFROMuserWHEREid = #{id}</select><!--刪除--><delete id="delete" parameterType="long">DELETE FROMuserWHEREid = #{id}</delete><!--更新--><update id="update" parameterType="userDo">UPDATEuser<set><if test="roleName != null and roleName != ''">role_name = #{roleName},</if><if test="name != null and name !=''">name = #{name},</if><if test="password != null and password != ''">password = #{password},</if></set>WHEREid = #{id}</update><!--查詢所有用戶--><select id="findAll" resultMap="userMap">SELECTid,role_name,name,passwordFROMuser</select><!--根據用戶姓名查詢用戶信息--><select id="findByName" resultMap="userMap" parameterType="string">SELECTid,role_name,name,passwordFROMuserWHEREname = #{name}</select>
</mapper>
5、編寫service以及serviceimpl以及控制層controller
此處省略
6、啟動類添加注解
/*** FileName: ServiceCoreApplication* Date: ? ? 2018/12/5 8:42:42* Description:* History:* <author> ? ? ? ? ?<time> ? ? ? ? ?<version> ? ? ? ? ?<desc>* 作者姓名 ? ? ? ? ? 修改時間 ? ? ? ? ? 版本號 ? ? ? ? ? ? ?描述*/ package com.XXXX.demo.core;import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.ComponentScan;/*** ServiceCore啟動類* @create 2018/12/5 8:42:42* @since 1.0.0*/ @SpringBootApplication @MapperScan("com.XXXX.demo.core.dao") @ComponentScan("com.XXXX.demo") @EnableEurekaClient public class ServiceCoreApplication {public static void main(String[] args){SpringApplication.run(ServiceCoreApplication.class, args);}}
?
?
總結
以上是生活随笔為你收集整理的springboot/springcloud整合mybatis(mysql)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软考个人笔记
- 下一篇: mysql string长度限制_Str