springboot大家都知道了,搭建一个spring框架只需要秒秒钟。下面给大家介绍一下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; /**
* Created by wangwei on 2016/9/2.
*/
@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; /**
* Created by wangwei on 2016/9/2.
*/
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; /**
* Created by Administrator on 2016/9/2.
*/
@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; /**
* Created by wangwei on 2016/9/2.
*/
@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();
}
}
@RestController是对应的restful风格的控制器,@RequestMapping里面可以对应一个数组哦
打开浏览器,输入:http://localhost:1111/home/user
效果如下:
上面的对于简单的sql确实是非常的容易搞定,但是对于复杂的sql还是要到专门的地方去写啊!
接下来就为大家说说如何配置mapper.xml.
mybatis:
type-aliases-package: com.wangsu_bi.web.entity
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
以上是在application.yml中配置的mybatis属性:
mybatis-config.xml的结构如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-mybatis.org//DTD Config 3.0//EN" "http://mybatis.org.dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
这样就ok啦。就可以在mapper.xml中疯狂的写我们所需要的SQL啦!
本博客涉及的项目的地址:https://github.com/shenlanzhizunjustwangwei/springBoot
希望大家喜欢。