我对SpringBoot的理解很浅,只知道他是一个自动提供配置的好框架,对于Mybatis,现在还记得在mapper.xml里面写查询语言,通过resultType传递对象给Controller,有一次面试,他问我:如果通过左连接查询两张表,怎么返回数据对象,回答一下:定义一个ResultMap,type是你的左边查询的那张表名路径,里面加上你要查询的字段名及property,然后跟一个association type是你右边查询的那张表名路径,里面加上你要查询的字段名及property。当然还有查询是一对多,多对多的情况,那么你需要用到collection标签,有需要的可以再去了解一下。下面就开始实现。
1、SpringBoot创建
新建SpringBoot initializr--------在dependences里面选上spring web、MySQL、Mybatis、JDBC API--------创建
2、新建package,目录结构如下
3、各个页面代码1. entity.Jvm [Java] 纯文本查看 复制代码 package com.jff.sbootmybaties.entity;
public class Jvm {
private int id;
private int bytes;
private int instances;
private String class_name;
private int num;
public int getId() {
return id;
}
public int getBytes() {
return bytes;
}
public int getInstances() {
return instances;
}
public String getClass_name() {
return class_name;
}
public int getNum() {
return num;
}
public void setId(int id) {
this.id = id;
}
public void setBytes(int bytes) {
this.bytes = bytes;
}
public void setInstances(int instances) {
this.instances = instances;
}
public void setClass_name(String class_name) {
this.class_name = class_name;
}
public void setNum(int num) {
this.num = num;
}
}
2. dao.JvmDao [Java] 纯文本查看 复制代码 package com.jff.sbootmybaties.dao;
import com.jff.sbootmybaties.entity.Jvm;
import java.util.List;
public interface JvmDao {
List<Jvm> getAll();
Jvm getJvmById(int id);
void del(int id);
void update(Jvm jvm);
void insert(Jvm jvm);
}
3. service.JvmService [Java] 纯文本查看 复制代码 package com.jff.sbootmybaties.service;
import com.jff.sbootmybaties.dao.JvmDao;
import com.jff.sbootmybaties.entity.Jvm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class JvmService {
@Autowired
JvmDao jvmDao;
public List<Jvm> getAll(){
return jvmDao.getAll();
}
public Jvm getJvmById(int id){
return jvmDao.getJvmById(id);
}
public void insert(Jvm j){
jvmDao.insert(j);
}
public void update(Jvm j){
jvmDao.update(j);
}
public void del(int id){
jvmDao.del(id);
}
}
4. controller.JvmController [Java] 纯文本查看 复制代码 package com.jff.sbootmybaties.controller;
import com.jff.sbootmybaties.entity.Jvm;
import com.jff.sbootmybaties.service.JvmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("jvm")
public class JvmController {
@Autowired()
JvmService jvmService;
// @RequestMapping("/find")
// public ModelAndView find(){
// ModelAndView modelAndView=new ModelAndView();
// List<Jvm> list=jvmService.getAll();
// modelAndView.addObject("list",list);
// modelAndView.setViewName("/find.html");
// for (Jvm jvm:list){
// System.out.println(jvm.getId());
// System.out.println(jvm.getInstances());
// }
// return modelAndView;
// }
@RequestMapping("/find")
public ModelAndView find(@RequestParam(value="id") int id){
ModelAndView modelAndView=new ModelAndView("find");
Jvm jvm=jvmService.getJvmById(id);
modelAndView.addObject("jvm",jvm);
System.out.println(jvm.getId());
System.out.println(jvm.getInstances());
return modelAndView;
}
@RequestMapping(value = "/insert",method = RequestMethod.POST)
public ModelAndView insert(Jvm jvm){
jvmService.insert(jvm);
System.out.println("插入成功");
ModelAndView modelAndView=new ModelAndView("index");
return modelAndView;
}
@RequestMapping("/del")
public void del(@RequestParam int id){
jvmService.del(id);
}
}
5. application.yml [Java] 纯文本查看 复制代码 spring:
datasource:
name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3308/test?serverTimezone=UTC
username: root
password: 123456
Spring:
mvc:
view:
prefix: /
suffix: .html
resources:
static-locations: classpath:/static/
mybatis:
mapper-locations: classpath:templates/*.xml
server:
port: 8080
坑点对于SpringBoot项目,我想要在前端获取Controller返回的ModelAndView对象中获取jvm数组对象,但是html对于${}取值无法识别,网上说使用thymeleaf引擎可以通过th之类的成功引用,我试了一下,在pom中加入了
第一种pom: [Java] 纯文本查看 复制代码 <dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
第二种pom: [Java] 纯文本查看 复制代码 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
然后再application.yml配置thymeleaf,把html文件放在templates下面,发现跳转页面是404,html文件放回static文件下,发现并没有使用thymeleaf解析,我最后是定义了spring mvc view 的prefix:/与suffix:.html,页面可以跳转成功了,只是还是无法在html页面显示数据。
转载自CSDN 原文链接:https://blog.csdn.net/EndlessJF/article/details/106168208
|