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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
2.2通过application.properties配置数据源等
mybatis.config-location=classpath:mapper/config/mybatis-config.xmlmybatis.mapper-locations=classpath:mapper/*.xmlspring.datasource.url=jdbc:mysql://localhost:3062/px_user?useUnicode=true&characterEncoding=UTF-8spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.username=rootspring.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;@RestControllerpublic class courseController { @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; } @RequestMapping("/findJsp") public String yeMian(){ return "test"; } @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; } @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; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
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项目就构建完毕了。
宁波整形医院http://www.lyxcl.org/
宁波整形美容医院http://www.iyestar.com/
接下来我们来也“服务”。
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/> </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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
4.2配置application.properties
配置端口和ip
server.port=8761eureka.instance.hostname=localhosteureka.client.registerWithEureka=falseeureka.client.fetchRegistry=falseeureka.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@EnableEurekaServerpublic 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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
5.2在application.properties进行更改
设置端口和服务名字,名字可以设置自己的项目名,也可以随意起。端口号是服务注册后,访问时要用到的,不要跟其他服务重复。我在这里将此次端口号设置为8387
#springboot+mybatis#jiazai mybatis peizhimybatis.config-location=classpath:mapper/config/mybatis-config.xmlmybatis.mapper-locations=classpath:mapper/*.xml#bieming#mybatis.type-aliases-package=com.pxAppInterface.entity#shujuyuanspring.datasource.url=jdbc:mysql:#shujuyuan qudongspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.username=rootspring.datasource.password=root#------------------update -------------------------eureka.client.serviceUrl.defaultZone=http:server.port=8387spring.application.name=pxApp#------------------update -------------------------
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
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