-
創(chuàng)建一個新的模塊
注意模塊選擇羹铅,并選擇jdk版本至少為8
此處可以添加自身所需依賴
- 準(zhǔn)備工作
添加所需依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<optional>true</optional>
</dependency>
</dependencies>
在application.properties中添加配置,其中中username和password為自身數(shù)據(jù)庫的用戶名和密碼(以my sql為例)
## 數(shù)據(jù)源配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_spring?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=**
spring.datasource.password=**
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#指定實(shí)體類映射的包
mybatis.type-aliases-package=com.springboot.mybatis.entity
構(gòu)建包結(jié)構(gòu)
- 進(jìn)行編碼
- 編寫entity實(shí)體類,其中屬性對應(yīng)數(shù)據(jù)庫中表的數(shù)據(jù)
@Data
public class User {
private Long userId;
private String mobile;
private String password;
private String username;
private String avatar;
}
- 在Mapper中編寫需要使用到的sql語句愉昆,在結(jié)果集中column為數(shù)據(jù)庫屬性名职员,property為idea中屬性名,還有就是sql語句在最后空一格為了方便拼接
public interface UserMapper {
@Results({@Result(column = "user_id",property = "userId"),
@Result(column = "mobile",property = "mobile"),
@Result(column = "password",property = "password"),
@Result(column = "username",property = "username"),
@Result(column = "avatar",property = "avatar")
})
@Select("SELECT * FROM t_sys_user ")
List<User> selectAll();
@Results({@Result(column = "user_id",property = "userId"),
@Result(column = "mobile",property = "mobile"),
@Result(column = "password",property = "password"),
@Result(column = "username",property = "username"),
@Result(column = "avatar",property = "avatar")
})
@Select("SELECT * FROM t_sys_user WHERE user_id = #{userId} ")
User getOne(Long userId);
@Delete("DELETE FROM t_sys_user WHERE user_id =#{userId} ")
void delete(Long userId);
@Insert("INSERT INTO t_sys_user(mobile,password,username,avatar)"+
"VALUES(#{mobile},#{password},#{username},#{avatar}) ")
void insert(User user);
@Update("UPDATE t_sys_user SET password=#{password},avatar=#{avatar} WHERE user_id=#{userId} ")
void updata(User user);
}
- service中寫個實(shí)現(xiàn)mapper中方法的接口
public interface UserService {
List<User> selectAll();
User getOne(long userId);
void delete(long userId);
User insert(User user);
void updata(User user);
}
- 在serviceimpl中實(shí)現(xiàn)service中接口的方法
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List<User> selectAll() {
return userMapper.selectAll();
}
@Override
public User getOne(long userId) {
return userMapper.getOne(userId);
}
@Override
public void delete(long userId) {
userMapper.delete(userId);
}
@Override
public User insert(User user) {
userMapper.insert(user);
return user;
}
@Override
public void updata(User user) {
userMapper.updata(user);
}
}
對serviceImpl中的方法進(jìn)行測試跛溉,以便排查錯誤
在controller中寫入RESTful請求
@RestController
@RequestMapping(value = "/api")
public class UserController {
@Resource
private UserService userService;
@RequestMapping(value = "/users",method = RequestMethod.GET)
public List<User> selectAll(){
return userService.selectAll();
}
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
public User getOne(@PathVariable("id") long id){
return userService.getOne(id);
}
@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
public void deleteUser(@PathVariable("id") long id){
userService.delete(id);
}
@RequestMapping(value = "/user",method = RequestMethod.POST)
public User addUser(@RequestBody User user){
return userService.insert(user);
}
@RequestMapping(value = "/user",method = RequestMethod.PUT)
public void update(@RequestBody User user){
userService.updata(user);
}
}
- 打開啟動類焊切,記得加上注釋,不然可能找不到mapper芳室,最后運(yùn)行
@SpringBootApplication
@MapperScan("com.springboot.mybatis.mapper")
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}
-
出現(xiàn)以下結(jié)果說明運(yùn)行成功
- 使用postman驗(yàn)證
其中有幾點(diǎn)要注意
- 要注意動作如get,put芽死,delete卖毁,post等的區(qū)別
- 結(jié)果為200即為成功
-
使用put和post時要注意一下幾處