1. 业务逻辑介绍
在这里插入图片描述在这里插入图片描述
- controller: 控制层,负责具体模块的业务流程控制,需要调用service逻辑设计层的接口来控制业务流程, 与前端接口的对接和每个功能接口的实现方法都写在这里,可以说是代码的主干部分。
- service:业务层,给controller层的类提供接口进行调用。一般就是自己写的方法封装起来,就是声明一下,具体实现在serviceImpl中。
- model:与数据库中的表对应,实现set和get的方法。
- mapper:通常我们在Mapper层里面写接口,里面有与数据打交道的方法。SQL语句通常写在xml文件里面。
2. 添加mybatis相关依赖
在这里插入图片描述3. mybatis全局配置文件
sqlMapConfig.xml
.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
4. 各部分代码
User.java
.
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
UserServiceImpl.java
.
package com.olivia.demo.service.impl;
import java.util.List;
import java.util.Map;
import com.olivia.demo.mapper.UserMapper;
import com.olivia.demo.model.User;
import com.olivia.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAllUser() {
List<User> list = userMapper.findAll();
return list;
}
}
UserService.java
.
package com.olivia.demo.service;
import com.olivia.demo.model.User;
import java.util.List;
public interface UserService {
/**
* 根据接口查询所用的用户
*/
public List<User> findAllUser();
}
UserMapper.java
.
import com.olivia.demo.model.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
public List<User> findAll();
}
UserController.java
.
package com.olivia.demo.controller;
import java.util.List;
import com.olivia.demo.model.User;
import com.olivia.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/")
public List<User> list(){
List<User> list = userService.findAllUser();
System.out.println(list);
return list;
}
}
UserMapper.xml
.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.olivia.demo.mapper.UserMapper">
<select id="findAll" resultType="com.olivia.demo.model.User">
select * from user
</select>
</mapper>
5. 配置文件以及数据库
application.properties
logging.level.org.springframework=DEBUG
#springboot mybatis
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
mybatis.type-aliases-package = com.olivia.demo.model
#数据库
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/springboot_test
spring.datasource.username = root
spring.datasource.password = 123qwe
在这里插入图片描述
在这里插入图片描述
6. 运行项目
第一次运行项目的时候,发现报错了,提示如下
原因可能是由于数据库和系统时区差异所造成的,解决方法:修改配置文件中的spring.datasource.url属性,如下:
application.properties
logging.level.org.springframework=DEBUG
#springboot mybatis
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
mybatis.type-aliases-package = com.olivia.demo.model
#数据库
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/springboot_test?serverTimezone=GMT
spring.datasource.username = root
spring.datasource.password = 123qwe
再次运行项目
在这里插入图片描述
网友评论