SSM(Spring+Spring MVC+MyBatis)是当前主流的框架组合开发方式之一,普遍被应用于互联网项目中。如果要使用Spring Boot开发一个基于SSM框架的应用,那么我们要怎么做呢?下面以一个用户查询案例为例,来讲解如何在Spring Boot中使用MyBatis。
SQL语句:
添加pom.xml依赖:
文件3-1 application.properties
文件3-2 User.java
文件3-3 UserMapper.java
文件3-4 UserService.java
文件3-5 UserServiceImpl.java
文件3-6 UserController.java
文件3-7 user.html
SQL语句:
# 创建一个名称为tb_user的表 CREATE TABLE tb_user ( id int(32) PRIMARY KEY AUTO_INCREMENT, username varchar(32), address varchar(256) ); # 插入3条数据 INSERT INTO tb_user VALUES ('1', '小韩', '北京市海淀区'); INSERT INTO tb_user VALUES ('2', '小石', '北京市昌平区'); INSERT INTO tb_user VALUES ('3', '小陈', '北京市顺义区');
<!-- MyBatis启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency><!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
#DB Configuration
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/microservice
spring.datasource.username=root
spring.datasource.password=123456
#logging
logging.level.com.xc.springboot=debug
package com.xc.springboot.po; public class User { private Integer id; private String username; private String address; //get set... }
package com.xc.springboot.mapper; @Mapper @Repository public interface UserMapper { // 查询所有用户 @Select("select * from tb_user") List<User> getAllUsers(); // 删除用户 @Delete("DELETE FROM tb_user WHERE id =#{id}") void delete(Integer id); }
package com.xc.springboot.service; public interface UserService { // 查询所有 List<User> getAllUsers(); // 删除数据 void deleteUser(Integer id); }
package com.xc.springboot.service.impl; @Transactional @Service public class UserServiceImpl implements UserService { //注入用户Mapper @Autowired private UserMapper userMapper; @Override public List<User> getAllUsers() { return userMapper.getAllUsers(); } @Override public void deleteUser(Integer id) { System.out.println("id:" + id); userMapper.delete(id); } }
package com.xc.springboot.controller; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; // 查询所有用户 @RequestMapping("/userList") public List<User> getAllUsers() { return userService.getAllUsers(); } // 删除用户 @RequestMapping("/delete/{id}") public void delete(@PathVariable Integer id) { userService.deleteUser(id); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户信息</title> <link rel="stylesheet" type="text/css" href="ui/themes/default/easyui.css"> <link rel="stylesheet" type="text/css" href="ui/themes/icon.css"> <script type="text/javascript" src="ui/jquery.min.js"></script> <script type="text/javascript" src="ui/jquery.easyui.min.js"></script> <script type="text/javascript" src="ui/locale/easyui-lang-zh_CN.js"></script> <script type="text/javascript"> $(function () { $('#grid').datagrid({ url: 'user/userList', fit: true, columns: [[ {field: 'id', title: '编号', 50}, {field: 'username', title: '姓名', 200}, {field: 'address', title: '地址', 200}, {field: 'del', title: '删除', 100}]] }); }); </script> </head> <body> <table id="grid"></table> </body> </html>