07_Spring Boot 整合 Mybatis 的完整 Web 案例

1、pom.xml配置

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.linjb

springboot-mybatis

war

0.0.1-SNAPSHOT

springboot-mybatis Maven Webapp

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

1.5.1.RELEASE

1.2.0

5.1.39

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.mybatis.spring.boot

mybatis-spring-boot-starter

${mybatis-spring-boot}

mysql

mysql-connector-java

${mysql-connector}

junit

junit

4.12

springboot-mybatis

org.springframework.boot

spring-boot-maven-plugin

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

UTF-8

 

2、application.properties 配置

## 数据源配置

spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

## Mybatis 配置

mybatis.typeAliasesPackage=com.linjb.model

mybatis.mapperLocations=classpath:mapper/*.xml

 

mybatis.typeAliasesPackage 配置为 org.spring.springboot.domain,指向实体类包路径。mybatis.mapperLocations 配置为 classpath 路径下 mapper 包下,* 代表会扫描所有 xml 文件。

mybatis 其他配置相关详解如下:

mybatis.config = mybatis 配置文件名称 mybatis.mapperLocations = mapper xml 文件地址 mybatis.typeAliasesPackage = 实体类包路径 mybatis.typeHandlersPackage = type handlers 处理器包路径 mybatis.check-config-location = 检查 mybatis 配置是否存在,一般命名为 mybatis-config.xml mybatis.executorType = 执行模式。默认是 SIMPLE

 

3、Application 启动类

package com.linjb;

 

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

 

@SpringBootApplication

//mapper 接口类扫描包配置

@MapperScan("com.linjb.dao")

public class Application {

 

public static void main(String[] args) {

// 程序启动入口

// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件

SpringApplication.run(Application.class,args);

}

}

mapper 接口类扫描包配置注解 MapperScan :用这个注解可以注册 Mybatis mapper 接口类。

 

4、Model 层

package com.linjb.model;

 

public class College {

 

private Integer collegeId;

private String collegeName;

 

public Integer getCollegeId() {

return collegeId;

}

 

public void setCollegeId(Integer collegeId) {

this.collegeId = collegeId;

}

 

public String getCollegeName() {

return collegeName;

}

 

public void setCollegeName(String collegeName) {

this.collegeName = collegeName;

}

}

 

5、Dao 层

package com.linjb.dao;

 

import com.linjb.model.College;

 

public interface CollegeDao {

 

public College findByName(String collegeName);

}

 

6、Mapper.xml 文件

CollegeMapper.xml

college_id,college_name

 

 

7、Controller 层

package com.linjb.controller;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

import com.linjb.dao.CollegeDao;

import com.linjb.model.College;

 

@RestController

@RequestMapping(value="/college")

public class CollegeController {

 

@Autowired

private CollegeDao collegeDao;

@RequestMapping(value = "/findCollegeByName", method = RequestMethod.GET)

public College findCollegeByName(@RequestParam(value = "collegeName", required = true) String collegeName) {

return collegeDao.findByName(collegeName);

}

}

 

运行结果:

07_Spring Boot 整合 Mybatis 的完整 Web 案例_第1张图片

你可能感兴趣的:(搬砖之,SpringBoot,专栏)