(二、下) springBoot 、maven 、mysql、 mybatis、 通用Mapper、lombok 简单搭建例子 《附项目源码》
接着上篇文章中 继续前进。
一、在maven 的pom.xm中添加组件依赖, mybatis通用Mapper,及分页插件
1、mybatis通用Mapper <!-- mybatis通用Mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.3</version> </dependency> 2、mybatis分页插件 <!-- 依赖地址:https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.7</version> </dependency>
二、创建表:user:
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '系统ID', `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名', `age` int(4) NULL DEFAULT NULL COMMENT '年龄', `gender` varchar(4) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别 1 男 ,2女', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; INSERT INTO `user` VALUES ('1', '路飞', 16, '1'); INSERT INTO `user` VALUES ('2', '罗罗诺亚索隆', 18, '1'); INSERT INTO `user` VALUES ('3', '娜美', 17, '2'); SET FOREIGN_KEY_CHECKS = 1;
三、再项目中创建 包结构,实体类,service, mapper, 等。
1、包结构及 用到的接口和类 如下:
2、各个需要改动的地方及 具体代码:
(1) application.yml 配置文件
#spring spring: #mysql 数据源 datasource: url: jdbc:mysql://127.0.0.1:3306/fish?zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&characterSetResults=utf8&useSSL=false username: root password: etoak driver-class-name: com.mysql.jdbc.Driver #mybatis mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.fish.winter.model configuration: map-underscore-to-camel-case: true #Mybatis通用Mapper mapper: mappers: com.fish.winter.base.BaseMapper not-empty: false identity: MYSQL before: true #Mybatis分页插件pagehelper pagehelper: helperDialect: mysql reasonable: false supportMethodsArguments: true params: count=countSql
(2)项目启动类 WinterApplication
package com.fish.winter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import tk.mybatis.spring.annotation.MapperScan; @SpringBootApplication @MapperScan(basePackages = "com.fish.winter.mapper") // 注意这里导入的 MapperScan 的包是:tk.mybatis.spring.annotation.MapperScan public class WinterApplication { public static void main(String[] args) { SpringApplication.run(WinterApplication.class, args); } }
(3)创建 通用Mapper 的通用 持久层接口:BaseMapper, 以便所有实体类的mapper 继承此接口,用来调用 通用Mapper 的各种方法。
package com.fish.winter.base; import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.MySqlMapper; /** * @author 路飞 * @date 2018-9-19 * @description 持久化层的父类 */ public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T> { }
(4)创建实体类 User, 类中用到了 lombok 的 @Data 和 @Builder 注解
package com.fish.winter.model;
import lombok.Builder;
import lombok.Data;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@Builder
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "select uuid()")
private String id;
private String name;
private Integer age;
private String gender;
}
(5)在 controller 中添加 访问接口
package com.fish.winter.controller; import com.fish.winter.model.User; import com.fish.winter.service.UserService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; @RestController @RequestMapping("/test") public class TestController { @RequestMapping("/hello") public String test() { return "你好spriongBoot"; } @Resource private UserService userService; @RequestMapping("/user") private List<User> queryByExample() { return userService.list(); } }
(6)创建service ,及实现类
package com.fish.winter.service; import com.fish.winter.model.User; import java.util.List; public interface UserService { // 获取user 信息 List<User> list(); }
package com.fish.winter.serviceImpl; import com.fish.winter.mapper.UserMapper; import com.fish.winter.model.User; import com.fish.winter.service.UserService; import org.springframework.stereotype.Service; import tk.mybatis.mapper.entity.Example; import javax.annotation.Resource; import java.util.List; @Service("userService") public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public List<User> list() { Example example = new Example(User.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("name", "路飞"); List<User> userList = userMapper.selectByExample(example); return userList; } }
(6)在浏览器中访问:http://127.0.0.1:8080/test/user ,结果如下。
项目源码地址:https://gitee.com/coderLOL/winter
==================================================================
有兴趣的朋可以自己研究下 Mybatis通用Mapper 的使用方法 ,下面是 文档地址:
作者:橡胶果实 —— 要成为海贼王的少年
出处:https://www.cnblogs.com/topfish/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。