SpringBoot整合Mybatis注解开发的实现代码

时间:2021-05-20

官方文档:

https://mybatis.org/mybatis-3/zh/getting-started.html

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: 8144spring: #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@AllArgsConstructorpublic 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.*;@Servicepublic 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@Repositorypublic 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);}

然后直接调用即可

这里有个问题,使用注解开发的话sql语句全写在注解里,那么如果要实现批量更新插入要怎么写呢,目前还没解决,找到办法再更

到此这篇关于SpringBoot整合Mybatis注解开发的实现代码的文章就介绍到这了,更多相关SpringBoot整合Mybatis注解开发内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章