IDEA+tomcat+spring-boot+mysql+mybatis
CREATE TABLE users
(
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
salary float(10) DEFAULT NULL,
PRIMARY KEY (id)
);
Users.java
package com.china.andrew.wanson.model;
public class Users {
private String name;
private Long salary;
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getSalary() {
return salary;
}
public void setSalary(Long salary) {
this.salary = salary;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
application.properties
server.port=9095
spring.datasource.url=jdbc:mysql://localhost:3306/employee
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
WansonApplication.java
package com.china.andrew.wanson;
import com.china.andrew.wanson.model.Users;
import org.apache.ibatis.type.MappedTypes;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MappedTypes(Users.class)
@MapperScan("com.china.andrew.wanson.mapper")
@SpringBootApplication
public class WansonApplication {
public static void main(String[] args) {
SpringApplication.run(WansonApplication.class, args);
}
}
``
![](https://images2018.cnblogs.com/blog/1404925/201808/1404925-20180827185453848-901359228.png)
# UsersResource.java
```java
package com.china.andrew.wanson.resource;
import com.china.andrew.wanson.mapper.UsersMapper;
import com.china.andrew.wanson.model.Users;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/rest/users")
public class UsersResource {
public UsersResource() {
}
public UsersResource(UsersMapper usersMapper) {
this.usersMapper = usersMapper;
}
private UsersMapper usersMapper;
@GetMapping("/all")
public List<Users> getAll(){
return usersMapper.findAll();
}
}
UserMapper.java
package com.china.andrew.wanson.mapper;
import com.china.andrew.wanson.model.Users;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UsersMapper {
@Select("select * from users")
List<Users> findAll();
}
posted on 2018-08-27 18:55 Indian_Mysore 阅读(1168) 评论(1) 编辑 收藏 举报