1.整合Mybatis的话,Dao层就需要Mybatis的接口,接口方法的返回值的实现类,mapper文件,还有application.yaml文件 
接口里面定义我们要执行的方法,并且使用@Mapper注解(在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类,并且将接口类注入到容器中)  定义User类   定义mapper文件,namespace表示dao接口的全类名,id表示要执行的方法名,resultType表示返回的类型。 
配置数据库的相关信息

2接着来到了Service层,先创建个Service接口和实现类 
 在三层架构里面,Service会调用Dao层,所以这里注入了userMapper,然后调用userMapper的findAll()方法。
3来到了Controller层,controller层会调用Service层,所以这里注入Service层的变量userServiceImpl,然后调用userServiceImple的findAll方法。 
4最后走下流程,controller调用Service层的findAll方法,然后在Service层会调用Dao层的findAll方法,然后就会找到配置文件,执行sql语句。返回结果。
最后赋上建表语句
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `tb_user` VALUES ('1', 'laowang', '112233');
INSERT INTO `tb_user` VALUES ('2', 'laoli', '123456');
|