环境搭建可以查看我的上一篇文章
基于SpringBoot+Mybatis+Thymeleaf的XX管理系统实现一(准备+环境搭建)
这一部分实现controller层、entity层、service层的相关代码,先把spring的hello world跑通
也是对第一步配环境的检查和对spring DI和IOC思想的理解
controller层——控制层,常用于接受request请求,返回response请求
entity层——实体层,主要保存数据库实体的实体类,
service层——用于对服务进行实现,逻辑进行实现
mapper层——用于存放DB操作的接口类
注:在resource下也要与mapper层有对应的文件夹和对应的文件名存放sql的xml文件,并进行配置
mysql就可以寻找到,resource的创建目录时,如果写成com.anthony.infosystem不能创建三级,只创建一级目录
因此,使用如上的创建方式即可创建三级。
再到这个infosystem下创建mapper目录,存放xml文件
将不同层的Java类文件创建好之后,如图所示,
然后开始逐个编写代码。 先实现最基本的功能 写个简单的springboot+mybatis的hello world
即 使用select * from table语句实现打印全部,连接数据库成功
所有代码如下:
controller
package com.anthony.infosystem.controller;
import com.anthony.infosystem.entity.Bond;
import com.anthony.infosystem.service.IBondService;
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 org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class controller {
@Autowired
IBondService bondService;
@RequestMapping("/showAll")
public String PrintAll(Model model)
{
List bondList=bondService.findAllRecord();
model.addAttribute("list",bondList);
return "HelloWorld";
}
}
实体类Bond (getter和setter、tostring需要右键由IDEA生成,字段名字和数据库要对应)
package com.anthony.infosystem.entity;
import java.util.Date;
public class Bond {
Integer id;
String bonds_name;
String sales_name;
Integer amount;
Date created_at;
Date updated_at;
public Bond() {
}
@Override
public String toString() {
return "Bond{" +
"id=" + id +
", bonds_name='" + bonds_name + '\'' +
", sales_name='" + sales_name + '\'' +
", amount=" + amount +
", created_at=" + created_at +
", updated_at=" + updated_at +
'}';
}
public Bond(Integer id, String bonds_name, String sales_name, Integer amount, Date created_at, Date updated_at) {
this.id = id;
this.bonds_name = bonds_name;
this.sales_name = sales_name;
this.amount = amount;
this.created_at = created_at;
this.updated_at = updated_at;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBonds_name() {
return bonds_name;
}
public void setBonds_name(String bonds_name) {
this.bonds_name = bonds_name;
}
public String getSales_name() {
return sales_name;
}
public void setSales_name(String sales_name) {
this.sales_name = sales_name;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public Date getCreated_at() {
return created_at;
}
public void setCreated_at(Date created_at) {
this.created_at = created_at;
}
public Date getUpdated_at() {
return updated_at;
}
public void setUpdated_at(Date updated_at) {
this.updated_at = updated_at;
}
}
Mapper
package com.anthony.infosystem.mapper;
import com.anthony.infosystem.entity.Bond;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface BondMapper {
List findAll();
}
service接口
package com.anthony.infosystem.service;
import com.anthony.infosystem.entity.Bond;
import java.util.List;
public interface IBondService {
List findAllRecord();
}
service实现类
package com.anthony.infosystem.service.impl;
import com.anthony.infosystem.entity.Bond;
import com.anthony.infosystem.mapper.BondMapper;
import com.anthony.infosystem.service.IBondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BondServiceImpl implements IBondService {
@Autowired
BondMapper bondMapper;
@Override
public List findAllRecord() {
return bondMapper.findAll();
}
}
mapper的xml文件(注意:同名、路径必须相同)
application.properties文件 (注意路径和mysql的url 这里改了端口号为8077)
#mybatis连接数据库的参数
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/group3?characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong
spring.datasource.username=root
spring.datasource.password=1234
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/
#mybatis的配置
mybatis.type-aliases-package=com.anthony.infosystem.entity
mybatis.mapper-locations=classpath:com/anthony/infosystem/mapper/*.xml
server.port=8077
spring.thymeleaf.cache=false
HelloWorld.html
债券查询结果
债券查询结果
债券类型
债券销售经理
交易价格
创建时间
修改时间
A
张三
1
4
X
然后运行并访问localhost:8077/showAll即可查看数据
必须保证mysql数据库服务是开的、url正确、里面有数据!!!