springBoot 框架搭建
阿新 • • 發佈:2022-05-11
配置框架
官網IO配置:start.spring.io
src和pom.xml檔案有用
依賴:
專案結構
resource裡面的application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
實體類 entitry
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
對映mapper
import com.example.demo.entitry.User; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import org.springframework.transaction.annotation.Transactional; import java.util.List; public interface UserMapper { @Select("Select * from mybatis.user;") List<User> findAll(); @Update("insert into mybatis.user (name,pwd) values (#{name},#{pwd})") @Transactional void addUser(User user); @Update("update mybatis.user set (name=#{name},pwd=#{pwd}})where id=#{id}})") @Transactional void updateUserByID(User user); @Select("Select * from mybatis.user limit #{page},#{pageSize};") List<User> limitUser(int page,int pageSize); @Select("Select count(id) from mybatis.user;") int count(); }
控制層controller
import com.example.demo.entitry.User; import com.example.demo.mapper.UserMapper; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; //這個註解會將結果一JSON格式返回 @RestController //這個註解是介面路徑 @RequestMapping("/user") public class UserController { //引入介面 @Resource UserMapper userMapper; @GetMapping("/getUser") public List<User> getUser() { //點選最左邊這個 return userMapper.findAll(); } //@PathVariable,用地址連結裡面的引數來傳參,不用body/params @GetMapping("/getByUserId/{id}") public String getByUserId(@PathVariable("id") String userId) { System.out.println(userId); return userId; } //@RequestParam,用params傳參,並且引數必填 @GetMapping("/getByUserId2") public String getByUserId2(@RequestParam("id") String userId) { System.out.println(userId); return userId; } @GetMapping("/getByUserId3") public String getByUserId3(@RequestParam("id") String userId, @RequestParam("name") String userName) { System.out.println(userId + " " + userName); return userId + " " + userName; } @PostMapping public User addUser(@RequestBody User user) { System.out.println(user); //userMapper.addUser(user); return user; } //@RequestBody, @PutMapping("/updateUser") public User updateUser(@RequestBody User user) { System.out.println(user); //userMapper.updateUserByID(user); return user; } //分頁查詢 @GetMapping("/findByPage") public Map limitUser(@RequestParam("page") int page,@RequestParam("pageSize") int pageSize) { Map<String, Object> pageAndCount = new HashMap<>(); page=(page-1)*pageSize; pageAndCount.put("User",userMapper.limitUser(page,pageSize)); pageAndCount.put("count", userMapper.count()); return pageAndCount; } }