标签:body ram localhost comment autowire mysql ide success amp
1、添加依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
2、application配置文件
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://localhost:3306/Take_Lessons?serverTimezone=UTC&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=xxxxx
关于更多更详细的配置请参考:https://blog.csdn.net/ajlzhu/article/details/81009845
3、新建dao包、user类、userDao接口、UserService类、TestController类
创建后的目录结构(多余的不用在意):
4、数据库user表
CREATE TABLE `User` ( `user_id` int(4) NOT NULL, `user_name` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT ‘用户姓名‘, `user_identify` int(2) DEFAULT NULL COMMENT ‘用户身份‘, `salt` varchar(20) DEFAULT NULL COMMENT ‘盐‘, `user_number` varchar(20) DEFAULT NULL COMMENT ‘用户学号或工号‘, `user_grade` varchar(5) DEFAULT NULL COMMENT ‘学生年级‘, `user_major` varchar(30) DEFAULT NULL COMMENT ‘学生专业‘, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
5、user类
public class User { private int id; private String user_name; private String salt; private int user_identify; private String user_number; private String user_grade; private String user_major; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getSalt() { return salt; } public void setSalt(String salt) { this.salt = salt; } public int getUser_identify() { return user_identify; } public void setUser_identify(int user_identify) { this.user_identify = user_identify; } public String getUser_number() { return user_number; } public void setUser_number(String user_number) { this.user_number = user_number; } public String getUser_grade() { return user_grade; } public void setUser_grade(String user_grade) { this.user_grade = user_grade; } public String getUser_major() { return user_major; } public void setUser_major(String user_major) { this.user_major = user_major; } public User() { // TODO Auto-generated constructor stub } }
6、userDao接口
@Mapper public interface UserDao { @Select("select * from User where user_id = #{id}") public User getById(@Param("id")int id); }
7、UserServicce类
@Service public class UserService { @Autowired UserDao userDao; public User getById(int id){ return userDao.getById(id); } }
8、控制器
@ResponseBody @GetMapping("/user") public Result<User> user(){ User user=userService.getById(1); return Result.success(user); }
9、测试
到这里就完全整合完成了,甚至都不需要进行任何多余的配置。
【mybatis】-- springboot整合mybatis
标签:body ram localhost comment autowire mysql ide success amp
原文地址:https://www.cnblogs.com/jsyllhb/p/10553821.html