欢迎关注公众号“小东方不败”
创建项目:
目前稳定的最新版本是2.7.5
,勾选两个依赖:Lombok
和Spring Web
然后需要导入依赖:(Lombok的依赖已经导入了)
org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.2
mysql mysql-connector-java 8.0.31
确保依赖导入成功:
application.yml
上一个笔记学习过yml和properties文件的差异之后,决定以后都用yml更好吧。
首先需要配置数据库连接的4个要素和项目部署的端口号和上下文路径(其实端口和项目的上下文路径不需要部署也行,默认8080,项目上下文路径是“”):
server:port: 8080servlet:context-path: /mybatis
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: 你的密码url: jdbc:mysql://127.0.0.1/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
其次,因为还有mybatis的配置:
mybatis:type-aliases-package: com.bones.pojomapper-locations: classpath:mybatis/*.xml
上面的配置对应于:
注意:
mapper-locations: classpath:mybatis/*.xml mapper
映射文件包扫描
type-aliases-package
实体类别名包扫描
要完成的功能就是数据库表user的所有数据:
项目最终结构:
实体类:com.bones.pojo.User
package com.bones.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.io.Serializable;@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {private Integer uid;private String uname;private Integer password;
}
mapper层:com.bones.mapper.UserMapper
package com.bones.mapper;import com.bones.pojo.User;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface UserMapper {List queryAll();
}
mapper xml文件
service层:com.bones.service.UserService
package com.bones.service;import com.bones.pojo.User;import java.util.List;public interface UserService {List queryAll();
}
其实现类:com.bones.service.UserServiceImpl
package com.bones.service.impl;import com.bones.mapper.UserMapper;
import com.bones.pojo.User;
import com.bones.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic List queryAll() {return userMapper.queryAll();}
}
controller层com.bones.controller.UserController
package com.bones.controller;import com.bones.pojo.User;
import com.bones.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;@Controller
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;@ResponseBody@RequestMapping("queryAllUser")public List queryAll(){return userService.queryAll();}
}
运行启动类,访问http://localhost:8080/mybatis/user/queryAllUser
得到数据:
如果是用火狐访问,可以得到JSON格式显示的数据:
以上就是一层一层的编写步骤。
1.我个人写代码的习惯是:先写实体类(Lombok注解一写,so quick)—>创建好各个层的类或者接口(加上注解,但是不写具体代码)—》从controller层写起,从controller层先定义好返回的类型,这样用IDEA快捷键快速写完service层和mapper层,一层一层往下写。如果一层一层往上写,还得考虑返回值类型,不想动这个脑。
2.还有就是在注入对象的时候,往往IDEA会误报,建议将IDEA的报错级别调低:
确定代码无问题,可以通过降低idea检查代码的严格程度来消除报错快捷键(windows):ctrl+alt+shift+h
我个人不是很喜欢把报错级别调整低。
3.在resource下新建mybatis文件夹,mapper.xml文件名没有要求了,不需要和接口名完全对应了,是根据namespace去找接口。但是最好还是和接口名字保持一致 (增强可读性+开发效率)
4.在上面的mapper接口中加了注解
@Mapper
,其实可以不加.如果不加@Mapper
注解,那么要在启动类上加:
@MapperScan("com.bones.mapper")
两个注解写1个即可。
上一篇:初始数据结构