微服务+mybaits-plus+layui分页对数据库进行增删改查

新建pom项目,引入依赖


        org.springframework.boot
        spring-boot-starter-parent
        1.5.10.RELEASE
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Edgware.SR2
                pom
                import
            
        
    

新建子模块SPRINGCLOUD_EUREKASERVER:注册中心
main方法:启动注册中心

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerMain {

    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaServerMain.class).web(true).run(args);
    }

}

引入依赖


        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
        
    
    /**
    	将注册中心打包到Linux系统上所需要的插件
    **/
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

注册中心resources配置文件:application.yml

eureka:
  instance:
    preferIpAddress: true #将来注册到注册中心的微服务包括注册中心都是使用IP地址
    hostname: localhost
  client:
    registerWithEureka: false #是否注册到注册中心 注册中心自己不用注册
    fetchRegistry: false #是否抓取注册中心的注册信息
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/ #微服务和客户端用来注册和发现的地址
server:
  port: 8761

新建子模块SPRINGCLOUD_USERSERVICE:服务端
实体类

package cn.ps.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;

public class User{
	
	@TableId(type=IdType.AUTO)
    private int id;
	@TableField("username")
    private String userName;
	
	@TableField("usersex")
    private String userSex;
	
	@TableField("userage")
    private String userAge;
	
	@TableField("userphone")
    private String userPhone;
	
	@TableField("usermoney")
    private String userMoney;
	
	@TableField("useremail")
    private String userEmail;
    
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserSex() {
		return userSex;
	}
	public void setUserSex(String userSex) {
		this.userSex = userSex;
	}
	public String getUserAge() {
		return userAge;
	}
	public void setUserAge(String userAge) {
		this.userAge = userAge;
	}
	public String getUserPhone() {
		return userPhone;
	}
	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}
	public String getUserMoney() {
		return userMoney;
	}
	public void setUserMoney(String userMoney) {
		this.userMoney = userMoney;
	}
	public String getUserEmail() {
		return userEmail;
	}
	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	} 
}
public class Result {
    private int code;
    private String msg;
    private int count;
    private List data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }

接口映射类

@Mapper
public interface UserMapper extends BaseMapper{

}

service层

@RestController
public class UserServiceImpl{
	@Autowired
	private UserMapper userMapper;
	@GetMapping("/users")
	public Result queryUser(String userName,String userMoney,String curPage, String numPage) {
		//第一次访问没有当前页
		if(StringUtils.isEmpty(curPage)) {
			curPage="1";
		}
		if(StringUtils.isEmpty(numPage)) {
			numPage="10";
		}
		QueryWrapper qw=new QueryWrapper();
		if(StringUtils.isNotEmpty(userName)) {
			qw.like("username", userName);
		}
		if(StringUtils.isNotEmpty(userMoney)) {
			qw.eq("usermoney", userMoney);
		}
		
		//转换成int类型
		int cur=Integer.parseInt(curPage);
		int num=Integer.parseInt(numPage);
		IPage pageData=userMapper.selectPage(new Page(cur,num), qw);
		Result result=new Result();
		result.setCode(0);
		result.setCount((int)pageData.getTotal());
		result.setData(pageData.getRecords());
		return result;
	}
	@PostMapping("/user")
	public void addUser(@RequestBody User user) {
		userMapper.insert(user);
	}
	@DeleteMapping("/user/{id}")
	public void deleteUser(@PathVariable("id") int id) {
		userMapper.deleteById(id);
	}
	@PutMapping("/user/{id}")
	public void updateUser(@RequestBody User user) {
		userMapper.updateById(user);
	}

mybaits-plus分页插件

@EnableTransactionManagement
@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")
public class MybaitsPlusConfgin {
		  /**
		   * 分页插件
		   */
		  @Bean
		  public PaginationInterceptor paginationInterceptor() {
		      return new PaginationInterceptor();
		  }
}

main方法:启动服务端微服务

@SpringBootApplication
@EnableEurekaClient
@RestController
public class UserServerMain {
    public static void main(String [] args){
        new SpringApplicationBuilder(UserServerMain.class).web(true).run(args);
    }
}

服务端resources配置文件:application.yml

eureka:
  instance:
    preferIpAddress: true  #将来注册到注册中心的微服务包括注册中心都是用ip地址
    hostname: localhost
  client:
    registerWithEureka: true  #是否注册到注册中心 注册中心自己不需要注册
    fetchRegistry: false  #是否抓取注册中的注册信息
    serviceUrl:
      defaultZone: http://192.168.88.131:8761/eureka/   #微服务和客户端用来注册和发现的地址
spring:
  application:
    name: userservice
  datasource:
    url: jdbc:mysql://localhost/my_mysql
    password: 789456123
    driver-class-name: com.mysql.jdbc.Driver
    username: tan_tan
server:
  port: 8880

引入依赖:服务端微服务


        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.cloud
            spring-cloud-starter-eureka
        

        
            mysql
            mysql-connector-java
        

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
    		com.baomidou
    		mybatis-plus-boot-starter
    		3.0.6
		

        
        	commons-lang
        	commons-lang
        	2.2
    	
		
		
   			com.alibaba
   			druid-spring-boot-starter
   			1.1.10
		
    

新建子模块SPRINGCLOUD_HTML:客户端

控制层

@RestController
public class UserController {

    @Autowired
    private UserFeign userFeign;
	@GetMapping(value = "/users")
	public Result queryUser(String userName,String userMoney,String page,String limit){
		try {
			Result pb=userFeign.queryUser(userName,userMoney, page, limit);
			return pb;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@PostMapping(value = "/user")
	public Result addUser(User user){
		Result result=new Result();
		try {
			userFeign.addUser(user);
		}catch (Exception e){
			result.setCode(1);
			result.setMsg("新增出错"+e.getMessage());
		}
		return result;
	}

	@DeleteMapping(value = "/user/{id}")
	public Result deleteUser(@PathVariable int id){
		Result result=new Result();
			try {
				userFeign.deleteUser(id);
			}catch (Exception e){
				result.setCode(1);
				result.setMsg("删除失败"+e.getMessage());
			}
		return result;
	}
	
	@PutMapping(value = "/user/{id}")
	public Result updateUser(@PathVariable("id") int id,User user){
		Result result=new Result();
		user.setId(id);
		try {
			userFeign.updateUser(id,user);
		}catch (Exception e){
			result.setCode(1);
			result.setMsg("修改失败"+e.getMessage());
		}
		return result;
	}

实体类

public class User{

	@TableId(type=IdType.AUTO)
    private int id;
	@TableField("username")
    private String userName;
	
	@TableField("usersex")
    private String userSex;
	
	@TableField("userage")
    private String userAge;
	
	@TableField("userphone")
    private String userPhone;
	
	@TableField("usermoney")
    private String userMoney;
	
	@TableField("useremail")
    private String userEmail;
    
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserSex() {
		return userSex;
	}
	public void setUserSex(String userSex) {
		this.userSex = userSex;
	}
	public String getUserAge() {
		return userAge;
	}
	public void setUserAge(String userAge) {
		this.userAge = userAge;
	}
	public String getUserPhone() {
		return userPhone;
	}
	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}
	public String getUserMoney() {
		return userMoney;
	}
	public void setUserMoney(String userMoney) {
		this.userMoney = userMoney;
	}
	public String getUserEmail() {
		return userEmail;
	}
	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}
}
public class Result {
    private int code;
    private String msg;
    private int count;
    private List data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public List getData() {
        return data;
    }
    
    public void setData(List data) {
        this.data = data;
    }
}

新建接口:UserFeign

@FeignClient(name="USERSERVICE")
public interface UserFeign {
	@GetMapping("/users")
	public Result queryUser(@RequestParam("userName") String userName,@RequestParam("userMoney")String userMoney,@RequestParam("curPage") String curPage,@RequestParam("numPage") String numPage);
	
	@PostMapping("/user")
    public Result addUser(@RequestBody User user);
	
	@DeleteMapping("/user/{id}")
	public Result deleteUser(@PathVariable("id") int id);
	
	@PutMapping("/user/{id}")
	public Result updateUser(@PathVariable("id") int id,@RequestBody User user);
}

main方法:启动客户端微服务

@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
@EnableEurekaClient
@EnableFeignClients
public class UserUiMain {

    public static void main(String [] args){
        new SpringApplicationBuilder(UserUiMain.class).web(true).run(args);
    }
}

客户端resources配置文件:application.yml

eureka:
  instance:
    preferIpAddress: true 
    hostname: localhost
  client:
    registerWithEureka: true 
    fetchRegistry: true 
    serviceUrl:
      defaultZone: http://192.168.88.131:8761/eureka/ 
spring:
  application:
    name: userui 
server:
  port: 80

引入依赖:客户端微服务


        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
    		com.baomidou
    		mybatis-plus-boot-starter
    		3.0.6
		
    

客户端微服务中 webapp下新建resource,引入layui
客户端微服务中 webapp下引入jquery
客户端微服务中 webapp下编写html页面
HTML




    
    商品列表
    
    
    
    


你可能感兴趣的:(微服务+mybaits-plus+layui分页对数据库进行增删改查)