SpringBoot_數據訪問-整合MyBatis-註解版MyBatis

第一步:配置druid數據源
第二步 建立mapper類 /** * Created by chengbx on 2018/6/29. */ @Mapper public interface DepartmentMapper { @Select("select * from department where id =#{id}") public Department getDeptById(Integer id); @Delete("delete from department where id =#{id}") public int deleteById(Integer id); @Options(useGeneratedKeys = true,keyProperty = "id") @Insert("insert into department(department_name) values(#{departmentName}) ") public int insertDept(Department department); @Update("update department set department_name =#{departmentName} where id=#{id}") public int updateDept(Department department); }
第三步 編寫myBatis配置類 支持駝峯式命名 @Configuration public class MyBatisConfig { @Bean public ConfigurationCustomizer configurationCustomizer(){ return  new ConfigurationCustomizer(){ /** * Customize the given a {@link Configuration} object. * * @param configuration the configuration object to customize */ @Override public void customize(org.apache.ibatis.session.Configuration configuration) { configuration.setMapUnderscoreToCamelCase(true); } }; } }
第四步 建立實體類bean
public class Department {

private Integer id;
private String departmentName;

public Department() {
}

public Department(Integer id, String departmentName) {
this.id = id;
this.departmentName = departmentName;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getDepartmentName() {
return departmentName;
}

public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}

public class Employee {

private Integer id;
private String lastName;
private Integer gender;
private String email;
private Integer did;

public Employee() {
}

public Employee(Integer id, String lastName, Integer gender, String email, Integer did) {
this.id = id;
this.lastName = lastName;
this.gender = gender;
this.email = email;
this.did = did;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Integer getGender() {
return gender;
}

public void setGender(Integer gender) {
this.gender = gender;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public Integer getDid() {
return did;
}

public void setDid(Integer did) {
this.did = did;
}
}

 

建立Controller類調用 @RestController public class DepartmentController { @Autowired private DepartmentMapper departmentMapper; @GetMapping(value = "/dept/{id}") public Department getDepartment(@PathVariable("id") Integer id){ return departmentMapper.getDeptById(id); } @GetMapping(value = "/dept") public Department insertDept(Department department){ departmentMapper.insertDept(department); return department; } }
pom.xml 
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>com.baoxing</groupId>   <artifactId>springboot-mybatis-01</artifactId>   <version>0.0.1-SNAPSHOT</version>   <packaging>jar</packaging>   <name>springboot-mybatis-01</name>   <description>Demo project for Spring Boot</description>   <parent>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-parent</artifactId>      <version>1.5.15.BUILD-SNAPSHOT</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>   <dependencies>      <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-jdbc</artifactId>      </dependency>      <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-web</artifactId>      </dependency>      <dependency>         <groupId>org.mybatis.spring.boot</groupId>         <artifactId>mybatis-spring-boot-starter</artifactId>         <version>1.3.2</version>      </dependency>      <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->      <dependency>         <groupId>com.alibaba</groupId>         <artifactId>druid</artifactId>         <version>1.1.8</version>      </dependency>      <dependency>         <groupId>mysql</groupId>         <artifactId>mysql-connector-java</artifactId>         <scope>runtime</scope>      </dependency>      <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-test</artifactId>         <scope>test</scope>      </dependency>   </dependencies>   <build>      <plugins>         <plugin>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-maven-plugin</artifactId>         </plugin>      </plugins>   </build>   <repositories>      <repository>         <id>spring-snapshots</id>         <name>Spring Snapshots</name>         <url>https://repo.spring.io/snapshot</url>         <snapshots>            <enabled>true</enabled>         </snapshots>      </repository>      <repository>         <id>spring-milestones</id>         <name>Spring Milestones</name>         <url>https://repo.spring.io/milestone</url>         <snapshots>            <enabled>false</enabled>         </snapshots>      </repository>   </repositories>   <pluginRepositories>      <pluginRepository>         <id>spring-snapshots</id>         <name>Spring Snapshots</name>         <url>https://repo.spring.io/snapshot</url>         <snapshots>            <enabled>true</enabled>         </snapshots>      </pluginRepository>      <pluginRepository>         <id>spring-milestones</id>         <name>Spring Milestones</name>         <url>https://repo.spring.io/milestone</url>         <snapshots>            <enabled>false</enabled>         </snapshots>      </pluginRepository>   </pluginRepositories></project>
相關文章
相關標籤/搜索