好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用Spring Boot+MyBatis框架做查询操作的示例代码

一.在你建立的工程下创建 module 选择spring initializr创建。

二.在type处选择: maven project(项目的构建工具)

三.创建依赖时勾上web,mybatis,mysql(这个看你个人需要吧,可以自主选择)

建立好的项目结构如下:

注意:application.properties和application.yml是同一个东西,均为项目的核心配置文件

内容如下:

?

1

2

3

4

5

6

7

8

9

#连接数据库

spring.datasource.url=jdbc:mysql: //localhost:3306/smbms

spring.datasource.username=root

spring.datasource.password= 1234

spring.datasource.driverclassname=com.mysql.jdbc.driver

#引入mybatis的配置文件

mybatis:

   mybatis.mapper-locations=classpath:mapper/*.xml

   mybatis.type-aliases- package =com.example.sprboot.pojo

相应的pom.xml文件

?

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

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

<?xml version= "1.0" encoding= "utf-8" ?>

<project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

      xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >

   <modelversion> 4.0 . 0 </modelversion>

 

   <groupid>com.example</groupid>

   <artifactid>springboot</artifactid>

   <version> 0.0 . 1 -snapshot</version>

   <packaging>jar</packaging>

 

   <name>springboot</name>

   <description>demo project for spring boot</description>

 

   <parent>

     <groupid>org.springframework.boot</groupid>

     <artifactid>spring-boot-starter-parent</artifactid>

     <version> 2.0 . 5 .release</version>

     <relativepath/> <!-- lookup parent from repository -->

   </parent>

   <properties>

     <project.build.sourceencoding>utf- 8 </project.build.sourceencoding>

     <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding>

     <java.version> 1.8 </java.version>

   </properties>

   <dependencies>

     <!--添加web依赖-->

     <dependency>

       <groupid>org.springframework.boot</groupid>

       <artifactid>spring-boot-starter-web</artifactid>

     </dependency>

     <!--引入spring boot包-->

     <dependency>

       <groupid>org.springframework.boot</groupid>

       <artifactid>spring-boot-starter-test</artifactid>

       <scope>test</scope>

     </dependency>

     <!-- spring-mybatis -->

     <dependency>

       <groupid>org.mybatis.spring.boot</groupid>

       <artifactid>mybatis-spring-boot-starter</artifactid>

       <version> 1.3 . 0 </version>

     </dependency>

     <!-- mysql -->

     <dependency>

       <groupid>mysql</groupid>

       <artifactid>mysql-connector-java</artifactid>

     </dependency>

     <!--使用json对象-->

     <dependency>

       <groupid>com.alibaba</groupid>

       <artifactid>fastjson</artifactid>

       <version> 1.2 . 49 </version>

     </dependency>

     <!--使用thymeleaf标签-->

     <dependency>

       <groupid>org.springframework.boot</groupid>

       <artifactid>spring-boot-starter-thymeleaf</artifactid>

     </dependency>

   </dependencies>

   <build>

     <plugins>

       <plugin>

         <groupid>org.springframework.boot</groupid>

         <artifactid>spring-boot-maven-plugin</artifactid>

       </plugin>

     </plugins>

   </build>

</project>

相应的接口usermapper如下:

?

1

2

3

4

@repository

public interface usermapper {

   list<user> getlist();

}

service如下:

?

1

2

3

public interface userservice {

   list<user> getlist();

}

impl如下:

?

1

2

3

4

5

6

7

8

9

@service

public class userserviceimpl implements userservice {

   @resource

   private usermapper usermapper;

   @override

   public list<user> getlist() {

     return usermapper.getlist();

   }

}

在resources中建一个文件夹mapper里面放mapper.xml文件,代码如下:

?

1

2

3

<select id= "getlist" resulttype= "user" >

   select * from smbms_user

</select>

在templates文件夹中建html文件(注意:spring boot中不能跳转到.jsp文件,所以只能用html)

核心代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<table>

   <th>工号</th>

   <th>用户名</th>

   <th>姓名</th>

   <th>性别</th>

   <th>生日</th>

   <th>电话</th>

   <th>地址</th>

   <th>创建时间</th>

   <tr th:each= "user : ${users}" >

     <td th:text= "${user.id}" ></td>

     <td th:text= "${user.usercode}" ></td>

     <td th:text= "${user.username}" ></td>

     <td th:text= "${user.gender}" ></td>

     <td th:text= "${user.birthday}" ></td>

     <td th:text= "${user.phone}" ></td>

     <td th:text= "${user.address}" ></td>

     <td th:text= "${user.createdby}" ></td>

   </tr>

</table>

此处有一个th标签,需要引入一个<html xmlns:th="http://HdhCmsTestthymeleaf.org">

并在pom.xml中引入对应的jar包(html中不能使用jstl表达式)

大家可以扩展一下thymeleaf的知识

控制器代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

@controller

public class usercontroller {

   @resource

   private userservice userservice;

@requestmapping ( "/" )

   public string getstuinforlist(httpservletrequest request, model model){

     list<user> list=userservice.getlist();

     model.addattribute( "users" ,list);

     system.out.println(list);

     return "/index.html" ;

   }

}

注:在调通的时候,可能会报很多的错,基本上都是注解的使用出错,希望各位能够细心点

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:http://HdhCmsTestcnblogs测试数据/merakfire/p/9805235.html

查看更多关于使用Spring Boot+MyBatis框架做查询操作的示例代码的详细内容...

  阅读:10次