首页 > 代码库 > 新随笔
新随笔
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 @SpringBootApplication10 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/test2 spring.datasource.username=root3 spring.datasource.password=root4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver5 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 @Mapper11 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 @RestController12 @RequestMapping({"/home"})13 public class UserController {14 @Autowired15 UserMapper userMapper;16 17 @RequestMapping(value = "http://www.mamicode.com/user")18 @ResponseBody19 public String user(){20 User user = userMapper.findUserByName("王伟");21 return user.getName()+"-----"+user.getAge();22 }
新随笔
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。