-
新增父專案依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <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> 複製程式碼
-
引入mybatis和資料庫以及連線池的依賴
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> 複製程式碼
-
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/spring-boot-test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource 非註解版需要新增以下配置,mapper的xml放在resources下的mapper資料夾
mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.zml.demo.model mybatis.type-handlers-package=com.zml.demo.mapper
-
model 省略getter/setter
@Alias("User") public class User implements Serializable { private Long id; private String username; private String password; private String name; } 複製程式碼
-
controller
@Controller public class UserController { @Autowired private UserMapper userMapper; @RequestMapping("/users/{id}") @ResponseBody public User get(@PathVariable Long id){ User user = userMapper.get(id); return user; } @RequestMapping("/users") @ResponseBody public List<User> list(){ List<User> userList = userMapper.listAll(); return userList; } } 複製程式碼
-
mapper 註解版,不用註解和原來的一樣
@Component //為了消除Controller裡面的紅線警告 @Mapper public interface UserMapper { @Select("select * from user") @Results({ @Result(property = "username",column = "username"), @Result(property = "password",column = "password") } ) List<User> listAll(); @Select("select * from user where id = #{id}") User get(Long id); } 複製程式碼
spring boot 整合mybatis
相關文章
- Spring Boot 整合 MyBatisSpring BootMyBatis
- Spring boot 六 整合 MyBatisSpring BootMyBatis
- Spring boot學習(三) Spring boot整合mybatisSpring BootMyBatis
- Spring Boot 整合 Mybatis 和 MySQLSpring BootMyBatisMySql
- Spring Boot —— 整合 MyBatis-PlusSpring BootMyBatis
- 【Spring Boot】快速整合Mybatis-PlusSpring BootMyBatis
- Spring Boot系列(三):Spring Boot整合Mybatis原始碼解析Spring BootMyBatis原始碼
- Spring boot學習(五)Spring boot整合Mybatis Generator以及PageHelperSpring BootMyBatis
- 小代學Spring Boot之整合MyBatisSpring BootMyBatis
- Spring Boot(六)整合 MyBatis 操作 MySQL 8Spring BootMyBatisMySql
- Spring Boot整合MyBatis實現通用MapperSpring BootMyBatisAPP
- Spring Boot 整合 MyBatis和 SQL Server實踐Spring BootMyBatisSQLServer
- Spring boot入門(二):Spring boot整合MySql,Mybatis和PageHelper外掛Spring BootMySqlMyBatis
- MyBatis初級實戰之一:Spring Boot整合MyBatisSpring Boot
- 從零開始學Spring Boot系列-整合mybatisSpring BootMyBatis
- Spring Boot:Spring Boot配置MybatisSpring BootMyBatis
- Spring 整合 MyBatisSpringMyBatis
- mybatis整合springMyBatisSpring
- Spring整合MyBatisSpringMyBatis
- Spring Boot整合Mybatis完成級聯一對多CRUD操作Spring BootMyBatis
- spring-boot-route(八)整合mybatis運算元據庫SpringbootMyBatis
- 從零開始學Spring Boot系列-整合MyBatis-PlusSpring BootMyBatis
- Unit08: Spring與MyBatis整合 、 Spring整合MyBatis應用SpringMyBatis
- Spring整合Mybatis plusSpringMyBatis
- Mybatis與Spring整合MyBatisSpring
- Spring整合JUnit,MybatisSpringMyBatis
- Spring Boot 2.0(八):Spring Boot 整合 MemcachedSpring Boot
- Spring Boot(十八):使用 Spring Boot 整合 FastDFSSpring BootAST
- 【Spring Boot架構】整合Mybatis-Plus的例項詳解Spring Boot架構MyBatis
- Spring Boot 中使用 MyBatis 整合 Druid 多資料來源Spring BootMyBatisUI
- Spring Boot整合rabbitmqSpring BootMQ
- Spring Boot 整合 rabbitmqSpring BootMQ
- Spring Boot 整合 elasticsearchSpring BootElasticsearch
- Spring Boot 整合 dockerSpring BootDocker
- Spring Boot 整合 elkSpring Boot
- Spring Boot 整合 ApolloSpring Boot
- Spring Boot整合RedisSpring BootRedis
- Spring Boot 整合redisSpring BootRedis