@[toc]java
最大的優勢是SQL語句靈活,適合調優情景,業務複雜情景mysql
最大的劣勢是不一樣數據庫之間的遷移web
pom.xml中加入spring
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
application.properties中加入sql
#dateSource spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.username=root spring.datasource.password=root # mybatis # 下劃線轉駝峯開啓 mybatis.configuration.map-underscore-to-camel-case=true # mapper掃描位置 mybatis.mapper-locations=classpath:mapper/*.xml
須要說明的是,我引入的mysql驅動爲 com.mysql.cj.jdbc.Driver,而不是 com.mysql.jdbc.Driver。如下是一些參數的說明;數據庫
本地的 mysql 版本爲 5.7.20。服務器
建表語句mybatis
CREATE TABLE USER_INFO ( user_id DECIMAL(10) PRIMARY KEY NOT NULL, user_name VARCHAR(20) DEFAULT "" NOT NULL ); CREATE UNIQUE INDEX USER_INFO_user_id_uindex ON USER_INFO (user_id); ALTER TABLE USER_INFO COMMENT = '用戶基本信息表';
插入數據架構
INSERT INTO test.user_info (user_id, user_name) VALUES (1, '特朗普'); INSERT INTO test.user_info (user_id, user_name) VALUES (2, '唐納德');
如今的項目路徑爲app
在SpringBoot的入口類中加 @MapperScan 註解,以掃描 DAO 類,不用在每一個dao接口加個@Mapper 註解。
package com.example; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.dao") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
注:若是沒有分拆項目成微服務架構,或分佈式架構,application主類中的@SpringBootApplication自動會掃描本包中的@Controller,@Service,@Resource等,是不須要浪費另外一行@ComponentScan註解,配置路徑的。
UserController.java
package com.example.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.example.service.IUserService; @Controller @RequestMapping("user") public class UserController { @Autowired private IUserService userService; @RequestMapping("/queryUser") @ResponseBody void queryUser() { this.userService.queryUser(); } }
IUserService.java
package com.example.service; public interface IUserService { void queryUser(); }
UserServiceIml.java
package com.example.service; import com.example.dao.UserDao; import com.example.entity.UserEntity; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service("userService") public class UserServiceIml implements IUserService { @Resource private UserDao userDao; @Override public void queryUser() { List<UserEntity> userList = this.userDao.queryUser(); System.out.println("================"); System.out.println(userList); System.out.println("================"); } }
注:若是是SSM架構過來的,這個@Resource註解可能比較陌生,由於我估計會和我以前同樣用@Autowired,可是我是沒有在dao層用 @Repository 註解的,以前用了在主類用了@MapperScan自動去掃描全部的dao,因此不能再用以前的@Autowired。
UserDao.java
package com.example.dao; import com.example.entity.UserEntity; import java.util.List; public interface UserDao { List<UserEntity> queryUser(); }
UserEntity.java
package com.example.entity; public class UserEntity { private long userId; private String userName; public long getUserId() { return userId; } public void setUserId(long userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Override public String toString() { return "UserEntity{" + "userId=" + userId + ", userName='" + userName + '\'' + '}'; } }
若是你運行不成功,請對比引入的包的版本,和數據庫配置是不是根據你本地的,順便看看註解是否和文中同樣。
效果圖以下;
注:若是是跟着本教程來的,由於一開始就引入了spring security,而新增的路徑不在剛纔的權限中,因此我給zs用戶改爲了皆可訪問。若是沒有跟着以前配置spring security,無需理會以下內容
//http.authorizeRequests().antMatchers("/user/addUser").hasRole("AAA") http.authorizeRequests().antMatchers("/**").hasRole("AAA")