- pom文件中添加倉庫地址
<repositories>
<repository>
<id>september</id>
<name>Spring Snapshots</name>
<url>http://120.92.166.27:9081/nexus/content/groups/public/</url>
</repository>
</repositories>
- 添加最新版本的maven依賴
<dependency>
<groupId>org.september</groupId>
<artifactId>alliance-smartdao</artifactId>
<version>2.3.1-SNAPSHOT</version>
</dependency>
- 配置數(shù)據(jù)源
#方言設(shè)置
spring.alliance.dao.dialect=mysql
#因為支持多數(shù)據(jù)源,所以對數(shù)據(jù)源做了個分組的功能,每個數(shù)據(jù)源配置自己所屬的分組,每個分組下有多個數(shù)據(jù)源,通常是一個寫,多個讀。
#如果有多個數(shù)據(jù)源需要從0,1,2,3依次增加
spring.alliance.dao.datasource[0].group = logdog
#可以是read, write, write/read 三種之一
spring.alliance.dao.datasource[0].type = write/read
spring.alliance.dao.datasource[0].driverClass=com.mysql.cj.jdbc.Driver
spring.alliance.dao.datasource[0].jdbcUrl = jdbc:mysql://localhost:3306/logdog?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
spring.alliance.dao.datasource[0].username = root
spring.alliance.dao.datasource[0].password = ********
- 在你的service類中注入CommonDao
@Autowired
private CommonDao commonDao;
- 使用CommonDao保存數(shù)據(jù)
// User是一個實體類
User user = new User();
user.setName("xzye");
commonDao.save(user);
mapper文件的默認(rèn)保存路徑是 classpath:/mybatis/mapper/ 下,我們建議以約定的方式,大家把mapper文件都放在這個目錄下。當(dāng)然也可以通過實現(xiàn)MyBatisConfigManager接口的方式來修改。
User是一個實體類,實體類如何定義以及支持的特性,請閱讀 實體類的使用 一文。
CommonDao中更多api的使用方法,請閱讀 CommonDao Api詳解 一文