栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot+mybatis+spring+springmvc 项目整合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot+mybatis+spring+springmvc 项目整合

一、pom文件引入依赖

    
        org.springframework.boot
        spring-boot-starter-data-jdbc
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.1.4
   
   
       mysql
       mysql-connector-java
       runtime
   
   
       junit
       junit
       4.12
       test
  

二、application文件配置属性

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

 

三、启动类添加注解@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 文件配置





        
        
        

    
    id, name, passward
  
  
        select
        
        from t_user
        where id = #{id,jdbcType=INTEGER}
    

五 、定义实体类

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;
    }


}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/710009.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号