1 依赖
<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>
<!--notice 数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
2 application。xml 配置
spring:
# 数据源配置 //spring.datasource.url
datasource:
url: jdbc:mysql://localhost:3306/drog?useUnicode=true&characterEncoding=utf8&useSSL=false&tinyInt1isBit=true
username: root
password: 123456
3 application 扫描配置
@SpringBootApplication
@MapperScan("com.mybatisplus.mapper")//扫描mapper包下的类到spring容器中
public class MybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusApplication.class, args);
}
}
4 dao对象生成
public interface UserMapper extends BaseMapper<User> {
}
5 代码中使用
/@RequestMapping("/demo")
// notice:
// notice: @RequestMapping:提供路由信息,负责URL到Controller中的具体函数的映射
@Controller
public class UserController {
@Autowired
UserMapper userMapper;
// notice: 通过 http://localhost:8080/user访问该方法
// notice: @ResponseBody:表示该方法的返回结果直接写入HTTP response body中
// notice: 在使用@RequestMapping后,返回值通常解析为跳转路径
// notice: 加上@esponsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。
// notice: 比如异步获取json数据,加上@Responsebody后,会直接返回json数据。该注解一般会配合@RequestMapping一起使用
@RequestMapping("/user")
@ResponseBody
public String insert(){
User user = new User();
user.setAge(12);
user.setName("zxl");
user.setEmail("zxl@163.com");
userMapper.insert(user);
return "ok";
}
// notice: 不加@esponsebody返回结果会被解析为跳转路径
@RequestMapping("/index")
public String index(){
return "index";
}
@PostMapping("/login")
@ResponseBody//返回json //@RequestBody接受json格式数据
public Object login(@RequestParam("userName") String userName, @RequestParam("password") String password ){//, @RequestBody String obj
User user = new User();
user.setAge(12);
user.setName(userName);
user.setEmail(password);
return user;
}
}