分开来看,不管是springboot还是mybatis其实都是其对应领域比较主流的开发框架或是数据库关系映射框架,那么你了解为什么要在springboot中集成mybatis吗?该怎么去实现呢?
其实为什么要在springboot中集成mybatis很简单,你试想系统开发如果不整合不整合数据访问层,没有数据的交互,那还怎么玩?下面呢我们还是一起来看看其实现步骤吧。
首先:创建一个名为springboot-mybatis的maven项目,记住:一定要maven哦,不懂maven的可以自己恶补一下maven知识,这里就不介绍maven了。
下面给出pom.xml的完整配置:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.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>springboot-mybatis</groupId> <artifactId>springboot-mybatis</artifactId> <version>1.0.0</version> <packaging>war</packaging> <name>springBoot-mybatis</name> <description>Spring Boot project</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.2.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> </project>
之后创建一个启动类:
package org.shenlan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args); } }
这样一个完整的springboot项目就完成了,是不是很简单。
接下来就可以整理与mybatis的东东了。
首先,创建配置文件:application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver server.port=1111
这里server.port=1111是定义了改项目的端口,默认的是8080.
然后,定义一个java的实体类:
package org.shenlan.web; public class User { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
这里实体类的字段要和数据库的字段对应起来,不然就要取别名了。
之后,定义一个dao的接口:
package org.shenlan.web; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; @Mapper public interface UserMapper { @Select("select * from user where name = #{name}") User findUserByName(@Param("name") String name); }
@Mapper就是我们要与mybatis融合关键的一步,只要一个注解就搞定了。
最后我们就来写一个测试类吧:
package org.shenlan.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping( { "/home" }) public class UserController { @Autowired UserMapper userMapper; @RequestMapping(value = "/user") @ResponseBody public String user() { User user = userMapper.findUserByName("王伟"); return user.getName() + "-----" + user.getAge(); } }
好了以上就是有关springboot集成mybatis的所有内容了,还想了解更多java架构师的相关信息吗?关注本站消息即可获取哦。