這是一個(gè)SpringBoot框架整合Mybatis的小Demo
使用的開發(fā)工具: idea
使用的數(shù)據(jù)庫(kù): MySQL
下面開始~
1)、新建一個(gè)Spring Initializr

2)、自己定義下項(xiàng)目的名字(圖略)

3)、選擇需要的依賴

4)、設(shè)置好項(xiàng)目名稱后,先看 pom.xml 文件配置(這里主要關(guān)注的是依賴是否齊全)
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
5)、配置application.xml (這里我用的是application.yml 該格式更為簡(jiǎn)潔美觀)
spring 配置數(shù)據(jù)庫(kù)的信息
spring:
datasource:
username: root
password: Ab123456
url: jdbc:mysql://localhost:3306/emp
driver-class-name: com.mysql.jdbc.Driver
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
這里的目的是把thymeleaf的緩存設(shè)置為禁用
thymeleaf:
cache=false
mybatis 的配置 locations 是mapper.xml的路徑
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
6)、數(shù)據(jù)庫(kù)建表測(cè)試(mysql)
create table emp(
emp_id int(18) primary key,
org_id int(18),
empcode varchar(32),
empname varchar(64),
sex char(1),
birthday date,
postcode char(6),
address varchar(128),
phone varchar(32),
wage double(6,1)
)
insert into emp values('1','1','001','張三豐','M',date_format('1957-01-25','%Y-%m-%d'),'710071','北京海淀區(qū)中關(guān)村大街01號(hào)','80080001',2000);
insert into emp values('2','1','002','張無(wú)忌','M',date_format('1972-11-30','%Y-%m-%d'),'710070','上海張江高科技園區(qū)碧波路456號(hào)','80080002',2000);
insert into emp values('3','1','003','張小寶','M',date_format('1974-05-19','%Y-%m-%d'),'710072','北京市海淀區(qū)彩和坊路8號(hào)天創(chuàng)科技大廈東門1301室','80080003',2000);
insert into emp values('4','1','004','趙大海','M',date_format('1977-08-07','%Y-%m-%d'),'710073','廣州市天河區(qū)體育東路','80080004',2000);
insert into emp values('5','1','005','趙小新','M',date_format('1956-04-19','%Y-%m-%d'),'710074','北京海淀區(qū)中關(guān)村22號(hào)','80080005',2000);
7)、先貼一下目錄結(jié)構(gòu)

enity -- 新建實(shí)體類 Emp
public class Emp {
private Integer emp_id;
private Integer org_id;
private String empCode;
private String empName;
private String sex;
private Date birthday;
private String postCode;
private String address;
private String phone;
private double wage;
public Integer getEmp_id() {
return emp_id;
}
public void setEmp_id(Integer emp_id) {
this.emp_id = emp_id;
}
public Integer getOrg_id() {
return org_id;
}
public void setOrg_id(Integer org_id) {
this.org_id = org_id;
}
public String getEmpCode() {
return empCode;
}
public void setEmpCode(String empCode) {
this.empCode = empCode;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
@Override
public String toString() {
return "Emp{" +
"emp_id=" + emp_id +
", org_id=" + org_id +
", empCode='" + empCode + '\'' +
", empName='" + empName + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
", postCode='" + postCode + '\'' +
", address='" + address + '\'' +
", phone='" + phone + '\'' +
", wage=" + wage +
'}';
}
}
8)、由于這里使用的持久化框架是Mybatis,所以dao層被Mapper替換. 新建EmpMapper
注意注釋不要忘了
@Repository
public interface EmpMapper {
List<Emp> queryAllEmp();
}
9)、Service層調(diào)用dao層(Mapper),新建 EmpService
@Service
public class EmpService {
@Autowired
EmpMapper empMapper;
public List<Emp> queryAllEmp(){
return empMapper.queryAllEmp();
}
}
10)、Controller層調(diào)用 Service層獲取數(shù)據(jù), 新建EmpController
這里我使用的返回值類型是ModelAndView(視圖),new 的時(shí)候可以直接傳入一個(gè)字符串,里面就寫你跳轉(zhuǎn)的頁(yè)面即可,不需要添加后綴.(這里使用的是html頁(yè)面,若使用jsp,則可以設(shè)置后綴為jsp,在配置文件application.yml
mvc:
view:
prefix: /WEB-INF/ (靜態(tài)頁(yè)面的目錄)
suffix: .jsp) (靜態(tài)頁(yè)面的后綴)
@RestController
@RequestMapping(value = "/emp")
public class EmpController {
@Autowired
private EmpService empService;
@RequestMapping(value = "/getAllEmp")
public ModelAndView getAllEmp() {
ModelAndView mav = new ModelAndView("list");
List<Emp> emps = empService.queryAllEmp();
mav.addObject("emps", emps);
return mav;
}
}
11)、完成上述步驟后,需要在啟動(dòng)類上添加一個(gè)@MapperScan 注釋指定掃描的mapper路徑
注: 每個(gè)人的啟動(dòng)類名字不一定相同
@MapperScan("com.zrr.empmgr.mapper")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
12)、在啟動(dòng)類上運(yùn)行項(xiàng)目,在瀏覽器上輸入 localhost:8080/emp/getAllEmp 進(jìn)行測(cè)試

好了,剩下的可以自己去拓展了~ 增刪改查(mybatis.xml 的 標(biāo)簽更改為 相應(yīng)的標(biāo)簽)
如刪除 <delect></delete> 新增<insert></insert> 更新<update></update>
附上html文件源碼(css文件/js文件無(wú)法上傳)
html 標(biāo)簽加入 xmlns:th="http://www.thymeleaf.org" 可以在使用th語(yǔ)法的時(shí)候出現(xiàn)提示
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Dashboard Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link href="asserts/css/bootstrap.min.css" th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/css/dashboard.css" th:href="@{/asserts/css/dashboard.css}" rel="stylesheet">
<style type="text/css">
/* Chart.js */
@-webkit-keyframes chartjs-render-animation {
from {
opacity: 0.99
}
to {
opacity: 1
}
}
@keyframes chartjs-render-animation {
from {
opacity: 0.99
}
to {
opacity: 1
}
}
.chartjs-render-monitor {
-webkit-animation: chartjs-render-animation 0.001s;
animation: chartjs-render-animation 0.001s;
}
</style>
</head>
<body>
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0">
<a class="navbar-brand col-sm-3 col-md-2 mr-0" >Company name</a>
<input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">
<ul class="navbar-nav px-3">
<li class="nav-item text-nowrap">
<a class="nav-link" >Sign out</a>
</li>
</ul>
</nav>
<div class="container-fluid">
<div class="row">
<nav class="col-md-2 d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active" >
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home">
<path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
<polyline points="9 22 9 12 15 12 15 22"></polyline>
</svg>
Dashboard <span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" >
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file">
<path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path>
<polyline points="13 2 13 9 20 9"></polyline>
</svg>
Orders
</a>
</li>
<li class="nav-item">
<a class="nav-link" >
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-shopping-cart">
<circle cx="9" cy="21" r="1"></circle>
<circle cx="20" cy="21" r="1"></circle>
<path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path>
</svg>
Products
</a>
</li>
<li class="nav-item">
<a class="nav-link" >
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-users">
<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
<circle cx="9" cy="7" r="4"></circle>
<path d="M23 21v-2a4 4 0 0 0-3-3.87"></path>
<path d="M16 3.13a4 4 0 0 1 0 7.75"></path>
</svg>
Customers
</a>
</li>
<li class="nav-item">
<a class="nav-link" >
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-bar-chart-2">
<line x1="18" y1="20" x2="18" y2="10"></line>
<line x1="12" y1="20" x2="12" y2="4"></line>
<line x1="6" y1="20" x2="6" y2="14"></line>
</svg>
Reports
</a>
</li>
<li class="nav-item">
<a class="nav-link" >
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-layers">
<polygon points="12 2 2 7 12 12 22 7 12 2"></polygon>
<polyline points="2 17 12 22 22 17"></polyline>
<polyline points="2 12 12 17 22 12"></polyline>
</svg>
Integrations
</a>
</li>
</ul>
<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Saved reports</span>
<a class="d-flex align-items-center text-muted" >
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-plus-circle"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="16"></line><line x1="8" y1="12" x2="16" y2="12"></line></svg>
</a>
</h6>
<ul class="nav flex-column mb-2">
<li class="nav-item">
<a class="nav-link" >
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
<polyline points="14 2 14 8 20 8"></polyline>
<line x1="16" y1="13" x2="8" y2="13"></line>
<line x1="16" y1="17" x2="8" y2="17"></line>
<polyline points="10 9 9 9 8 9"></polyline>
</svg>
Current month
</a>
</li>
<li class="nav-item">
<a class="nav-link" >
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
<polyline points="14 2 14 8 20 8"></polyline>
<line x1="16" y1="13" x2="8" y2="13"></line>
<line x1="16" y1="17" x2="8" y2="17"></line>
<polyline points="10 9 9 9 8 9"></polyline>
</svg>
Last quarter
</a>
</li>
<li class="nav-item">
<a class="nav-link" >
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
<polyline points="14 2 14 8 20 8"></polyline>
<line x1="16" y1="13" x2="8" y2="13"></line>
<line x1="16" y1="17" x2="8" y2="17"></line>
<polyline points="10 9 9 9 8 9"></polyline>
</svg>
Social engagement
</a>
</li>
<li class="nav-item">
<a class="nav-link" >
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
<polyline points="14 2 14 8 20 8"></polyline>
<line x1="16" y1="13" x2="8" y2="13"></line>
<line x1="16" y1="17" x2="8" y2="17"></line>
<polyline points="10 9 9 9 8 9"></polyline>
</svg>
Year-end sale
</a>
</li>
</ul>
</div>
</nav>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<h2>Section title</h2>
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th>員工ID</th>
<th>機(jī)構(gòu)ID</th>
<th>員工編號(hào)</th>
<th>員工姓名</th>
<th>性別</th>
<th>出生日期</th>
<th>郵政編碼</th>
<th>聯(lián)系地址</th>
<th>聯(lián)系電話</th>
<th>員工薪資</th>
</tr>
</thead>
<tbody>
<tr th:each="emp:${emps}">
<td th:text="${emp.emp_id}">1,001</td>
<td th:text="${emp.org_id}">Lorem</td>
<td th:text="${emp.empCode}">ipsum</td>
<td th:text="${emp.empName}">dolor</td>
<td th:text="${emp.sex}">sit</td>
<td th:text="${emp.birthday}">sit</td>
<td th:text="${emp.postCode}">sit</td>
<td th:text="${emp.address}">sit</td>
<td th:text="${emp.phone}">sit</td>
<td th:text="${emp.wage}">sit</td>
</tr>
</tbody>
</table>
</div>
</main>
</div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script type="text/javascript" th:href="@{/asserts/js/jquery-3.2.1.slim.min.js}" src="asserts/js/jquery-3.2.1.slim.min.js"></script>
<script type="text/javascript" th:href="@{/asserts/js/popper.min.js}" src="asserts/js/popper.min.js"></script>
<script type="text/javascript" th:href="@{/asserts/js/bootstrap.min.js}" src="asserts/js/bootstrap.min.js"></script>
<!-- Icons -->
<script type="text/javascript" th:href="@{/asserts/js/feather.min.js}" src="asserts/js/feather.min.js"></script>
<script>
feather.replace()
</script>
<!-- Graphs -->
<script type="text/javascript" th:href="@{/asserts/js/Chart.min.js}" src="asserts/js/Chart.min.js"></script>
<script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
datasets: [{
data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],
lineTension: 0,
backgroundColor: 'transparent',
borderColor: '#007bff',
borderWidth: 4,
pointBackgroundColor: '#007bff'
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: false
}
}]
},
legend: {
display: false,
}
}
});
</script>
</body>
</html>