spring Boot webx
来源:互联网 发布:淘宝2016开学爆到活动 编辑:程序博客网 时间:2024/06/06 01:38
spring Boot web项目
开发环境
JAVAversion 1.8Spring BootVersion 1.5.8IDEIntelliJ IDEA
请自行配置好环境变量什么的
--------
第一步
新建项目
File→New Project→Spring Initializr
这里选择默认的就行 想使用其他版本的自行上官网下载~
然后点击下一步
第二步 命名什么的自己起吧 最下面的3个会根据你起的名字自动更改不用去管它 直接下一步
language 那里可以选择kotlin(懒人必学的东西。。。
第三部
选择所需的东西
这里我们只要选择web mysql mybatis就行了其他的东西用不到 然后点击下一步就可以了
第4步 就是项目的保存位置什么的了自己选吧
-------上面就是创建spring boot web 项目的过程了
idea 下的spring 项目基本结构
你会神奇的发现 明明是web项目 但是却没有web.xml这些东西了。那是因为servlet3.0开始已经不需要web.xml,而spring 默认使用tomcat7.0 支持servlet 3.0 所以就不需要那种东西了。。
--------
接下来就很简单了 创建mapper,service,serviceimp,controller 以及pojo 五个包
打开pom.xml添加模板引擎 这里使用的是thymeleaf 除了这个当然还有其他的大家可以自己去谷歌
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
创建个数据库 以及表格goods id为主键 全部为非空
---------
pojo包下新建goods类 对应数据库中的表格
public class Goods { private int id; private String goodsname; private Integer billstatus; private Integer goodsdistrict; private BigDecimal goodsprice; private Integer goodscount; private Date creationtime; @Override public String toString() { return "Goods{" + "id=" + id + ", goodsname='" + goodsname + '\'' + ", billstatus=" + billstatus + ", goodsdistrict=" + goodsdistrict + ", goodsprice=" + goodsprice + ", goodscount=" + goodscount + ", creationtime=" + creationtime + '}'; } //set get 方法以下省略
mapper包下新建interface GoodsMapper类用于书写sql语句
@Repositorypublic interface GoodsMapper { @Select("select * from goods where id =#{id}") Goods selectByid(int id);}
service 包下新建GoodsService类
public interface GoodsService { public Goods query(int id);}
serviceimp包下新建Goodsserviceimpl类
@Service("goodsService")public class GoodsServiceImpl implements GoodsService { @Autowired private GoodsMapper goodsMapper; @Override public Goods query(int id) { return this.goodsMapper.selectByid(id); }}
创建数据库配置委文件datasource.properties
spring.datasource.url = jdbc:mysql://localhost:3306/自己数据库的名称?useUnicode=true&characterEncoding=utf-8spring.datasource.username = 写自己数据库的用户名spring.datasource.password = 密bao码
spring.datasource.driver = com.mysql.jdbc.Driver
这里使用默认的请导入正确的包
@MapperScan("me.exam1.goods.mapper")//mapper文件扫描@PropertySource("datasource.properties")@SpringBootApplicationpublic class GoodsApplication {public static void main(String[] args) {SpringApplication.run(GoodsApplication.class, args);}@Resourceprivate Environment env;public DataSource dataSource(){DataSource dataSource =new DataSource();dataSource.setUrl(env.getProperty("spring.datasource.url"));dataSource.setUsername(env.getProperty("spring.datasource.username"));//用户名dataSource.setPassword(env.getProperty("spring.datasource.password"));//密码dataSource.setDriverClassName(env.getProperty("spring.datasource.driver"));return dataSource;}}
接着创建页面,页面要创建在resource文件夹下的template里
新建个index.jsp 注意属性名字的大小写要和实体类的一样
<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head> <meta charset="UTF-8"/> <title>Title</title></head><body> <h1> Spring Boot 就是这么简单 </h1><h1 th:each="gs:${goodsList}"> <p th:text="${gs.goodsname}"></p></h1></body></html>
写着新建个controller
@RequestMapping("/home")@RestControllerpublic class IndexController { @Resource private GoodsService goodsService; @RequestMapping("/index") public ModelAndView index(){ Goods goods = goodsService.query(1); List<Goods> goodsList = new ArrayList<>(); goodsList.add(goods); Goods goods1=goodsService.query(2); goodsList.add(goods1); ModelAndView modelAndView = new ModelAndView("index"); modelAndView.addObject("goodsList", goodsList); return modelAndView; }
阅读全文
0 0
- spring Boot webx
- webX
- Webx
- webx
- Webx框架:Spring Schema 和 Spring Ext
- webx中Spring框架的基本流程
- 【Spring Boot】Spring Boot
- 结合Spring IOC 看Webx & HSF & Spring MVC
- spring boot
- spring boot
- spring-boot
- spring boot
- Spring Boot
- spring boot
- spring boot
- Spring Boot
- spring boot
- Spring Boot
- 编程语言介绍
- php开发者与composer的不得不说故事
- Linux常用的命令
- BootStrap
- Spring MVC的Controller统一异常处理:HandlerExceptionResolver
- spring Boot webx
- git删除远程仓库中的文件
- tcp长连接中客户端重连
- Retrofit(okhttp)的动态代理
- python3 Serial 串口助手的接收读取数据
- 详解Nginx虚拟主机配置中server_name的具体写法
- perl urlencode
- Ubuntu16.04卸载其他版本的opencv并安装opencv3.2.0+opencv contrib3.2.0
- 【第五届蓝桥杯】小朋友排队