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

Springboot 与 mybatis集成

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

Springboot 与 mybatis集成

数据库创建user表,我使用是mysql

 

1,创建spring boot 项目

1.2  artifact写入项目名,将package后缀去除

 1.3 可直接在此处配置依赖

2.1 pom.xml 文件可以不用管,只需要改动mysql驱动版本,不改也可以



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.entor
    springboot_mybatis
    0.0.1-SNAPSHOT
    springboot_mybatis
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-data-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            mysql
            mysql-connector-java
            5.1.47 
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


 2.2 全部的文件创建如图

 

 2.3将application.properties文件改为application.xml文件

application.xml文件配置入下

#使用yml配置文件
server:
  port: 8090 #更改端口号

#数据库库连接数据
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/自己的数据库?useUnicode=true&characterEncoding=utf-8&useSSL=false
    password: root
    username: root

#mybatis的相关配置
mybatis:
  type-aliases-package: com.entor.entity  #配置别名包
  configuration:
    map-underscore-to-camel-case: true  #配置驼峰命名
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #配置数据库日志文件

2.4写入实体类

package com.entor.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;
import java.util.Date;

@Data //代替set,get等方法
@AllArgsConstructor //有参构造
@NoArgsConstructor  //无参构造
public class User {

    private Integer id; //使用Inter当没有值时候为空
    private String name;
    private String username;
    private String password;
    private Integer sex;    //1男0女
    private Integer age;
    private Date birthday;
    private Timestamp createTime;
}

 2.5写mapper类

package com.entor.mapper;

import com.entor.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

public interface UserMapper {

    // @Insert,mybatis的 @Inser注解可以用来在Mapper类注解保存方法的sql语句
    @Insert("insert into user(name,username,password,sex,age) values(#{name},#{username},#{password},#{sex},#{age})")
    public void add(User user);

    @Select("select * from user where id = #{id}")
    public User queryById(int id);

}

2.6写service层

package com.entor.service;

import com.entor.entity.User;

public interface UserService {

    public void add(User user);

    public User queryById(int id);

}

2.7写实现层

package com.entor.service.impl;

import com.entor.entity.User;
import com.entor.mapper.UserMapper;
import com.entor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//作用在类上,该注解会将当前类自动注入到spring容器中,不需要再在applicationContext.xml文件定义bean了。
@Service
public class UserServiceImpl implements UserService {

    //@Autowired 可以更准确地控制应该在何处以及如何进行自动装配
    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public void add(User user) {
        userMapper.add(user);
    }

    @Override
    public User queryById(int id) {
        return userMapper.queryById(id);
    }
}

2.8最后写controller层

package com.entor.controller;

import com.entor.entity.User;
import com.entor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController //访问返回的是数据
public class UserController {

    @Autowired
    private UserService userService;

    


    @PostMapping("/add")
    public void add(User user){
        userService.add(user);
    }

    @GetMapping("/queryById")
    public User queryById(int id){
        return userService.queryById(id);
    }

}

3,记得在启动类中,开启扫描mapper类 

package com.entor;

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

@SpringBootApplication
@MapperScan(basePackages = "com.entor.mapper") //扫描mapper
public class SpringbootMybatisApplication {

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

}

 对于注解的可参考(3条消息) @getmapping注解的作用_【Spring】常用注解整理_weixin_39790877的博客-CSDN博客

4,可下载postman测试

post测试

 get测试

 

5,额外的,如果使用 注解@Controller //访问返回页面,添加thymeleaf依赖

   
            org.springframework.boot
            spring-boot-starter-thymeleaf
    

controller层

package com.entor.controller;

import com.entor.entity.User;
import com.entor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller //访问返回页面
public class UserController2 {

    @Autowired
    private UserService userService;

    @PostMapping("/add1")
    public void add1(User user){
        userService.add(user);
    }

    @GetMapping("/queryById1")
    //Model 对象负责在控制器和展现数据的视图之间传递数据。Model 就是个 key-value 集合。
    public String queryById1(int id, Model model){
        User user = userService.queryById(id);
        model.addAttribute("user",user);
        return "index";
    }
}

在templates包下建index.html文件




    
    修改用户信息


    
    







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

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

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