码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot专题1----springboot与mybatis的完美融合

时间:2016-09-02 13:15:45      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

       springboot大家都知道了,搭建一个spring框架只需要秒秒钟。下面给大家介绍一下springboot与mybatis的完美融合:

       首先:创建一个名为springboot-mybatis的maven项目,记住:一定要maven哦,不懂maven的可以自己恶补一下maven知识,这里就不介绍maven了。

   下面给出pom.xml的完整配置:

       

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>springboot-mybatis</groupId>
 8     <artifactId>springboot-mybatis</artifactId>
 9     <version>1.0.0</version>
10     <packaging>war</packaging>
11 
12     <name>springBoot-mybatis</name>
13     <description>Spring Boot project</description>
14 
15     <parent>
16         <groupId>org.springframework.boot</groupId>
17         <artifactId>spring-boot-starter-parent</artifactId>
18         <version>1.3.2.RELEASE</version>
19         <relativePath/>
20     </parent>
21 
22     <properties>
23         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26 
27     <dependencies>
28 
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-test</artifactId>
37             <scope>test</scope>
38         </dependency>
39 
40         <dependency>
41             <groupId>org.mybatis.spring.boot</groupId>
42             <artifactId>mybatis-spring-boot-starter</artifactId>
43             <version>1.1.1</version>
44         </dependency>
45 
46         <dependency>
47             <groupId>org.springframework.boot</groupId>
48             <artifactId>spring-boot-starter-web</artifactId>
49         </dependency>
50 
51         <dependency>
52             <groupId>mysql</groupId>
53             <artifactId>mysql-connector-java</artifactId>
54             <version>5.1.21</version>
55         </dependency>
56 
57     </dependencies>
58 
59     <build>
60         <plugins>
61             <plugin>
62                 <groupId>org.springframework.boot</groupId>
63                 <artifactId>spring-boot-maven-plugin</artifactId>
64             </plugin>
65             <plugin>
66                 <groupId>org.apache.maven.plugins</groupId>
67                 <artifactId>maven-surefire-plugin</artifactId>
68                 <configuration>
69                     <skip>true</skip>
70                 </configuration>
71             </plugin>
72         </plugins>
73     </build>
74 
75 </project>

之后创建一个启动类:

 1 package org.shenlan;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 /**
 7  * Created by wangwei on 2016/9/2.
 8  */
 9 @SpringBootApplication
10 public class Application {
11     public static void main(String[] args){
12         SpringApplication.run(Application.class,args);
13     }
14 }

这样一个完整的springboot项目就完成了,是不是很简单。

接下来就可以整理与mybatis的东东了。

首先,创建配置文件:application.properties

1 spring.datasource.url=jdbc:mysql://localhost:3306/test
2 spring.datasource.username=root
3 spring.datasource.password=root
4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
5 
6 server.port=1111

这里server.port=1111是定义了改项目的端口,默认的是8080.

然后,定义一个java的实体类:

 1 package org.shenlan.web;
 2 
 3 /**
 4  * Created by wangwei on 2016/9/2.
 5  */
 6 public class User {
 7     private Integer id;
 8     private String name;
 9     private Integer age;
10 
11     public Integer getId() {
12         return id;
13     }
14 
15     public void setId(Integer id) {
16         this.id = id;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public Integer getAge() {
28         return age;
29     }
30 
31     public void setAge(Integer age) {
32         this.age = age;
33     }
34 }

这里实体类的字段要和数据库的字段对应起来,不然就要取别名了。

之后,定义一个dao的接口:

 1 package org.shenlan.web;
 2 
 3 import org.apache.ibatis.annotations.Mapper;
 4 import org.apache.ibatis.annotations.Param;
 5 import org.apache.ibatis.annotations.Select;
 6 
 7 /**
 8  * Created by Administrator on 2016/9/2.
 9  */
10 @Mapper
11 public interface UserMapper {
12 
13     @Select("select * from user where name = #{name}")
14     User findUserByName(@Param("name")String name);
15 }

@Mapper就是我们要与mybatis融合关键的一步,只要一个注解就搞定了。

哈哈哈,最后我们就来写一个测试类吧:

 1 package org.shenlan.web;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.ResponseBody;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 /**
 9  * Created by wangwei on 2016/9/2.
10  */
11 @RestController
12 @RequestMapping({"/home"})
13 public class UserController {
14     @Autowired
15     UserMapper userMapper;
16 
17     @RequestMapping(value = "/user")
18     @ResponseBody
19     public String user(){
20         User user = userMapper.findUserByName("王伟");
21         return user.getName()+"-----"+user.getAge();
22     }
23 }

     @RestController是对应的restful风格的控制器,@RequestMapping里面可以对应一个数组哦

     打开浏览器,输入:http://localhost:1111/home/user

     效果如下:

技术分享

     本博客涉及的项目的地址:https://github.com/shenlanzhizunjustwangwei/springBoot

     希望大家喜欢。

     如果觉得对你有帮助,可以打赏哦:

技术分享

SpringBoot专题1----springboot与mybatis的完美融合

标签:

原文地址:http://www.cnblogs.com/shenlanzhizun/p/5832976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!