IDEA + Spring Boot + Maven + MySQL + Mybatis 组合
1. 创建 Spring Boot
2. Hello,Word 完成
引入Mybatis
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
加入MySQL地址
server.port= 8008
spring.datasource.url=jdbc:mysql://***.***.*.**:3306/*****?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=*****
@RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
@ResponseBody
public String getHello() {
return "Hello,Word!!!";
}
3. Mybatis 、MySQL 数据输出到页面展示
实体类
package com.example.demo.model;
public class User {
private Integer id;
private String company;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
DAO类
package com.example.demo.dao;
import com.example.demo.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserDao {
// MyBatis 的注解
@Select("select id, company from suppliers")
public List<User> getUserList();
}
新增User输出数据验证
package com.example.demo.controller;
import com.example.demo.dao.UserDao;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
@ResponseBody
public String getHello() {
return "Hello,Word!!!";
}
@RequestMapping(value = "/user", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
@ResponseBody
public List<User> getUser() {
return userDao.getUserList();
}
}
自此组合完毕,如果途中遇到报错,记得 install 、clean 、或刷新下。可能是没有下载导致的报错
祝愿各位,技术成长的道路上,不断摸索,不断创新,不断成长、
活着就是为了改变世界