参考文章: https://www.cnblogs.com/ityouknow/p/6037431.html
- pom
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
说明: springboot版本: 2.1.5.RELEASE
- application.properties
# mysql
spring.datasource.url=jdbc:mysql://212.64.xxx.xxx:3306/test?autoR&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.在启动类中添加对 mapper 包扫描@MapperScan
package com.wangzaiplus.test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@SpringBootApplication
@MapperScan("com.wangzaiplus.test.mapper")
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
/**
* 跨域
* @return
*/
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
final CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(urlBasedCorsConfigurationSource);
}
}
说明: springboot项目添加corsFilter解决跨域问题
或者直接在 Mapper 类上面添加注解@Mapper,建议使用上面那种,不然每个 mapper 加个注解也挺麻烦的
- mapper
package com.wangzaiplus.test.mapper;
import com.wangzaiplus.test.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import java.util.List;
public interface UserMapper {
@Select("select * from user")
@Results({
@Result(property = "username", column = "username", jdbcType = JdbcType.VARCHAR),
@Result(property = "password", column = "password")
})
List<User> selectAll();
@Select("select * from user where id = #{id}")
@Results({
@Result(property = "username", column = "username", jdbcType = JdbcType.VARCHAR),
@Result(property = "password", column = "password")
})
User selectOne(Integer id);
@Insert("insert into user(username, password) values(#{username}, #{password})")
void insert(User user);
@Update("update user set username=#{username}, password=#{password} where id = #{id}")
void update(User user);
@Delete("delete from user where id = #{id}")
void delete(Integer id);
}
- controller
package com.wangzaiplus.test.controller;
import com.wangzaiplus.test.pojo.User;
import com.wangzaiplus.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("users")
public String getAll() {
List<User> users = userService.getAll();
return users.toString();
}
@GetMapping("{id}")
public String getOne(@PathVariable Integer id) {
User user = userService.getOne(id);
return user + "";
}
@PostMapping
public String add(User user) {
return userService.add(user) + "";
}
@PutMapping
public String update(User user) {
return userService.update(user) + "";
}
@DeleteMapping("{id}")
public String delete(@PathVariable Integer id) {
return userService.delete(id) + "";
}
}
说明: 不用纠结
user + ""不严谨什么的, 仅供示例, 另, restful接口风格
- service
package com.wangzaiplus.test.service;
import com.wangzaiplus.test.pojo.User;
import java.util.List;
public interface UserService {
List<User> getAll();
User getOne(Integer id);
boolean add(User user);
boolean update(User user);
boolean delete(Integer id);
}
- impl
package com.wangzaiplus.test.service.impl;
import com.wangzaiplus.test.mapper.UserMapper;
import com.wangzaiplus.test.pojo.User;
import com.wangzaiplus.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getAll() {
return userMapper.selectAll();
}
@Override
public User getOne(Integer id) {
return userMapper.selectOne(id);
}
@Override
public boolean add(User user) {
userMapper.insert(user);
return true;
}
@Override
public boolean update(User user) {
userMapper.update(user);
return true;
}
@Override
public boolean delete(Integer id) {
userMapper.delete(id);
return true;
}
}
- pojo
package com.wangzaiplus.test.pojo;
import lombok.Data;
@Data
public class User {
private Integer id;
private String username;
private String password;
}
说明: @Data lombok
9.sql
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `unq_username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
- 接口请求
其他接口类似, 略
目前为止, 注解方式实现springboot与mybatis整合已完成, 接口均测试通过