山东大学 暑期实训 云主机服务比价与预测系统(九)Spring boot整合 Mybatis
Spring boot作为一个后端常用的综合性架构,它还能够在自身基础上整合其他的框架,以达成某些代码或结构要求。
今天我们使用的是Spring boot 和Mybatis 整合
1.创建一个maven工程,结构如下
2.修改pom.xml中的依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.设定一个实体类user ,定义几个简单的get ,set方法
import org.springframework.stereotype.Component;
@Component
public class User {
private int id;
private String name;
private String telephone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4 接口
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.lee.test.pojo.User;
@Mapper
public interface UserMapper {
List<User> getUser(int id);
}
public interface UserService {
public List<User> getUser(int id);}
6接口实现
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUser(int id) {
return userMapper.getUser(id);
}
}
7建立controller 实现操作
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.lee.test.pojo.User;
import com.lee.test.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getUser")
public List<User> getUser(@RequestParam("id") int id) {
return userService.getUser(id);
}
}
8在配置文件properties中
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations: classpath:mapper/*.xml