springboot+mybatis+spring+springmvc 项目整合

一、pom文件引入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.4</version>
   </dependency>
   <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <scope>runtime</scope>
   </dependency>
   <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.12</version>
       <scope>test</scope>
  </dependency>

二、application文件配置属性

.yml 文件

# mapper-locations:mybatis扫描的xml文件路径
#type-aliases-package:mybatis扫描的实体类路径 
mybatis:
    mapper-locations: classpath:mappers/*xml
    type-aliases-package: com.yunluo.appointment.*.model
server:
    port: 8080
spring:
    application:
        name: app
    datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        name: defaultDataSource
        password: 123489
        url: jdbc:mysql://localhost:3306/da?serverTimezone=UTC
        username: su

.properties 文件 

mybatis.mapper-locations=classpath:mappers/*.xml
mybatis.type-aliases-package=com/example/times/user/model
server.port=8080
spring.application.name=appointment
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.name=defaultDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/dda?serverTimezone=UTC
spring.datasource.username= sun
spring.datasource.password=1234

三、启动类添加注解@MapperScan

package com.yunluo.appointment;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.yunluo.appointment.*.mapper")
public class AppointmentApplication {

    public static void main(String[] args) {
        SpringApplication.run(AppointmentApplication.class, args);
    }

}

四、Mapper.xml 文件配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yunluo.appointment.user.mapper.UserMapper">
<resultMap id="map" type="com.yunluo.appointment.user.model.User">
        <id column="id" property="id" jdbcType="java.lang.Integer"/>
        <result column="name" property="name" jdbcType="java.lang.String"/>
        <result column="passward" property="passward"jdbcType="java.lang.String"/>
</resultMap>
    <sql id="Base_Column_List">
    id, name, passward
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="map">
        select
        <include refid="Base_Column_List"/>
        from t_user
        where id = #{id,jdbcType=INTEGER}
    </select>
</mapper>

五 、定义实体类

package com.yunluo.appointment.user.model;

import lombok.ToString;

@ToString
public class User {
    private Integer id;

    private String name;

    private String passward;

    public User(Integer id, String name, String passward) {
        this.id = id;
        this.name = name;
        this.passward = passward;
    }

    public User() {
        super();
    }

    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 String getPassward() {
        return passward;
    }

    public void setPassward(String passward) {
        this.passward = passward;
    }
}

六、定义Service接口以及其实现类 

package com.yunluo.appointment.user.service;

import com.yunluo.appointment.user.model.User;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public interface UserService {
   
       User selectByPrimaryKey(Integer id);
}
package com.yunluo.appointment.user.service.imp;

import com.yunluo.appointment.user.mapper.UserMapper;
import com.yunluo.appointment.user.model.User;
import com.yunluo.appointment.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

   @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

 
}

七、定义Junit测试类

package com.yunluo.appointment;

import com.yunluo.appointment.user.model.User;
import com.yunluo.appointment.user.service.UserService;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = AppointmentApplication.class)
class AppointmentApplicationTests {

    @Test
    void contextLoads() {
    }

    @Autowired
    private UserService userService;

    private User user;

    @Before
    public void setUp() throws Exception {
        user = new User();
    }

    @Test
    public void get() {
        user = userService.selectByPrimaryKey(1);
        System.out.println(user);
    }

}
 

八、定义Controller类

package com.yunluo.appointment.user.controller;


import com.yunluo.appointment.user.model.User;
import com.yunluo.appointment.user.service.UserService;
import com.yunluo.appointment.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    private User user;

    @RequestMapping("/get")
    public JsonData get(Integer id) {
            user = userService.selectByPrimaryKey(id);
            JsonData jsonData = new JsonData();
            jsonData.setResult(user);
            return jsonData;
    }


}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot是一个用于简化Spring应用程序开发的框架。它并没有官方实现Mybatis的启动器,但是Mybatis官方自己实现了一个启动器,可以在pom.xml文件中引入依赖来使用。\[1\]为了开发web工程,需要在pom.xml中引入spring-boot-starter-web依赖,这个依赖包含了Spring WebMVC和Tomcat等web开发的特性。\[2\]在使用SpringBoot快速入门时,需要进行以下步骤:添加依赖spring-boot-starter-web和spring-boot-starter-parent,创建控制器Controller和DispatcherServlet前端控制器,然后启动SpringBoot项目。\[3\]在控制器类上使用注解来标识该类是一个业务控制器,比如使用@SpringBootApplication注解来标识启动类,@Controller注解来标识控制器类。\[3\]至于整合SpringBootSpringMVCMybatis框架,可以通过配置文件和注解来实现。具体的整合方式可以根据项目需求和实际情况进行选择和配置。 #### 引用[.reference_title] - *1* *3* [springboot整合mybatis等框架](https://blog.csdn.net/qq_42652006/article/details/126833620)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [springboot整合mybatisspringmvc](https://blog.csdn.net/sunhongbing1024/article/details/83186783)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值