Mybatis逆向工程仅仅针对单表操作
一、测试用数据库表drop table if exists t_student;
create table t_student
(
id int(10) not null auto_increment,
name varchar(20) null,
age int(10) null,
constraint PK_T_STUDENT primary key clustered (id)
);
insert into t_student(name,age) values("zhangsan",25);
insert into t_student(name,age) values("lisi",28);
insert into t_student(name,age) values("wangwu",23);
insert into t_student(name,age) values("Tom",21);
insert into t_student(name,age) values("Jck",55);
insert into t_student(name,age) values("Lucy",27);
insert into t_student(name,age) values("zhaoliu",75);
二、逆向工程配置文件(代码生成器)
GeneratorMapper.xml
三、 添加逆向工程插件:
org.mybatis.generator mybatis-generator-maven-plugin 1.4.0 GeneratorMapper.xml true true
总pom.xml文件(引入的依赖不一定都能用上)
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.5 com.example springboot13 0.0.1-SNAPSHOT 11 org.springframework.boot spring-boot-starter-web mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 org.springframework.boot spring-boot-starter-test test org.mybatis mybatis 3.5.7 compile src/main/java ** Student queryStudentById(Integer id); }
3、StudentServiceImp
@Service
public class StudentServiceImp implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public Student queryStudentById(Integer id) {
return studentMapper.selectByPrimaryKey(id);
}
}
4、测试用Controller
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/student/{id}")
@ResponseBody
public Object student(@PathVariable Integer id) {
Student student = studentService.queryStudentById(id);
return student;
}
}
5、测试结果
6、如果报错:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.springboot13.mapper.StudentMapper.selectByPrimaryKey
可能是因为pom.xml文件的build标签下没有加上如下代码、导致不能扫描到mapper文件夹下的xxMapper.xml文件,因为xml属于资源文件,但是我们却放在java文件夹下面,后面会介绍怎么使生成的xml直接放在resources文件夹下
src/main/java **/*.xml
加入以后重新加载Maven项目,删除target文件夹,重新加载模块(项目),防止缓存没有更新
7、当然,也可以将mapper中的.mxl文件放在resources目录下(一般在resuorces目录下新建一个mapper文件夹存放xxMapper.xml文件(推荐))*
代码生成配置文件更改一下也可以自动生成到resources目录下:
并且在配置文件中加入以下配置:
# 指定mybatis映射文件路径,并不是指定编译路径,而是告诉你去哪里找 # IDEA编译会自动加入resources目录下的文件 mybatis.mapper-locations=classpath:mapper/*.xml
此时也能正常访问
如果出现如下图错误,并不影响项目运行
换用@Resources注解可以消除错误(按道理Autowired是没错的)
的确有效,但是这个并不是解决问题,只是告诉IDEA忽略这种错误警告
2、在mapper文件上添加@Repository注解简介:java.lang.SuppressWarnings是J2SE5.0中标准的Annotation之一。可以标注在类、字段、方法、参数、构造方法,以及局部变量上。
作用:告诉编译器忽略指定的警告,不用在编译完成后出现警告信息。
使用:
@SuppressWarnings(“”)
@SuppressWarnings({})
@SuppressWarnings(value={})根据sun的官方文档描述:
value -将由编译器在注释的元素中取消显示的警告集。允许使用重复的名称。忽略第二个和后面出现的名称。出现未被识别的警告名不是错误:编译器必须忽略无法识别的所有警告名。但如果某个注释包含未被识别的警告名,那么编译器可以随意发出一个警告。各编译器供应商应该将它们所支持的警告名连同注释类型一起记录。鼓励各供应商之间相互合作,确保在多个编译器中使用相同的名称。
示例:
· @SuppressWarnings(“unchecked”)
告诉编译器忽略 unchecked 警告信息,如使用List,ArrayList等未进行参数化产生的警告信息。
· @SuppressWarnings(“serial”)
如果编译器出现这样的警告信息:The serializable class WmailCalendar does notdeclare a static final serialVersionUID field of type long
使用这个注释将警告信息去掉。· @SuppressWarnings(“deprecation”)
如果使用了使用@Deprecated注释的方法,编译器将出现警告信息。
使用这个注释将警告信息去掉。· @SuppressWarnings(“unchecked”, “deprecation”)
告诉编译器同时忽略unchecked和deprecation的警告信息。
· @SuppressWarnings(value={“unchecked”, “deprecation”})
等同于@SuppressWarnings(“unchecked”, “deprecation”)
@Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。具体只需将该注解标注在 DAO类上即可
这是从spring2.0新增的一个注解,用于简化 Spring 的开发,实现数据访问
@repository需要在Spring中配置扫描包地址,然后生成dao层的bean,之后被注入到ServiceImpl中
3、在mapper文件上添加@Component注解@Component把普通pojo实例化到spring容器中,相当于配置文件中的
@Component是 @Service @Controller @Repository 的父注解 通过类路径扫描自动检测实现类
4、在mapper文件上添加@Mapper注解使用@mapper后,不需要在spring配置中设置扫描地址,通过mapper.xml里面的namespace属性对应相关的mapper类,spring将动态的生成Bean后注入到ServiceImpl中。
以上解决方案都可以,但是如果用以上方案,那么我主程序类里面的**@MapperScan**注解又有什么用呢?,意义何在!
我们都知道,使用**@MapperScan**的目的是为了简化开发,不用每个mapper都去指定@Mapper注解,但是使用@MapperScan注解后却会爆红(虽然不影响执行,但是却让人一脸迷惑)
希望哪位大神给个关于这个问题的精确解释,欢迎留言,谢谢!!!!!!!
5、既然又想用@MapperScan,又不想写请其他注解该怎么办呢?方法当然有:
A:忍受IDEA随便在你的代码上标红,能运行就行(反正我受不了)
B:使用**@Resource注解**代替@Autowired (也就是之前提到的解决方案)
@Resource private StudentMapper studentMapper;
在大多数的应用场景下,@Resources都可以代替@Autowired
@Autowired注解 -【Spring底层原理】 (qq.com)
6、如果又不想用其他注解代替@Autowired,又不想添加其他注解,但是又能符合编码规范,该怎么解决呢,欢迎知道的大神留言讨论,或者详细讲解下IDEA报错的原因及其解释


