当前位置:   article > 正文

idea创建springboot简单查询数据库查询案例_idea springboot+mybatis+sqlserver简单查询

idea springboot+mybatis+sqlserver简单查询

完整代码下载地址:https://download.csdn.net/download/qq_37231511/12028858

 

1.创建数据库mybatis_01,并建表user,插入一条数据,如图

2.创建springboot项目

点击next, 设置项目名称

点击next,选择需要用的框架,spring web、 mybatis、mysqlDriver

点击next,点击finish即创建成功,如图

3.整合MBG插件,自动生成实体类与mapper文件,MBG插件配置可见博文:https://blog.csdn.net/qq_37231511/article/details/103510314

4.配置application.properties

  1. #mysql
  2. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  3. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_01?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
  4. spring.datasource.username=root
  5. spring.datasource.password=root
  6. #dao
  7. #与文件目录一致
  8. mybatis.type-aliases-package==com.xue.demo1.repository.dao
  9. mybatis.mapper-locations=classpath*:com/xue/demo1/repository/mapper/*.xml

5.在pom.xml中build中配置资源,因为mapper.xml文件没有放置到src-main-resources下面,是不会被maven build plugin给默认扫描到的

  1. <resources>
  2. <resource>
  3. <directory>src/main/java</directory>
  4. <includes>
  5. <include>**/*.xml</include>
  6. <include>**/*.properties</include>
  7. </includes>
  8. <filtering>true</filtering>
  9. </resource>
  10. </resources>

6. 创建controller包,并创建类UserController

  1. package com.xue.demo1.controller;
  2. import com.xue.demo1.entity.User;
  3. import com.xue.demo1.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class UserController {
  9. @Autowired
  10. private UserService userService;
  11. @RequestMapping("/user")
  12. public User getUser(){
  13. User user = userService.getUser(1);
  14. if (null != user){
  15. return user;
  16. }else {
  17. return null;
  18. }
  19. }
  20. }

7.创建service层创建接口UserService与接口实现类UserServiceImpl

UserService

  1. package com.xue.demo1.service;
  2. import com.xue.demo1.entity.User;
  3. public interface UserService {
  4. public User getUser(Integer id);
  5. }

UserServiceImpl

  1. package com.xue.demo1.service.impl;
  2. import com.xue.demo1.entity.User;
  3. import com.xue.demo1.repository.dao.UserMapper;
  4. import com.xue.demo1.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class UserServiceImpl implements UserService {
  9. @Autowired
  10. private UserMapper userMapper;
  11. @Override
  12. public User getUser(Integer id) {
  13. User user = userMapper.selectByPrimaryKey(id);
  14. if(null != user){
  15. return user;
  16. }else {
  17. return null;
  18. }
  19. }
  20. }

 8.至此代码编写完成,项目结构如图

9.运行项目,在浏览器输入localhost:8080/user,如图

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/714027
推荐阅读
相关标签
  

闽ICP备14008679号