mybatis-plus01——quickStart

mybatis-plus01——quickStart,第1张

mybatis-plus01——quickStart 1. 引入依赖
		
			com.baomidou
			mybatis-plus-boot-starter
			3.0.5
		

		
			mysql
			mysql-connector-java
		
2. 配置数据库链接信息

当使用springboot2.1以上时,需要配置时区

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=xxxxxx

2.1以下时

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus
spring.datasource.username=root
spring.datasource.password=xxxxxx
3. 编写实体类及mapper接口继承baseMapper
package com.minghao.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
package com.minghao.mapper;

import com.baomidou.mybatisplus.core.mapper.baseMapper;
import com.minghao.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper extends baseMapper {

}
4. 在启动类上加@MapperScan(mapper接口的包名)
package com.minghao;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.minghao.mapper")
public class Mpdemo1107Application {

	public static void main(String[] args) {
		SpringApplication.run(Mpdemo1107Application.class, args);
	}

}
5. 在测试类中注入并测试查询所有用户方法
class Mpdemo1107ApplicationTests {

	@Autowired
	private UserMapper userMapper;

	@Test
	public void findAll(){
		List users = userMapper.selectList(null);
		for (User user : users) {
			System.out.println(user);
		}
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5138727.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存