第一步 :
idea创建一个Springboot springMvc mybatis项目 File -> New -> Project -> 选择spring initializr -> next->->next->选择web ->打勾spring web starter->next->确认
这样就创建好一个springboot结构项目了然后配置
第二步 :
配置依赖pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency> <!--此处是配置lombok 让实体更方便一些吧-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第三步 配置application.properties
server.port=8080#端口号
#这里配置数据库路径注意serverTimezone这个是时区问题
spring.datasource.url=jdbc:mysql://localhost:3306/库名?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
#账号
spring.datasource.username=root
#密码
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# mybatis接口文件位置
mybatis.mapper-locations: classpath:mapper/*.xml
第四步创建包 整体结构
第五步 配置里面需要配一下DemoApplication
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.mapper")//扫描包
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
第六步 写POJO
package com.example.demo.pojo;
import lombok.*;
@Data
@Builder
@AllArgsConstructor
@RequiredArgsConstructor //这里封装用了Lombok很实用减少代码量
public class User {
private int userId;
private String userName;
}
第七步 写Mapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> query();
}
第八步 写Mapper.xml SQL映射userMapper接口,配置一个查询sql此id与mapper接口保持与方法名对应
<?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.example.demo.mapper.UserMapper">
<select id="query" resultType="com.example.demo.pojo.User">
select USER_ID as userId,USER_NAME as userName from USER
</select>
</mapper>
第九步 写Service ServiceImpl
package com.example.demo.service;
import com.example.demo.pojo.User;
import java.util.List;
public interface UserService {
List<User> query();
}
package com.example.demo.service.Impl;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper UserMapper;
@Override
public List<User> query() {
return UserMapper.query();
}
}
第十步 写controller
package com.example.demo.controller;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/user")
@ResponseBody
public List<User> query(){
List<User> query = userService.query();
System.out.println(query.size());
for (User d:query) {
System.out.println( d.getUserId());
}
return query;
}
}