1.创建springboot项目,注意这里的版本选择2.0.0,之前因为用的别的版本在配置Mybatis的时候老是出错(使用jpa也是一样)
添加这两个依赖:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
<?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>com.xidian.house</groupId> <artifactId>house</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hosue</name> <description>房产销售平台</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version><!-- 当要使用mybatis 或者jpa的时候一定要使用2.0.0.0的版本 --> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. 在application.yml配置数据库和Mybatis文件
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/house username: root password: root mybatis: config-location: classpath:/mybatis/mybatis-config.xml
3.配置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> <settings> <!--配置關閉緩存 --> <setting name="cacheEnabled" value="false"/> <setting name="mapUnderscoreToCamelCase" value="true"/><!-- 將表的下劃線形式映射成駝峰形式 --> <setting name="useGeneratedKeys" value="true"/> <setting name="defaultExecutorType" value="REUSE"/> <!-- 事務超時時間 --> <setting name="defaultStatementTimeout" value="600"/> </settings> <typeAliases><!-- 定義對象的別名 ,在mapping.xml文件中非常有用--> <typeAlias type="com.xidian.house.common.model.User" alias="user"/> </typeAliases> <mappers><!-- 定義sql文件的邏輯 --> <mapper resource="mapper/user.xml"/> </mappers> </configuration>
3.定义user类
public class User {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
4.新建接口UserMapper
@Mapper public interface UserMapper { public List<User> selectUser(); }
5.user.xml是和UserMapper对应的,一个接口方法对应一条sql语句
<mapper namespace="com.xidian.house.mapper.UserMapper"> <select id="selectUser" resultType="user"> select id from user </select> </mapper>
6.创建service类
@Service public class UserServie { @Autowired private UserMapper userMapper; public List<User> getUsers(){ return userMapper.selectUser(); } }
7.创建controll类
@RestController public class UserController { @Autowired private UserServie userService; @RequestMapping("/user") public List<User> getUsers(){ return userService.getUsers(); } }
8.在启动类中配置@MapperScan("com.xidian.house.mapper"),在启动的时候就能扫描指定包中的mapper文件,将其配置成Spring的bean
@SpringBootApplication @MapperScan("com.xidian.house.mapper") public class HosueApplication { public static void main(String[] args) { SpringApplication.run(HosueApplication.class, args); } }
浏览器中输入地址:http://127.0.0.1:8080/user
显示输出:[{"id":15},{"id":29},{"id":14},{"id":8},{"id":9},{"id":10},{"id":11},{"id":12},{"id":13},{"id":27},{"id":7}]