SpringBoot~整合mybatis+微服务注册

mybatis主要是将sql从代码中剥离出来,配置到配置文件(.xml)中的优秀框架。Springboot可以通过简单的配置,简化了传统的springmvc中很多的配置文件,就可以实现项目的构建。在分布服务的概念中,尤其适用于这种框架。

1.创建数据库表,存入简单数据。

id name
123 张三
02 李四

2.创建springboot项目pxApp

这里写图片描述

2.1配置pxApp项目的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="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.gbicc</groupId>
    <artifactId>pxApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>pxApp</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

    </properties>


    <dependencies>



        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

2.2通过application.properties配置数据源等

#springboot+mybatis
#jiazai mybatis peizhi
mybatis.config-location=classpath:mapper/config/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml

#bieming
#mybatis.type-aliases-package=com.pxAppInterface.entity
#shujuyuan
spring.datasource.url=jdbc:mysql://localhost:3062/px_user?useUnicode=true&characterEncoding=UTF-8
#shujuyuan qudong
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root

2.3配置mybatis-config.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <typeAlias alias="Integer" type="java.lang.Integer" />
        <typeAlias alias="Long" type="java.lang.Long" />
        <typeAlias alias="HashMap" type="java.util.HashMap" />
        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
        <typeAlias alias="ArrayList" type="java.util.ArrayList" />
        <typeAlias alias="LinkedList" type="java.util.LinkedList" />
    </typeAliases>
</configuration>

2.4配置*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="com.pxAppInterface.mapper.UserMapper" >
  <resultMap id="user" type="com.pxAppInterface.entity.User" >
    <result column="id" property="Id" jdbcType="VARCHAR" />
    <result column="name" property="Name" jdbcType="VARCHAR" />
  </resultMap>
    <select id="getUserp" resultType="com.pxAppInterface.entity.User" parameterType="map">
        SELECT * FROM user where id=#{Id} 
    </select>

    <select id="getUserpCount" resultType="java.lang.Integer">
        SELECT count(*) FROM user
    </select>
</mapper>

service,dao User类都省略不写了,与以往项目没什么不同。

3.运行及访问

springboot 的强大之处,也体现在了这儿。项目启动无需在tomcat等服务器中,直接通过运行3.2的主函数方法,既可以启动成功。
访问时的url路径,也要安全简洁的多。只是采取这种方便的路径,Controller的接收参数格式,要做如3.1的改变。

3.1写Controller

package com.pxAppInterface.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.pxAppInterface.entity.ChapterTab;
import com.pxAppInterface.entity.User;
import com.pxAppInterface.service.ChapterTabService;
import com.pxAppInterface.service.UserService;


//相当于@Controller+@ResponseBody
@RestController
public class courseController {

    //注入service对象
    @Autowired
    private UserService userService;
    @Autowired
    private ChapterTabService chapterTabService;

    @RequestMapping("ssm")

    public User findAll(){
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("Id", "123");
        User u = userService.getUserp(map);
        return u;
    }
    //使用@RestController注解,返回String类型时,返回的就是字符串,不是页面
    @RequestMapping("/findJsp")
    public String yeMian(){
        return "test";
    }

    //访问路径http://localhost:8080/findAllFH/资产风险预警
    @RequestMapping("/findAllFH/{courseName}")
    public List<ChapterTab>  findAllFH( @PathVariable String courseName){
        Map<String, Object> map = new HashMap<String,Object>();
        Map<String, Object> map1 = new HashMap<String,Object>();
        map1.put("courseName", courseName);
        List<ChapterTab> chapterList = chapterTabService.getFH(map1);
        map.put("chapterList",chapterList);
        return chapterList;
    }
    //访问路径http://localhost:8080/findAllFH2/
    @RequestMapping("/findAllFH2")
    public List<ChapterTab>  findAllFH2(){
        Map<String, Object> map = new HashMap<String,Object>();
        Map<String, Object> map1 = new HashMap<String,Object>();
        map1.put("courseName", "资产风险预警");
        List<ChapterTab> chapterList = chapterTabService.getFH(map1);
        map.put("chapterList",chapterList);
        return chapterList;
    }
}

3.2 启动类

运行此方法,及可通过3.1中的路径访问,并查询出数据

package com.pxAppInterface;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@MapperScan("com.pxAppInterface.mapper")
public class PxAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(PxAppApplication.class, args);
    }
}

启动后的示例图:
访问findJsp 无法返回jsp页面,具体访问方法,请看下篇博客。
这里写图片描述
这里写图片描述

至此,一个整合mybatis的springboot项目就构建完毕了。
接下来我们来也“服务”。

4.构建服务项目testapp-servers

这里写图片描述
新建一个springboot项目,作为服务项目。

4.1 pom.xml

导入spring-cloud-starter-eureka-server和spring-cloud-dependencies

<?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/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.gbicc</groupId>
    <artifactId>testapp-servers</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>testapp-servers</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

4.2配置application.properties

配置端口和ip

server.port=8761

eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/

4.3启动该项目

package com.theserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class TestappServersApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestappServersApplication.class, args);
    }
}

4.4启动后访问

由于application.properties中配置的端口为8761,ip配置的为localhost,所以访问路径为http://localhost:8761/,此时可以看到,红色箭头标记处,并无服务已注册。启动成功。
这里写图片描述

5.注入服务

将本文2和2.1中建的pxApp项目,作为注入对象。

5.1 对pxApp项目的pom文件进行更改

1.加入spring-cloud-starter-eureka依赖

<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

2.设置spring-cloud-starter-eureka的版本

<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>

3.加入dependencyManagement

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type> 
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

4.更改好的完整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="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.gbicc</groupId>
    <artifactId>pxApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>pxApp</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
    </properties>


    <dependencies>
        <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>


        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type> 
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

5.2在application.properties进行更改

设置端口和服务名字,名字可以设置自己的项目名,也可以随意起。端口号是服务注册后,访问时要用到的,不要跟其他服务重复。我在这里将此次端口号设置为8387

#springboot+mybatis
#jiazai mybatis peizhi
mybatis.config-location=classpath:mapper/config/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml

#bieming
#mybatis.type-aliases-package=com.pxAppInterface.entity
#shujuyuan
spring.datasource.url=jdbc:mysql://120.199.82.51:3062/px_user?useUnicode=true&characterEncoding=UTF-8
#shujuyuan qudong
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root

#------------------update -------------------------
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

server.port=8387
spring.application.name=pxApp
#------------------update -------------------------

5.3给启动类加入@EnableDiscoveryClient注解

加入此注解后再启动,并访问刚刚的服务项目路径
http://localhost:8761/
此时发现pxAPP已被成功注入
这里写图片描述

6.访问pxApp服务下的ssm接口

eclipse可以停到pxApp项目,但是不要将testapp-servers项目停掉。
http://192.168.1.111:8387/ssm
这里写图片描述
访问成功。

ps:192.168.1.111为注册服务是的本地ip
这里写图片描述

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/han_xiaoxue/article/details/78730577
个人分类: java框架
上一篇loadrunner测试并发量并生成报告
下一篇数据库~neo4j的基本语句
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭