spring Boot webx

spring Boot web项目
开发环境
  
JAVAversion 1.8
Spring BootVersion 1.5.8
IDEIntelliJ 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语句
@Repository
public 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-8
spring.datasource.username = 写自己数据库的用户名
spring.datasource.password = 密bao码
spring.datasource.driver = com.mysql.jdbc.Driver
返回springboot的启动类里进行添加数据配置(spring boot 默认使用的是tomcat-jdbc 要使用其他的只需要在pom添加就行了。)
这里使用默认的请导入正确的包
@MapperScan("me.exam1.goods.mapper")//mapper文件扫描
@PropertySource("datasource.properties")
@SpringBootApplication
public class GoodsApplication {

	public static void main(String[] args) {
		SpringApplication.run(GoodsApplication.class, args);
	}

	@Resource
	private 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")
@RestController
public 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;
    }

最后启动程序 成功显示 注意路径 这里的路径要和你自己的controller层的requestmapping的地址一致



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值