这篇文章主要介绍了Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(二),非常不错,具有参考借鉴价值,需要的朋友可以参考下
在上篇文章给大家介绍了Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一),接下来我们添加分页相关的依赖,时间紧张,直接上代码了,贴上我的pom文件
4.0.0com.imoocdemo0.0.1-SNAPSHOTjardemoDemo project for Spring Bootorg.springframework.bootspring-boot-starter-parent1.4.3.RELEASEUTF-8UTF-81.8org.mybatis.spring.bootmybatis-spring-boot-starter1.1.1org.springframework.bootspring-boot-starter-webMysqLmysql-connector-javaruntimeorg.springframework.bootspring-boot-starter-testtestorg.mybatis.spring.bootmybatis-spring-boot-starter1.1.1org.springframework.bootspring-boot-starter-redisorg.springframework.bootspring-boot-starter-activemqorg.springframework.bootspring-boot-starter-actuatorcom.github.pageHelperpageHelper4.1.6org.springframework.bootspring-boot-maven-plugin
接下来是yml文件,主要加入了mybatis的配置,以及sql的打印
spring: datasource: name: test url: jdbc:MysqL://localhost/imooc?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 driver-class-name: com.MysqL.jdbc.Driver mybatis: type-aliases-package: com.imooc.model mapper-locations: classpath:mybatis/mapper/*.xml check-config-location: true config-location: classpath:mybatis/mybatis-config.xml logging: level: com.imooc.repository: debug com.imooc.service.impl: debug com.imooc.controller: debug com.imooc.activemq: debug
接下来是repositpry文件
@Repository public interface UserRepository { List findUsersByUsername(@Param("username") String username); int getCount(); int saveUser(User user); int modifyUser(User user); int removeUser(@Param("userId") int userId); }
service文件
@Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override public Map getTableData(int pageNum, int pageSize, String username) { try { pageHelper.startPage(pageNum, pageSize); List userList = userRepository.findUsersByUsername(username); int count = userRepository.getCount(); Map tableData = new HashMap(); tableData.put("list", userList); tableData.put("count", count); return tableData; } catch (Exception e) { e.printstacktrace(); } return null; } } public interface UserService { Map getTableData(int pageNum, int pageSize, String username); }
controller文件
@RestController public class UserController { @Autowired private UserService userService; @GetMapping("getTableData") public Map getTableData(int pageNum, int pageSize, String username) { try { return userService.getTableData(pageNum, pageSize, username); } catch (Exception e) { e.printstacktrace(); } return null; } }
public class User { private Integer userId; private String username; private Byte sex; private Date createTime; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Byte getSex() { return sex; } public void setSex(Byte sex) { this.sex = sex; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
CREATE TABLE `t_user` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(32) DEFAULT NULL, `sex` tinyint(4) DEFAULT NULL, `create_time` datetime DEFAULT NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB AUTO_INCREMENT=10003 DEFAULT CHARSET=utf8
在static目录下新建 index.html文件
spring boot + mybatis + vue + elementui
启动文件
@EnableAutoConfiguration @Configuration @ComponentScan @MapperScan("com.imooc.repository") @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
启动项目,打开http://localhost:8080/index.html
推荐专题阅读:
spring boot开发教程:https://www.html.cn/Special/943.htm
mybatis教程:https://www.html.cn/Special/774.htm