当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot整合Mybatis,SpringMVC简单例子

SpringBoot整合Mybatis,SpringMVC简单例子

2020年08月17日  | 移动技术网IT编程  | 我要评论
SpringBoot目录结构数据库代码PersonController@RestController@RequestMapping("/person")public class PersonController { @Autowired private PersonService personService; @GetMapping("/getPersonInfo") public String getPersonInfo( Long tel ) {

SpringBoot

目录结构

在这里插入图片描述

数据库

在这里插入图片描述

代码

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://www.lhsxpumps.com/_localhost:8080/person/getPersonInfo?tel=15611111111 

在这里插入图片描述

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网