赞
踩
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.3</version>
</dependency>
package com.wpj.entity; import com.baomidou.mybatisplus.annotations.TableField; import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableName; import com.baomidou.mybatisplus.enums.IdType; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @AllArgsConstructor @NoArgsConstructor @TableName("t_user") public class User implements Serializable { @TableId(type = IdType.AUTO) private Integer id; private String username; private String password; private Integer sex; private String email; @TableField(exist = false) private User createUser; }
<dependency>
<groupId>com.wpj</groupId>
<artifactId>shop-entity</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
package com.wpj.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.wpj.entity.User;
public interface IUserMapper extends BaseMapper<User> {
}
<dependency>
<groupId>com.wpj</groupId>
<artifactId>shop-entity</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.wpj</groupId>
<artifactId>base</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
package com.wpj.service;
import com.wpj.entity.User;
public interface IUserService extends IBaseService<User> {
}
<dependency> <groupId>com.wpj</groupId> <artifactId>shop-service-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.wpj</groupId> <artifactId>shop-mapper</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency>
package com.wpj.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.baomidou.mybatisplus.mapper.BaseMapper; import com.wpj.entity.User; import com.wpj.mapper.IUserMapper; import com.wpj.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; @Service public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService { @Autowired private IUserMapper userMapper; @Override public BaseMapper<User> getMapper() { return userMapper; } }
package com.wpj; import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @DubboComponentScan(basePackages = "com.wpj.service") @MapperScan(basePackages = {"com.wpj.mapper"}) public class UserServiceApplication { public static void main(String[] args) { SpringApplication.run(UserServiceApplication.class, args); } }
spring:
datasource:
url: jdbc:mysql://localhost:3306/nz1904-springboot-shop
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
dubbo:
application:
name: user-service
registry:
address: zookeeper://47.98.33.215:2181
protocol:
port: -1
package com.wpj.service.impl; import com.wpj.entity.User; import com.wpj.service.IUserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest @RunWith(SpringRunner.class) public class TestService { @Autowired private IUserService userService; @Test public void testAdd(){ User user = new User(); user.setUsername("admin"); user.setPassword("admin"); user.setEmail("admin@qq.com"); user.setSex(1); int insert = userService.insert(user); System.out.println(insert); } }
flag默认1
<dependency>
<groupId>com.wpj</groupId>
<artifactId>shop-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
package com.wpj.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.baomidou.mybatisplus.plugins.Page; import com.wpj.entity.User; import com.wpj.service.IUserService; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/user") public class UserController { @Reference private IUserService userService; @RequestMapping("/getUserPage") public String getUserPage(Page<User> page, ModelMap map){ page = userService.getDubboPage(page); map.put("url", "user/getUserPage"); map.put("page",page); return "user/userList"; } }
package com.wpj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(scanBasePackages = "com.wpj", exclude = DataSourceAutoConfiguration.class)
public class UserWebApplication {
public static void main(String[] args) {
SpringApplication.run(UserWebApplication.class, args);
}
}
dubbo:
application:
name: user-web
registry:
address: zookeeper://47.98.33.215:2181
consumer:
check: false
retries: 3
spring:
thymeleaf:
cache: false
prefix: classpath:/templates/
package com.wpj.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.baomidou.mybatisplus.mapper.BaseMapper; import com.baomidou.mybatisplus.plugins.Page; import com.wpj.entity.User; import com.wpj.mapper.IUserMapper; import com.wpj.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; @Service public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService { @Autowired private IUserMapper userMapper; @Override public BaseMapper<User> getMapper() { return userMapper; } @Override public Page<User> getDubboPage(Page<User> page) { int current = page.getCurrent(); int size = page.getSize(); userMapper.selectPage(page, null); page.setRecords(userMapper.getUserList((current-1) * size, size)); return page; } }
package com.wpj.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.wpj.entity.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface IUserMapper extends BaseMapper<User> {
List<User> getUserList(@Param("index") int index, @Param("size") int size);
}
<?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.wpj.mapper.IUserMapper"> <resultMap id="userResultMap" type="user"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> <result column="sex" property="sex"/> <result column="email" property="email"/> <association property="createUser" javaType="user"> <id column="create_user" property="id"/> <result column="create_username" property="username"/> </association> </resultMap> <select id="getUserPage" resultMap="userResultMap"> SELECT u1.*, u2.username as create_username FROM t_user u1 LEFT JOIN t_user u2 ON (u1.create_user = u2.id) </select> </mapper>
mybatis-plus:
type-aliases-package: com.wpj.entity
mapper-locations: classpath:mapper/*.xml
@Test
public void getUserPage(){
Page<User> page = new Page<>();
page = userService.getDubboPage(page);
System.out.println(page);
}
<td th:text="${user.createUser.username}"/>
<div th:replace="/common/page::pageDiv"></div>
package com.wpj.config;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PagePlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
@SpringBootApplication(scanBasePackages = "com.wpj")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。