一、在eclipse中创建跑得起来的springboot项目
用以下代码里面的内容覆盖掉项目里的pom.xml。覆盖后, 更新一下项目。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.how2java</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>springboot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在src/main/java下创建com.how2java.springboot包,在其下创建 Application.java,其注解 @SpringBootApplication 表示这是一个SpringBoot应用,运行其主方法就会启动tomcat,不需要手动启动tomcat了,默认端口是8080
package com.how2java.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
接着在src/main/java下创建com.how2java.springboot.web包接着创建控制器类HelloController, 这个类就是Spring MVC里的一个普通的控制器。@RestController 是spring4里的新注解,是springmvc中@ResponseBody和@Controller的缩写,作用相当于@ResponseBody+@Controller
package com.how2java.springboot.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello Spring Boot!Psyche!!!";
}
}
接下来就运行Application.java, 然后访问地址http://127.0.0.1:8080/hello
二、springboot中运用mybatis
在src/main/resources下创建application.properties,新增数据库链接必须的参数
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
在pom.xml中增加对mysql和mybatis的支持
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
在src/main/java下增加一个包com.how2java.springboot.pojo,然后创建实体类Category
package com.how2java.springboot.pojo;
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在src/main/java下增加一个包:com.how2java.springboot.mapper,然后创建接口CategoryMapper。使用注解@Mapper 表示这是一个Mybatis Mapper接口。使用@Select注解表示调用findAll方法会去执行对应的sql语句。
package com.how2java.springboot.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.how2java.springboot.pojo.Category;
@Mapper
public interface CategoryMapper {
@Select("select * from category_ ")
List<Category> findAll();
}
在com.how2java.springboot.web包中创建CategoryController 类。
1. 接受listCategory映射
2. 然后获取所有的分类数据
3. 接着放入Model中
4. 跳转到listCategory.jsp中
package com.how2java.springboot.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.how2java.springboot.mapper.CategoryMapper;
import com.how2java.springboot.pojo.Category;
@Controller
public class CategoryController {
@Autowired CategoryMapper categoryMapper;
@RequestMapping("/listCategory")
public String listCategory(Model m) throws Exception {
List<Category> cs=categoryMapper.findAll();
m.addAttribute("cs8", cs);
return "listCategory";
}
}
创建src/main/webapp/WEB-INF/jsp文件夹,在其下创建listCategory.jsp,用jstl遍历从CategoryController 传递过来的集合:cs8.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${cs8}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
</tr>
</c:forEach>
</table>
因为在pom.xml中增加了jar包的依赖,所以仅仅通过Springboot本身的热部署是无法起作用的,得手动重启一下。然后访问测试地址:http://127.0.0.1:8080/listCategory
先在eclipse中运行listCategory.jsp,再在浏览器中测试网址,否则打不开网址