在Spring boot项目中使用 mybatis 与Vue实现对数据进行增删改查操作?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联专业为企业提供淮北网站建设、淮北做网站、淮北网站设计、淮北网站制作等企业网站建设、网页设计与制作、淮北企业网站模板建站服务,10多年淮北做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
pom文件
4.0.0 com.imooc demo 0.0.1-SNAPSHOT jar demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE UTF-8 UTF-8 1.8 org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 org.springframework.boot spring-boot-starter-web MySQL mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 org.springframework.boot spring-boot-starter-redis org.springframework.boot spring-boot-starter-activemq org.springframework.boot spring-boot-starter-actuator com.github.pagehelper pagehelper 4.1.6 org.springframework.boot spring-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 { ListfindUsersByUsername(@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 MapgetTableData(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 MapgetTableData(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; } }
sql
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项目中使用 mybatis 与Vue实现对数据进行增删改查操作问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。