今天就跟大家聊聊有关使用SpringBoot对Mybatis进行整合实现注解开发,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
SpringBoot整合Mybatis 引入maven依赖
(IDEA建项目的时候直接选就可以了)
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency>
配置application.yml文件
server: port: 8144 spring: #redis redis: host: 127.0.0.1 port: 6379 password: 123456 timeout: 3000 datasource: username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/hellomybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
建实体类、Controller类、Service类 实体类
package com.example.mybatisDemo.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class User { private long id; private String name; private String pwd; }
Controller类
package com.example.mybatisDemo.controller; import com.example.mybatisDemo.entity.User; import com.example.mybatisDemo.service.UserService; import org.springframework.web.bind.annotation.*; import java.util.*; @RestController @RequestMapping("api/user") public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @GetMapping() public List<User> getAllUsers(){ return userService.getUsers(); } @DeleteMapping("{id}") public int deleteUser(@PathVariable long id){ return userService.deleteUser(id); } @PostMapping public int saveUser(@RequestBody User user){ return userService.insertUser(user); } @PutMapping public int updateUser(@RequestBody User user){ return userService.updateUser(user); } @GetMapping("{id}") public User getUser(@PathVariable long id){ return userService.getUser(id); } }
Service类
package com.example.mybatisDemo.service; import com.example.mybatisDemo.entity.User; import com.example.mybatisDemo.repository.UserRepository; import org.springframework.stereotype.Service; import java.util.*; @Service public class UserService { private final UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public List<User> getUsers(){ return userRepository.getUsers(); } public int deleteUser(long id){ return userRepository.deleteUser(id); } public User getUser(long id){ return userRepository.getUser(id); } public int updateUser(User user){ return userRepository.updateUser(user); } public int insertUser(User user){ return userRepository.addUser(user); } }
建Repository类
package com.example.mybatisDemo.repository; import com.example.mybatisDemo.entity.User; import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Repository; import java.util.*; /** * Mapper注解能省去以前复杂的xml配置,直接用注解写sql语句 * 不添加Repository注解依赖注入会报错(不影响运行),强迫症还是加上吧 */ @Mapper @Repository public interface UserRepository { @Select("select * from user") List<User> getUsers(); @Delete("delete from user where id = #{id}") int deleteUser(long id); @Insert("insert into user(id,name,pwd) values (#{id},#{name},#{pwd})") int addUser(User user); @Update("update user set name=#{name},pwd=#{pwd} where id = #{id}") int updateUser(User user); @Select("select * from user where id = #{id}") User getUser(long id); }
看完上述内容,你们对使用SpringBoot对Mybatis进行整合实现注解开发有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。