一篇超详细的Spring|一篇超详细的Spring Boot对jdbc支持的文章
目录
- 项目结构
- pom.xml
- 启动类
- Dao层
- Service层
- Controller层
- 测试类测试
- 总结
项目结构
文章图片
pom.xml
pom.xml:
spring-boot-02com.keafmd 1.0-SNAPSHOT 4.0.0 spring-boot-08org.springframework.boot spring-boot-starter-weborg.apache.tomcat.embed tomcat-embed-jasperjstl jstl1.2 org.springframework.boot spring-boot-starter-jdbcmysql mysql-connector-java5.1.46 org.springframework.boot spring-boot-starter-testorg.springframework.boot spring-boot-devtools
application.yml
server:port: 80spring:datasource:url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=falsedriver-class-name: com.mysql.jdbc.Driverusername: rootpassword: 18044229
启动类
package com.keafmd; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Keafmd * * @ClassName: App08 * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-08 11:48 * @Blog: https://keafmd.blog.csdn.net/ */@SpringBootApplicationpublic class App08 {public static void main(String[] args) {SpringApplication.run(App08.class, args); }}
Dao层
UserDao:
package com.keafmd; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Keafmd * * @ClassName: App08 * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-08 11:48 * @Blog: https://keafmd.blog.csdn.net/ */@SpringBootApplicationpublic class App08 {public static void main(String[] args) {SpringApplication.run(App08.class, args); }}
Service层
IUserService :
package com.keafmd.service; import java.util.List; /** * Keafmd * * @ClassName: IUserService * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-08 11:59 * @Blog: https://keafmd.blog.csdn.net/ */public interface IUserService {List list(); }
UserServiceImpl:
package com.keafmd.service.impl; import com.keafmd.dao.UserDao; import com.keafmd.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Keafmd * * @ClassName: UserServiceImpl * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-08 12:00 * @Blog: https://keafmd.blog.csdn.net/ */@Servicepublic class UserServiceImpl implements IUserService {@AutowiredUserDao userDao; @Overridepublic List list() {return userDao.userList(); }}
Controller层
UserController:
package com.keafmd.controller; import com.keafmd.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * Keafmd * * @ClassName: UserController * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-08 18:04 * @Blog: https://keafmd.blog.csdn.net/ */@RestControllerpublic class UserController {@AutowiredIUserService userService; /*** http://127.0.0.1/userlist* @return*/@RequestMapping("userlist")List UserList(){return userService.list(); }}
测试类测试
UserDaoTest:
package com.keafmd.dao; import com.keafmd.App08; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest(classes = App08.class)class UserDaoTest {@AutowiredUserDao userDao; @Testpublic void test(){List userlist = userDao.userList(); for (Object o : userlist) {System.out.println(o); }}}
【一篇超详细的Spring|一篇超详细的Spring Boot对jdbc支持的文章】运行test方法的效果:
文章图片
运行启动类,测试效果
运行启动类,访问:http://127.0.0.1/userlist
文章图片
总结 本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
推荐阅读
- 由伐木工引发的陈述
- 随笔一篇
- 我为什么写不出一篇读书分享稿()
- 2018-06-29|2018-06-29 这是我的第一篇文章 markdown
- 《如何写出一篇好文章》之赠送礼物的训练
- 为了女儿,我尝试画画——DIY绘本之《一支铅笔的梦想》(绘画小白的第一篇作品)
- 自得其乐
- 适用于小白(VSCode搭建Vue项目,最详细的搭建步骤哦)
- 开工平安
- 用|用 Python 写网络编程(二)