参考:https://github.com/daleiwang/moxi
service
@Mapper 与 @Select 等
@Mapper似乎是一个myBaits 注解,表示将java方法和sql操作映射起来
sql语句里的用井号和花括号括起来的参数,对应实体类里的各个同名属性
例如#{userName} 对应了 参数admin对象的userName属性
@Mapper // mapper似乎是一个myBaits 注解,表示将java方法和sql操作映射起来
public interface AdminService {
//sql语句里的用井号和花括号括起来的参数,对应实体类里的各个同名属性
//例如#{userName} 对应了 参数admin对象的userName属性
@Select("SELECT * FROM `moxi`.`admin` where userName = #{userName} and password = #{password} ;")
Admin findByNameAndPassword(Admin admin);
@Select("SELECT * FROM `moxi`.`admin` where userName = #{userName} and password = #{password} and realName = #{realName}")
List<Admin> findByAdminWithPage(Admin admin, int start, int end);
@Insert("INSERT INTO `moxi`.`admin` (`id`, `userName`, `password`, `realName`, `age`, `phoneNumber`, `headPicture`, `addDate`, `updateDate`, `state`) VALUES (null, #{userName}, #{password}, #{realName}, #{age}, #{phoneNumber}, #{headPicture}, now(), now(), 0);")
int insert(Admin admin);
@Update("UPDATE `moxi`.`admin` SET `userName` = #{userName}, `password` = #{password}, `realName` = #{realName}, `age` = #{age}, `phoneNumber` = #{phoneNumber}, `headPicture` = #{headPicture}, `updateDate` = now(), `state` = #{state} WHERE `id` = #{id};")
int updateStateById(int id);
@Delete("DELETE FROM `moxi`.`admin` WHERE id = #{id}")
int deleteById(int id);
}
controller
如何知道每个url对应哪个html
当我看上面这个github里的项目,包括其中readme里提供的简书教程时,我很疑惑,怎样知道每个url对应哪个html文件。
后来弄懂了,大致如下:
- 把html文件放在项目根目录/src/main/resources/static文件夹下,
如果用了thymeleaf 就放在/src/main/resources/template文件夹下.
这里我们用了thymelead,所有选择后者 - 有
@RequestMapping(一串url)
注解的函数,最后返回该html在static目录下的完整路径,不含html后缀。例如想以"localhost:8080/test"这个url,跳转到template文件夹下的test.html ,就让该函数return "test";
下面是两个例子:
/**
* 登录跳转:使"localhost:8080/admin/login"路径对应template 下的login.html 文件
*
* @param model
* @return
*/
@GetMapping("/admin/login")
public String loginGet(Model model) {
return "login";
}
@RequestMapping("/admin/newsManage_{pageCurrent}_{pageSize}_{pageCount}")
//表示该函数处理 括号中的url里的请求,包括get、post等
public String newsManage(News news,@PathVariable Integer pageCurrent,@PathVariable Integer pageSize,@PathVariable Integer pageCount, Model model) {
//blabala
return "news/newsManage"; //表示运用到static文件加下,news文件夹下,文件名为newsManage.html的html文件
}//这样一来/admin/newsManage_{pageCurrent}_{pageSize}_{pageCount} 这个路径显示的就是newsManage.html这个html文件的内容
遇到过的错误
- 启动错误:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentService' defined in file [C:UsersyuqiaoDesktop est argetclassescomexample estserviceStudentService.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Could not find value method on SQL annotation. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 66; XML 文档结构必须从头至尾包含在同一个实体内。
StudentService中的sql语句写错,漏删了