您的位置:首頁技術文章
文章詳情頁

Springboot整合Mybatispuls的實例詳解

瀏覽:56日期:2023-04-09 13:11:14

Springboot整合MybatisPuls

Maven導入依賴,主要只需導入MyBatisPuls

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>

配置數據源

spring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTCserver.port=8082

編寫實體類

@Data@AllArgsConstructor@NoArgsConstructor@TableName('users')//連接的表名public class Users implements Serializable { @TableId('id')標記該變量為主鍵 private Integer id; private String Account; @TableField('passwraod' )//如果實體類變量和數據庫不同使用 private String password; private Integer Authority;}

mapper接口編寫繼承BaseMapper<這里為實體類>

@org.apache.ibatis.annotations.Mapper//讓Spring容器掃描該類為Mapper@Repositorypublic interface Mapper extends BaseMapper<Users> {}

BaseMapper源碼

Springboot整合Mybatispuls的實例詳解

實現接口方法

@RestControllerpublic class Control { @Autowired Mapper mapper; @RequestMapping('/hello') public Users Select(){ Users users = mapper.selectById(1); return users; }}

到此這篇關于Springboot整合Mybatispuls的文章就介紹到這了,更多相關Springboot整合Mybatispuls內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
国产综合久久一区二区三区