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;
  }

}



猜你喜欢

转载自blog.csdn.net/imezreal/article/details/80665426