springboot入门(1)—整合MyBatis
1、快速创建springboot项目
1.1、选择spring initializr,默认的为外网访问,选择customer,用阿里的服务地址:
https://start.aliyun.com/
1.2、next,选择web。
2、整合MyBatis
2.1、添加MyBatis起步依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
2.2、添加mysql驱动:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2.3添加数据库连接信息
mybatis.type-aliases-package=com.zdww.domain
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/bk?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1405284875
3、代码的编写
3.1、domain层
package com.zdww.domain;
/**
* @ClassName:User
* @Auther: crazy
* @Date: 2020/8/1 14:29
* @Description: TODO
* @Version:1.0
*/
public class User {
private String uname;
private String mail;
private String account;
private String role;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getmail() {
return mail;
}
public void setmail(String mail) {
this.mail = mail;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@Override
public String toString() {
return "User{" +
"uname='" + uname + '\'' +
", mail='" + mail + '\'' +
", account='" + account + '\'' +
", role='" + role + '\'' +
'}';
}
}
3.2、mapper层
package com.zdww.mapper;
import com.zdww.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @ClassName:UserMapper
* @Auther: crazy
* @Date: 2020/8/1 14:31
* @Description: TODO
* @Version:1.0
*/
@Mapper
public interface UserMapper {
@Select("select * from tuser")
public List<User> queryUserList();
}
3.3、ctroller层
package com.zdww.ctroller;
import com.zdww.domain.User;
import com.zdww.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @ClassName:MapperController
* @Auther: crazy
* @Date: 2020/8/1 14:35
* @Description: TODO
* @Version:1.0
*/
@CrossOrigin
@Controller
public class MapperController {
@Autowired
private
## UserMapper userMapper;
@RequestMapping("/queryUser")
@ResponseBody
public List<User> queryUser() {
List<User> users = userMapper.queryUserList();
return users;
}
}
4、调试运行