搭建服务模块为了模拟正式开发环境,只是少写了service层直接在controller里面直接引用,直接上图和代码:更为方便:
创建完成之后加入配置:
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"><parent><artifactId>springcloud-parent</artifactId><groupId>com.cxy</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><packaging>jar</packaging><artifactId>spring-person</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><!--千万注意,不要写成spring-cloud-netflix-eureka-client,写成这样不会报错,但是注册不了服务--><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.41</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.5</version><dependencies><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.41</version></dependency></dependencies><executions><execution><id>mybatis generator</id><phase>package</phase><goals><goal>generate</goal></goals></execution></executions><configuration><!--允许移动生产的文件--><verbose>true</verbose><!--允许自动覆盖文件,在开发者不可以设置为true--><overwrite>true</overwrite><!--制定生产文件的位置--><configurationFile>src/main/resources/mybatis_generator.xml</configurationFile></configuration></plugin></plugins></build> </project>
这个插件中加入了自动生成代码插件:
mybatis_generator.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration><!--驱动包的路径--><!--<classPathEntry location="F:\maven\repos\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar"/>--><context id="DB2Tables" targetRuntime="MyBatis3"><!--注释--><commentGenerator><property name="suppressAllComments" value="true"/><property name="suppressDate" value="true"/></commentGenerator><!--数据库连接--><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://127.0.0.1:3306/fr_db?zeroDateTimeBehavior=convertToNull&autoReconnect=true&useUnicode=true&characterEncoding=utf-8" userId="root"password="1234"/><javaTypeResolver><property name="forceBigDecimals" value="false"/></javaTypeResolver><!--生成Model类存放位置--><javaModelGenerator targetPackage="com.cxy.dataObject" targetProject="src/main/java"><!--是否对model添加构造函数--><property name="constructorBased" value="false"/><!--是否允许子包--><property name="enableSubPackages" value="true"/><!--建立的model对象是否不可变,也就是生成的model没有setter方法--><property name="immutable" value="false"/><property name="trimStrings" value="false"/></javaModelGenerator><!--生成映射文件存放位置--><sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"><property name="enableSubPackages" value="true"/></sqlMapGenerator><!--生成Mapper类存放位置--><javaClientGenerator type="XMLMAPPER" targetPackage="com.cxy.dao" targetProject="src/main/java"><property name="enableSubPackages" value="true"/></javaClientGenerator><!--生成与表对应的类名--><table tableName="person" domainObjectName="PersonDo" enableCountByExample="false"enableDeleteByExample="false"enableSelectByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false"></table></context></generatorConfiguration>
yml文件:
eureka:client:serviceUrl:defaultZone: http://127.0.0.1:8761/eureka/ #eureka注册中心地址 spring:application:name: cxy-person-service #应用名#datasource,数据连接datasource:driver-class-name: com.mysql.jdbc.Driverurl : jdbc:mysql://127.0.0.1:3306/fr_db?zeroDateTimeBehavior=convertToNull&autoReconnect=true&useUnicode=true&characterEncoding=utf-8password: 1234username: root#mybatis mybatis:type-aliases-package: com.cxy.dataObject #实体类映射文件包mapper-locations: classpath:mapping/*.xml #生成的sql语句 server:port: 8081
笔者在这个文件配置时候出现很多问题,慢慢的解决了,
注意yml文件格式
启动类:
package com.cxy;import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/**** @ClassName: PersonApplication* @Description:* @Auther: 陈绪友* @Date: 2019/1/2816:30* @version : V1.0*/ @SpringBootApplication @EnableEurekaClient //开启注解,注册服务 @MapperScan("com.cxy.dao") public class PersonApplication {public static void main(String[] args) {SpringApplication.run(PersonApplication.class,args);} }
controller
package com.cxy.controller;import com.cxy.dao.PersonDoMapper; import com.cxy.dataObject.PersonDo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;/**** @ClassName: PersonController* @Description:* @Auther: 陈绪友* @Date: 2019/1/2816:31* @version : V1.0*/ @RequestMapping("/person") @RestController public class PersonController {@Autowiredprivate PersonDoMapper personDoMapper;@RequestMapping(value = "{id}",method = RequestMethod.GET)
public PersonDo selectPersonDoByid(@PathVariable Integer id){
return personDoMapper.selectByPrimaryKey(id); }
@RequestMapping(value = "{id}",method = RequestMethod.DELETE)
public Integer deletePersonDoByid(@PathVariable Integer id){
return personDoMapper.deleteByPrimaryKey(id); } }
dao
package com.cxy.dao;import com.cxy.dataObject.PersonDo; import org.apache.ibatis.annotations.Mapper;@Mapper public interface PersonDoMapper {int deleteByPrimaryKey(Integer id);int insert(PersonDo record);int insertSelective(PersonDo record);PersonDo selectByPrimaryKey(Integer id);int updateByPrimaryKeySelective(PersonDo record);int updateByPrimaryKey(PersonDo record); }
pojo:个人喜欢叫做数据库数据模型,在正式开发中和这个是不可以直接返回给前端的,是需要进行封装给前端的
po,对应的数据库数据模型
dto 返回给前端的数据模型,即就是页面展示的模型
vo 是前端传输过来给后台的模型
package com.cxy.dataObject;public class PersonDo {private Integer id;private String name;private Integer age;private String address;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;} }
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.cxy.dao.PersonDoMapper"><resultMap id="BaseResultMap" type="com.cxy.dataObject.PersonDo"><id column="id" jdbcType="INTEGER" property="id" /><result column="name" jdbcType="VARCHAR" property="name" /><result column="age" jdbcType="INTEGER" property="age" /><result column="address" jdbcType="VARCHAR" property="address" /></resultMap><sql id="Base_Column_List">id, name, age, address</sql><select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">select <include refid="Base_Column_List" />from personwhere id = #{id,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">delete from personwhere id = #{id,jdbcType=INTEGER}</delete><insert id="insert" parameterType="com.cxy.dataObject.PersonDo">insert into person (id, name, age, address)values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{address,jdbcType=VARCHAR})</insert><insert id="insertSelective" parameterType="com.cxy.dataObject.PersonDo">insert into person<trim prefix="(" suffix=")" suffixOverrides=","><if test="id != null">id,</if><if test="name != null">name,</if><if test="age != null">age,</if><if test="address != null">address,</if></trim><trim prefix="values (" suffix=")" suffixOverrides=","><if test="id != null">#{id,jdbcType=INTEGER},</if><if test="name != null">#{name,jdbcType=VARCHAR},</if><if test="age != null">#{age,jdbcType=INTEGER},</if><if test="address != null">#{address,jdbcType=VARCHAR},</if></trim></insert><update id="updateByPrimaryKeySelective" parameterType="com.cxy.dataObject.PersonDo">update person<set><if test="name != null">name = #{name,jdbcType=VARCHAR},</if><if test="age != null">age = #{age,jdbcType=INTEGER},</if><if test="address != null">address = #{address,jdbcType=VARCHAR},</if></set>where id = #{id,jdbcType=INTEGER}</update><update id="updateByPrimaryKey" parameterType="com.cxy.dataObject.PersonDo">update personset name = #{name,jdbcType=VARCHAR},age = #{age,jdbcType=INTEGER},address = #{address,jdbcType=VARCHAR}where id = #{id,jdbcType=INTEGER}</update> </mapper>
这个是代码生成命令的截图
启动服务:
进入注册中心查看:
在此服务注册就完成了,那么远程是如何进行调用的呢,下接将会整合opnfeign进行服务调用,当然传统的httpclient,restemplate都是可以进行服务调用的
传统的方法就不调用了,比较繁琐,