當前位置:
首頁 > 知識 > SpringBoot整合MyBatis,MySql之從前台頁面到資料庫的小Demo

SpringBoot整合MyBatis,MySql之從前台頁面到資料庫的小Demo

結構圖:


SpringBoot整合MyBatis,MySql之從前台頁面到資料庫的小Demo


pom.xml文件引入依賴

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

application.properties文件

# 資料庫訪問配置
# 主數據源,默認的
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 配置mapper.xml的位置
mybatis.mapper-locations=classpath:mapper/*.xml
# 配置埠
server.port=8080
1
2
3
4
5
6
7
8
9
10
11
12

或者使用application.yml文件也可以


index.html頁面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/user/login" method="post">
<input type="text" name="username" placeholder="請輸入用戶名" /> <br/>
<input type="password" name="password" placeholder="請輸入密碼" /><br/>
<input type="submit" value="登錄"><input type="reset" value="重置">
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

注意:

在SpringBoot中載入靜態資源和在普通的web應用中不太一樣。靜態資源(js、css、圖片等資源)默認目錄位置需置於classpath下,並且符合以下目錄規則:

  • /static
  • /public
  • /resources
  • /META-INF/resources

不滿足這些規則中的一種,是無法訪問到靜態資源的

類似於這種結構:

SpringBoot整合MyBatis,MySql之從前台頁面到資料庫的小Demo

默認規則的優先順序順序:

META-INF/resources > resources > static > public ,也就是說如果在優先順序高的包里和優先順序低的包里有同名的靜態資源,springboot在訪問到優先順序高的包中的靜態資源之後將不再去優先順序低包中尋找。


UserController

/**
* Create by zhaihongwei on 2018/3/13
*/
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/user/login", method = RequestMethod.POST)
public void userLogin(User user) {
User userExt = userService.userLogin(user);
if (userExt != null) {
System.out.println("登錄成功啦:)");
return;
}
System.out.println("登錄失敗了:(");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

UserService介面:

/**
* Create by zhaihongwei on 2018/3/13
*/
public interface UserService {
User userLogin(User user);
}
1
2
3
4
5
6
7

UserService實現類:

/**
* Create by zhaihongwei on 2018/3/13
*/
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User userLogin(User user) {
return userMapper.userLogin(user);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

UserMapper介面

/**
* Create by zhaihongwei on 2018/3/13
*/
@Mapper
public interface UserMapper {
//@Select("SELECT * FROM user WHERE username=#{username} and password=#{password}")
User userLogin(User user);
}
1
2
3
4
5
6
7
8
9

注意:如果使用註解的形式寫SQL語句,需要將application.properties文件中配置mapper.xml的配置注釋掉。

UserMapper.xml文件

<?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.zhw.study.springboot_demo.dao.UserMapper">
<select id="userLogin" resultType="com.zhw.study.springboot_demo.pojo.User" parameterType="com.zhw.study.springboot_demo.pojo.User">
SELECT * FROM user WHERE username=#{username} AND password=#{password};
</select>
</mapper>
1
2
3
4
5
6
7
8
9
10

**注意:**mapper.xml文件所在的位置,要和application.properties文件中配置的保持一致

# 配置mapper.xml的位置
mybatis.mapper-locations=classpath:mapper/*.xml
1
2


SpringBoot整合MyBatis,MySql之從前台頁面到資料庫的小Demo



啟動類

@SpringBootApplication
@MapperScan("com.zhw.study.springboot_demo.dao")// 掃描Mapper介面所在的包
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
1
2
3
4
5
6
7
8
9

啟動之後直接訪問:http://localhost:8080/,就可以直接找到index.html文件了

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

const 注意事項(初始化,重載,參數和返回值)
負載均衡學習之DNS域名解析負載均衡

TAG:程序員小新人學習 |