欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot整合Mybatis,SpringMVC简单例子

程序员文章站 2022-05-28 21:36:34
SpringBoot目录结构数据库代码PersonController@RestController@RequestMapping("/person")public class PersonController { @Autowired private PersonService personService; @GetMapping("/getPersonInfo") public String getPersonInfo( Long tel ) {...

SpringBoot

目录结构

SpringBoot整合Mybatis,SpringMVC简单例子

数据库

SpringBoot整合Mybatis,SpringMVC简单例子

代码

PersonController

@RestController @RequestMapping("/person") public class PersonController { @Autowired private PersonService personService; @GetMapping("/getPersonInfo") public String getPersonInfo( Long tel ) { Person person = personService.getPersonInfo(tel); return person.toString(); } } 

Person

public class Person { private Integer id; private String name; private Long tel; private Integer age; public Integer getId() { return id; } public void setId( Integer id ) { this.id = id; } public String getName() { return name; } public void setName( String name ) { this.name = name; } public Long getTel() { return tel; } public void setTel( Long tel ) { this.tel = tel; } public Integer getAge() { return age; } public void setAge( Integer age ) { this.age = age; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", tel=" + tel + ", age=" + age + '}'; } } 

PersonMapper

@Component public interface PersonMapper { @Select("select * from person where tel = #{tel}") Person getPersonInfo( Long tel ); } 

PersonServiceImpl

@Service public class PersonServiceImpl implements PersonService { @Autowired private PersonMapper personMapper; public Person getPersonInfo( Long tel ) { Person person = personMapper.getPersonInfo(tel); return person; } } 

PersonService

public interface PersonService { Person getPersonInfo( Long tel ); } 

AppRun

@MapperScan("com.itcast.mapper") @SpringBootApplication public class AppRun { public static void main( String[] args ) { SpringApplication.run(AppRun.class, args); } } 

application.yml

server: port: 8080 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/person?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true username: root password: "123456" 

pom.xml

 <dependencies> <!--springboot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.2.2.RELEASE</version> </dependency> <!--数据源 默认为HikariCP 数据源--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>2.2.2.RELEASE</version> </dependency> <!--springboot整合mybatis依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!--mysql驱动包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.12</version> </dependency> </dependencies> 

访问路径

http://localhost:8080/person/getPersonInfo?tel=15611111111 

SpringBoot整合Mybatis,SpringMVC简单例子

本文地址:https://blog.csdn.net/qq_43532386/article/details/108032491

相关标签: spring boot Mybatis