Spring Boot之springboot+mysql+mybatis搭建
建立資料庫及資料表:
CREATE DATABASE /*!32312 IF NOT EXISTS*/`springbootdb` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `springbootdb`;
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`username` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2,建立User.java
package com.spb.SpringBootDemo.vo;
public class User {
private String id;
private String username;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
3, 在pom.xml中加入:
<properties>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
</properties>
<!-- Spring Boot Mybatis 依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!-- MySQL 連線驅動依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
4, 在resource下加入application.properties檔案,mysql和mybatis可以根據自己的地址配置。
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml
1
2
3
4
5
6
7
8
9
5,新增UserDao,並在resources下面建立mapper目錄,在下面建立UserMapper.xml
package com.spb.SpringBootDemo.dao;
import java.util.List;
import com.spb.SpringBootDemo.vo.User;
/**
*
* @author Administrator
*
*/
public interface UserDao {
/**
* 新增使用者
* @param user
*/
void createUser(User user);
/**
* 查詢使用者列表
* @return
*/
List<User> findAllUser();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.spb.SpringBootDemo.dao.UserDao">
<resultMap id="BaseResultMap" type="com.spb.SpringBootDemo.vo.User">
<result column="id" property="id" />
<result column="username" property="username" />
</resultMap>
<parameterMap id="User" type="com.spb.SpringBootDemo.vo.User"/>
<sql id="Base_Column_List">
id, username
</sql>
<select id="findAllUser" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from user
</select>
<insert id="createUser" parameterMap="User" useGeneratedKeys="true" keyProperty="id">
insert into
user(id,username)
values
(#{id},#{username})
</insert>
</mapper>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
6,最後增加service和實現controller,這裡service省略。
package com.spb.SpringBootDemo.rest;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spb.SpringBootDemo.service.IUserService;
import com.spb.SpringBootDemo.vo.User;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService service;
@RequestMapping("/userList")
public List<User> getUserList(){
return service.findAllUser();
}
@RequestMapping("/add")
public String addUser(@RequestBody User user){
if(user!=null){
service.createUser(user);
return "success";
}else{
return "error";
}
}
}
app.java中加入註解@MapperScan("com.spb.SpringBootDemo.dao")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
測試:
執行專案
用postman測試
原始碼:http://download.csdn.net/download/u012343297/10163177
---------------------
作者:嘿lemon
來源:CSDN
原文:https://blog.csdn.net/u012343297/article/details/78833744
版權宣告:本文為博主原創文章,轉載請附上博文連結!