记springboot+mybatis+freemarker+bootstrap的使…
2018-06-18 01:42:16来源:未知 阅读 ()
二、springboot+mybatis的使用
1.springboot的注解:@SpringBootApplication :启动项目:整合常用注解(@Configuration,@EnableAutoConfiguration,@ComponentScan)/扫包作用(只能在当前同级包下)
@EnableAutoConfiguration自动配置
@ComponentScan扫描一些组件如controller、service 可以扫多个包。扫多个包用法@ComponentScan(basePackages={"com.zty.controller","com.zty.service"})
@EntityScan扫描实体类的包
@MapperScan扫描Mapper类的包
@Controller 表明这是一个controller组件
@Service表明这是一个service组件
@Mapper表明这是一个Mybatis中的Mapper组件
@Autowired自动加载
@RequestMapping指定路由,负责URL到Controller中的具体函数的映射 参数有value method(可选)例:@RequestMapping(value="/login",method=RequestMethod.POST)
@Responsebody返回的字符串解析为Json格式
@RestController为rest风格的注解其中包含了@Responsebody
以上有想到的常用注解,更多注解请参考springboot官网
2.springboot项目结构

App包下是java的启动程序其中为项目启动的main方法,其中@EntityScan(basePackages={"com.zty.entity"})可以省略不要因为在@ComponentScan中已经包含了,代码如下
1 @ComponentScan(basePackages={"com.zty.controller","com.zty.service"}) 2 @EntityScan(basePackages={"com.zty.entity"}) 3 @MapperScan(basePackages={"com.zty.mapper"}) 4 @EnableAutoConfiguration 5 @SpringBootApplication 6 public class App { 7 8 public static void main(String[] args) { 9 SpringApplication.run(App.class, args); 10 } 11 }
controller代码示例如下
1 @Controller 2 public class LoginController { 3 @RequestMapping("/")//设置访问路径为域名根目录下 4 public String defaultView() { 5 return "login";//返回视图中的login.ftl模板 6 } 7 }
entity包为项目中的实体类,在这里省略
mapper包为mybatis中的mapper类,这里采用的是注解方式,代码示例如下(注意,实体类中的属性名要与数据库中的一致,不然mybatis不能自动注入)
1 @Mapper 2 public interface LoginMapper { 3 @Select("select id,name,password,depart,permission from user where name = #{name}") 4 User findUserByName(@Param("name") String name); 5 }
service代码示例如下
1 import com.zty.entity.User; 2 3 public interface LoginService { 4 User findUserByName(String name); 5 }
serviceimpl为service的具体实现,代码示例如下
1 @Service 2 public class LoginServiceImpl implements LoginService{ 3 @Autowired 4 private LoginMapper loginmapper; 5 @Override 6 public User findUserByName(String name) { 7 // TODO Auto-generated method stub 8 return loginmapper.findUserByName(name); 9 } 10 11 }
至此简单的一个项目后端已经完成,这里说一下静态资源的存放目录,springboot会自动在这几个目录下寻找资源
src/main/resources/static中存放的是图片或js、css、html之类的静态资源
src/main/resources/templates’中存放的是项目的模板文件(我用的模板是freemarker)
src/main/resources/static/webjars这是一个空目录,通过这个空目录可以访问bootstrap的资源(建空目录命名为webjars的原因可能是因为bootstrap的jar包名称为webjars吧)
来自本萌新的笔记,如果有误望各位大佬指正 :-p

标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:Java数据结构简述
下一篇:JSP标签和JSTL
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
