使用idea搭建springboot+mybatis项目

使用idea搭建springboot+mybatis项目_第1张图片

填写项目名

使用idea搭建springboot+mybatis项目_第2张图片

选择web,spring web

使用idea搭建springboot+mybatis项目_第3张图片

next,然后finish

springboot项目即搭建完成

创建controller包,HelloController类

使用idea搭建springboot+mybatis项目_第4张图片

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
}

浏览器输入地址:http://localhost:8080/hello

使用idea搭建springboot+mybatis项目_第5张图片

整合thymeleaf

pom.xml添加如下


    org.springframework.boot
    spring-boot-starter-thymeleaf


    org.springframework.boot
    spring-boot-starter-web

application.properties文件添加如下配置

#是否开启缓存
spring.thymeleaf.cache=true
#检查模板是否存在
spring.thymeleaf.check-template=true
#检查模板位置是否存在
spring.thymeleaf.check-template-location=true
#模板文件编码
spring.thymeleaf.encoding=UTF-8
#模板文件位置
spring.thymeleaf.prefix=classpath:/templates/
#content-type配置
spring.thymeleaf.servlet.content-type=text/html
#模板文件后缀
spring.thymeleaf.suffix=.html

添加实体类和controller

实体类:

public class Book {
    private int id;
    private String name;
    private String author;

    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;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

controller

@Controller
public class BookController {
    @GetMapping("/books")
    public ModelAndView books(){
        List list = new ArrayList<>();
        Book b1 = new Book();
        Book b2 = new Book();
        Book b3 = new Book();
        b1.setId(1);
        b1.setName("三国");
        b1.setAuthor("罗贯中");
        b2.setId(2);
        b2.setName("水浒");
        b2.setAuthor("施耐庵");
        b3.setId(3);
        b3.setName("红楼");
        b3.setAuthor("曹雪芹");
        list.add(b1);
        list.add(b2);
        list.add(b3);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("books",list);
        modelAndView.setViewName("books");
        return modelAndView;
    }
}

使用idea搭建springboot+mybatis项目_第6张图片

templates下新增books.html




    
    Title


    
编号 名称 作者

使用idea搭建springboot+mybatis项目_第7张图片

访问地址http://localhost:8080/books

使用idea搭建springboot+mybatis项目_第8张图片

整合mybatis

pom.xml文件新增


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.3.2


    com.alibaba
    druid
    1.1.9


    mysql
    mysql-connector-java
    runtime

application.properties配置文件中新增

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/bootdo1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456

mybatis.mapper-locations=classpath:mapper/*.xml

使用idea搭建springboot+mybatis项目_第9张图片

dao包下创建BookMapper,service下创建IBookService接口和BookService实现类,resources文件夹下创建mapper文件夹,以及BookMapper.xml文件

使用idea搭建springboot+mybatis项目_第10张图片

BookMapper

@Mapper
public interface BookMapper {
    List getAllBooks();
}

IBookService

public interface IBookService {
    List getList();
}

BookService

@Service
public class BookService implements IBookService {
    @Autowired
    BookMapper bookDao;
    public List getList(){
        return bookDao.getAllBooks();
    }
}

BookMapper.xml




    

BookMapper.xml中namespace的地址即为dao目录下的BookMapper地址,id与BookMapper.java中的的方法名相同

最后,修改BookController

@Controller
public class BookController {
    @Autowired
    public IBookService service;

    @GetMapping("/books")
    public ModelAndView books(){
//        List list = new ArrayList<>();
//        Book b1 = new Book();
//        Book b2 = new Book();
//        Book b3 = new Book();
//        b1.setId(1);
//        b1.setName("三国");
//        b1.setAuthor("罗贯中");
//        b2.setId(2);
//        b2.setName("水浒");
//        b2.setAuthor("施耐庵");
//        b3.setId(3);
//        b3.setName("红楼");
//        b3.setAuthor("曹雪芹");
//        list.add(b1);
//        list.add(b2);
//        list.add(b3);
        List list = service.getList();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("books",list);
        modelAndView.setViewName("books");
        return modelAndView;
    }
}

浏览器输入地址:localhost:8080/books,即可访问到数据库中查询到的数据

使用idea搭建springboot+mybatis项目_第11张图片

使用idea搭建springboot+mybatis项目_第12张图片

 

注:还有一种方法,application.properties配置文件中,注释掉

#mybatis.mapper-locations=classpath:mapper/*.xml

BookMapper.xml文件中的内容也注释掉,或直接删除BookMapper.xml

使用idea搭建springboot+mybatis项目_第13张图片

BookMapper接口方法中,添加@Select注解,如下所示

使用idea搭建springboot+mybatis项目_第14张图片

你可能感兴趣的:(使用idea搭建springboot+mybatis项目)