一、前提
一直使用mybatis generator作为orm研发,习惯了注解型sql就不太愿意写xml的sql,之前以为mybatis plus是必须写xml的,后来发现不用,也挺方便的
二、示例
添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
添加扫描路径
@SpringBootApplication
@MapperScan("com.wenx.mybatisplus.dao")
public class MybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusApplication.class, args);
}
}
添加配置
spring.application.name=call-out
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
logging.level.root=info
logging.level.com.wenx.mybatisplus=debug
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.typeAliasesPackage=com.wenx.mybatisplus.entity
mybatis-plus.global-config.db-config.id-type=AUTO
mybatis-plus.global-config.db-config.field-strategy=not_empty
mybatis-plus.global-config.db-config.column-underline=false
mybatis-plus.global-config.db-config.logic-delete-value=0
mybatis-plus.global-config.db-config.logic-not-delete-value=1
mybatis-plus.global-config.db-config.db-type=mysql
mybatis-plus.global-config.refresh=true
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false
实体
@Data
@ToString
public class Topic {
@TableId(type = IdType.AUTO)
private Integer id;
private String title;
private String content;
private String tag;
private Date inTime;
}
mapper
public interface TopicMapper extends BaseMapper<Topic> {
@Select("select * from topic")
Topic[] queryForList();
@Select("select * from topic where content = #{content} ")
Topic[] queryByParam(@Param("content")String content);
}
service
@Service
public class TopicService extends ServiceImpl<TopicMapper,Topic> {
}
测试代码
@SpringBootTest
@Slf4j
class MybatisplusApplicationTests {
@Autowired
private TopicService topicService;
@Autowired
private TopicMapper topicMapper;
@Test
void contextLoads() {
//mapper
Topic[] topicList = topicMapper.queryForList();
log.debug("topicList:{}",topicList);
Topic topic = new Topic();
topic.setContent("test");
topic.setInTime(new Date());
topic.setTag("testTag");
topic.setTitle("title");
topicMapper.insert(topic);
QueryWrapper<Topic> queryWrapper = new QueryWrapper<>();
queryWrapper.ge("content","test");
List<Topic> selectList = topicMapper.selectList(queryWrapper);
log.debug("selectList:{}",selectList);
Topic[] contents = topicMapper.queryByParam("test");
log.debug("contents:{}",contents);
//service
List<Topic> topics = topicService.list();
log.debug("topics:{}",topics);
List<Topic> topics1 = topicService.lambdaQuery().eq(Topic::getContent,"test").list();
log.debug("topics1:{}",topics1);
Topic topic1 = topicService.getOne(Wrappers.<Topic>lambdaQuery().eq(Topic::getId,12),false);
log.debug("topic1:{}",topic1);
topicService.lambdaUpdate().eq(Topic::getContent, "test").set(Topic::getInTime,new Date()).update();
}
}
需要用到的sql
DROP TABLE IF EXISTS `topic`;
CREATE TABLE `topic` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`content` varchar(255) DEFAULT NULL,
`tag` varchar(255) DEFAULT NULL,
`in_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of topic
-- ----------------------------
INSERT INTO `topic` VALUES ('1', 'title1', 'content1', 'tag1', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('2', 'title2', 'content2', 'tag2', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('3', 'title3', 'content3', 'tag3', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('4', 'title4', 'content4', 'tag4', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('5', 'title4', 'content4', 'tag4', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('6', 'title4', 'content4', 'tag4', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('7', 'title4', 'content4', 'tag4', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('8', 'title4', 'content4', 'tag4', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('9', 'title4', 'content4', 'tag4', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('10', 'title4', 'content4', 'tag4', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('11', 'title4', 'content4', 'tag4', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('12', 'title4', 'content4', 'tag4', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('13', 'title4', 'content4', 'tag4', '2019-04-15 09:33:35');
INSERT INTO `topic` VALUES ('14', 'title', 'test', 'testTag', '2020-08-07 02:59:22');
INSERT INTO `topic` VALUES ('15', 'title', 'test', 'testTag', '2020-08-07 03:01:10');
INSERT INTO `topic` VALUES ('16', 'title', 'test', 'testTag', '2020-08-07 03:09:47');
INSERT INTO `topic` VALUES ('17', 'title', 'test', 'testTag', '2020-08-07 03:10:34');
INSERT INTO `topic` VALUES ('18', 'title', 'test', 'testTag', '2020-08-07 03:20:36');
INSERT INTO `topic` VALUES ('19', 'title', 'test', 'testTag', '2020-08-07 03:24:48');
网友评论