SpringBoot整合Mybatis(注解方式)
SpringBoot整合Mybatis通常有两种方式,一种是以xml映射文件的,一种是以注解的方式实现的,这里主要是以注解的方式来实现
项目目录结构如下:
src
----main
--------java
------------com.example.app
----------------controller //控制器
--------------------UserController.java
----------------entity //实体类
--------------------User.java
----------------mapper //映射接口
--------------------UserRepository.java
----------------service //业务
--------------------UserService.java
----resource
--------application.properties
首先先建一个表
CREATE TABLE `stus` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
然后随便插几条数据即可
application.properties配置如下:
#端口配置
server.port = 8080
#数据库配置
spring.datasource.url = jdbc:mysql://localhost:3306/数据库名?useSSL=false
#用户名
spring.datasource.username =
#密码
spring.datasource.password =
#驱动
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
还是和以往一样先建一个实体类(User.java)
package com.example.app.entity;
/** * @author Woo_home * @create by 2019/10/4 */
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
接着写一个mapper接口(UserRepository .java)
package com.example.app.mapper;
import com.example.app.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/** * @author Woo_home * @create by 2019/10/4 */
@Mapper
public interface UserRepository {
@Select("select * from stus")
List<User> getAllUser(); //查询所有用户
}
接着就是业务的编写
package com.example.app.service;
import com.example.app.entity.User;
import com.example.app.mapper.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/** * @author Woo_home * @create by 2019/10/4 */
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getAllUser(){
return userRepository.getAllUser();
}
}
控制器代码(UserController.java)
package com.example.app.controller;
import com.example.app.entity.User;
import com.example.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
/** * @author Woo_home * @create by 2019/10/4 */
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/userList")
@ResponseBody
public List<User> userList(){
return userService.getAllUser();
}
}
启动类
@MapperScan是在插件包中定义的注解,该注解的参数是一个包名字符串,只要在@MapperScan中配置需要扫描的Mybatis接口的包路径,Spring就会自动扫描包下面的java接口,把这些类注册为Spring的bean
package com.example.app;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.app.mapper")
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
}
}
还没有评论,来说两句吧...