SpringBoot整合Mybatis,SpringMVC简单例子
程序员文章站
2022-12-20 13:16:02
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://localhost:8080/person/getPersonInfo?tel=15611111111
本文地址:https://blog.csdn.net/qq_43532386/article/details/108032491
推荐阅读
-
SpringBoot系列-整合Mybatis(XML配置方式)
-
SpringBoot系列-整合Mybatis(注解方式)
-
SpringBoot+Dubbo+Zookeeper整合搭建简单的分布式应用
-
springboot+mybatis日志显示SQL的最简单方法
-
SpringBoot无废话入门04:MyBatis整合
-
JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
-
SSM(Spring+SpringMVC+Mybatis)框架整合
-
springboot+springmvc+mybatis项目整合
-
SpringBoot 整合jdbc和mybatis
-
SpringBoot整合mybatis访问时报错Invalid bound statement (not found)