推荐使用 jdk1.8,我这里使用的是jdk9.0
右下角选自动导入就可以
//坐标在这里可以直接复制
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/eesy_mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
type: com.alibaba.druid.pool.DruidDataSource
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
}
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
即可使用@Data
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
}
@Mapper
public interface UserMapper {
//查询所有方法
@Select("select * from user")
public List<User> findAll();
}
@RestController
public class UserController {
//注入UserMapper 这里DEA可能会提示报错不用理会就行
@Autowired
UserMapper userMapper;
@GetMapping("/user")
public List<User> findAll(){
return userMapper.findAll();
}
}
<head>
<script src="js/jquery-1.12.4.min.js"></script>
<script>
$(function () {
//查询所有
var select_all = $("#select_btn");
select_all.click(function () {
$.ajax({
url: "/user",
type: "get",
success: function (result) {
var div = $("#div01");
for (var i = 0; i < result.length; i++) {
div.append("<p> 姓名:" + result[i].username + "</p >");
div.append("<p>生日:" + result[i].birthday + "</p >");
div.append("<p>性别:" + result[i].sex + "</p >");
div.append("<p>地址:" + result[i].address + "</p >");
}
}
}
)
});
})
</script>
</head>
<h1>这是首页!</h1>
<button id="select_btn">点击查询所有</button>
<div id="div01"></div>