一、Spring Boot项目的搭建
eclipse中 File-->new-->project
二、Spring Boot与Mybatis的集成
1.Mysql数据库
CREATE DATABASE microservice
CREATE TABLE tb_user(
id INT(32) PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(32),
address VARCHAR(256)
);
INSERT INTO tb_user VALUES('1','小韩','北京市海定区');
INSERT INTO tb_user VALUES('2','小侍','北京市昌平`microservice`区');
INSERT INTO tb_user VALUES('3','小陈','兰州市顺义区');
2.pom.xml文件中添加依赖,保存,eclipse会自动导入。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
3.application.properties文件添加配置,视情况而定,自己的数据库名、端口号、用户名和密码。
spring.datasource.url=jdbc:mysql://localhost:3306/microservice
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
4.后端代码编写:
4.1 在src/main/java、com.example包下建立四个包,分别为controller、mapper、po和service。选中com.example右击,new–>Package,
4.2 在po包下创建实体类User.java
实体类User.java代码如下:
package com.example.po;
public class User {
private Integer id;
private String username;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id)
{
this.id=id;
}
public String getUsername() {
return username;
}
public void setUsername(String username)
{
this.username=username;
}
public String getAddress() {
return address;
}
public void setAddress(String address)
{
this.address=address;
}
public User() {
super();
}
public User(Integer id,String username,String address) {
super();
this.id=id;
this.username=username;
this.address=address;
}
}
4.3 在mapper包下创建UserMapper.java
UserMapper.java代码如下:
package com.example.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.example.po.User;
@Mapper
public interface UserMapper {
//查询所有用户
@Select("select * from tb_user")
List<User> getAllUsers();
//删除用户
@Delete("Delete FROM tb_user WHERE id=#{id}")
void delete(Integer id);
}
4.4 在service包下创建接口UserService.java
UserService.java代码如下:
package com.example.service;
import java.util.List;
import com.example.po.User;
public interface UserService {
//查询所有
List<User> getAllUsers();
//删除数据
void deleteUser(Integer id);
}
4.5 在service包下新建包impl包,在impl包中新建UserService.java的实现类UserServiceImpl.java
UserServiceImpl.java代码如下:
package com.example.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.mapper.UserMapper;
import com.example.po.User;
import com.example.service.UserService;
@Service
@Transactional
public class UserServiceImpl implements UserService{
//注入用户Mapper
@Autowired
private UserMapper userMapper;
//查询所有用户
public List<User> getAllUsers(){
return this.userMapper.getAllUsers();
}
//删除用户
public void deleteUser(Integer id) {
System.out.println("删除了id为"+id+"的用户");
this.userMapper.delete(id);
}
}
4.6 在controller包下新建UserController.java
UserController.java代码如下:
package com.example.controller;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import com.example.po.User;
import com.example.service.UserService;
//这时要引入Web模块,需在pom.xml添加spring-boot-starter-web模块
@RestController
@RequestMapping("/user")
public class UserController {
//注入用户Service
@Autowired
private UserService userService;
//查询所有用户
@RequestMapping("/userList")
public List<User> getAllUsers(){
List<User> list=this.userService.getAllUsers();
return list;
}
//删除用户
@RequestMapping("/delete/{id}")
public void delete(@PathVariable Integer id) {
this.userService.deleteUser(id);
}
}
5.前端页面的编写
5.1 将Easy UI框架的资源文件拷贝到项目src/main/resources目录下的static文件夹中。
Easy UI框架的资源文件位置----自己百度下载
5.2在static文件夹下新建user.html
user.html代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户信息</title>
<link rel="stylesheet" type="text/css" href="ui/themes/default/easyui.css" >
<link rel="stylesheet" type="text/css" href="ui/themes/icon.css" >
<script type="text/javascript" src="ui/jquery.min.js"></script>
<script type="text/javascript" src="ui/jquery.easyui.min.js" ></script>
<script type="text/javascript" src="ui/locale/easyui-lang-zh_CN.js" ></script>
<script type="text/javascript" >
$(function(){
$('#grid').datagrid({
url:'user/userList',
fit:true,
columns:[[
{field:'id',title:'编号',width:50},
{field:'username',title:'姓名',width:200},
{field:'address',title:'地址',width:200},
{field:'del',title:'删除',width:100}
]]
});
});
</script>
</head>
<body>
<table id="grid"> </table>
</body>
</html>
6.启动游览器,输入localhost:8080/user.html进行测试