react 前端框架如何驱动企业数字化转型与创新发展
714
2023-03-07
SpringBoot整合Mybatis注解开发的实现代码
官方文档:
https://mybatis.org/mybatis-3/zh/getting-started.html
SpringBoot整合Mybatis 引入maven依赖
(IDEA建项目的时候直接选就可以了)
配置application.yml文件
server:
port: 8144
spring:
#redis
redis:
host: 127.0.0.1
port: 6379
password: 123456
timeout: 3000
datasourchttp://e:
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
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
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注http://解依赖注入会报错(不影响运行),强迫症还是加上吧
*/
@Mapper
@Repository
public interface UserRepository {
@Select("select * from user")
List
@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语句全写在注解里,那么如果要实现批量更新插入要怎么写呢,目前还没解决,找到办法再更
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~