Spring Boot Mybatis 配置

 Spring Boot Mybatis 配置

resource/mybatis-config.xml

配置數據庫連接方式(jdbc驅動),註冊mapper,

<?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="localCacheScope" value="STATEMENT"/>
<setting name="useGeneratedKeys" value="true"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://80.76.180.95" />
<property name="username" value="root" />
<property name="password" value="Password_12#$" />
</dataSource>
</environment>
</environments>


<mappers>
<mapper resource="mapper/TestMapper.xml"/>
<mapper class="com.deep.infra.persistence.sql.mapper.UserMapper"/>
<mapper class="com.deep.infra.persistence.sql.mapper.AgentMapper"/>
<mapper class="com.deep.infra.persistence.sql.mapper.RoleMapper" />


<mapper class="com.deep.infra.persistence.sql.mapper.NoticeMapper"/>
<mapper resource="mapper/BreedingPlanMapper.xml"/>
<mapper resource="mapper/DiagnosisPlanMapper.xml"/>
<mapper resource="mapper/NutritionPlanMapper.xml"/>
<!--<mapper resource="mapper/NoticePlanMapper.xml"/>-->


<mapper resource="mapper/PicMapper.xml"/>
<mapper resource="mapper/MessageMapper.xml"/>
<mapper resource="mapper/GenealogicalFilesMapperxml.xml" />
<mapper resource="mapper/DisinfectFilesMapper.xml" />
<mapper resource="mapper/ImmunePlanMapper.xml"/>
<mapper resource="mapper/RepellentPlanMapper.xml"/>
<mapper resource="mapper/GenealogicalFilesTransMapper.xml"/>
<mapper resource="mapper/DisinfectionArchivesMapper.xml" />
<mapper resource="mapper/OperationFileMapper.xml" />
<mapper resource="mapper/VideoPublish.xml"/>
<mapper resource="mapper/TypeBriefMapper.xml"/>
<mapper resource="mapper/EnvironmentTraceMapper.xml"/>


</mappers>
</configuration>

  
<!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="localCacheScope" value="STATEMENT"/>
<setting name="useGeneratedKeys" value="true"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://80.76.180.95" />
<property name="username" value="root" />
<property name="password" value="Password_12#$" />
</dataSource>
</environment>
</environments>


<mappers>
<mapper resource="mapper/TestMapper.xml"/>
<mapper class="com.deep.infra.persistence.sql.mapper.UserMapper"/>
<mapper class="com.deep.infra.persistence.sql.mapper.AgentMapper"/>
<mapper class="com.deep.infra.persistence.sql.mapper.RoleMapper" />


<mapper class="com.deep.infra.persistence.sql.mapper.NoticeMapper"/>
<mapper resource="mapper/BreedingPlanMapper.xml"/>
<mapper resource="mapper/DiagnosisPlanMapper.xml"/>
<mapper resource="mapper/NutritionPlanMapper.xml"/>
<!--<mapper resource="mapper/NoticePlanMapper.xml"/>-->


<mapper resource="mapper/PicMapper.xml"/>
<mapper resource="mapper/MessageMapper.xml"/>
<mapper resource="mapper/GenealogicalFilesMapperxml.xml" />
<mapper resource="mapper/DisinfectFilesMapper.xml" />
<mapper resource="mapper/ImmunePlanMapper.xml"/>
<mapper resource="mapper/RepellentPlanMapper.xml"/>
<mapper resource="mapper/GenealogicalFilesTransMapper.xml"/>
<mapper resource="mapper/DisinfectionArchivesMapper.xml" />
<mapper resource="mapper/OperationFileMapper.xml" />
<mapper resource="mapper/VideoPublish.xml"/>
<mapper resource="mapper/TypeBriefMapper.xml"/>
<mapper resource="mapper/EnvironmentTraceMapper.xml"/>


</mappers>
</configuration>

src/infra.persistence.sql/mapper/TestMapper.java
實現對數據庫操作的CRUD接口

  


src/domain/service/TestService.java

實例化CRUD接口
package com.deep.domain.service;
import com.deep.domain.model.TestModel;
import com.deep.infra.persistence.sql.mapper.TestMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * Created by zhongrui on 2018/2/1.
 */
@Service
public class TestService {
  @Resource
  private TestMapper testMapper;

  public TestModel getTestModel(String index) {
    TestModel model = this.testMapper.getTestModelById(index);
    return model;
  }

}


src/domain/model/TestModel.java

用於接收前端信息後對數據庫進行操作

package com.deep.domain.model;
public class TestModel {
  private int id;
  private String message;

  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getMessage() {
    return message;
  }
  public void setMessage(String message) {
    this.message = message;
  }

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章