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

Springboot整合mybatis试验-Date01

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

Springboot整合mybatis试验-Date01

Web02成功案例分析

Springboot整合mybatis试验,为员工管理系统配置数据库做准备

2021-10-27

使用IDEA完成
查看依赖


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.6
         
    
    com
    web02
    0.0.1-SNAPSHOT
    web02
    web02
    
        1.8
    
             
             
    
             //手动导入mybatis
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        
             //JDBC
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
             //Web
        
            org.springframework.boot
            spring-boot-starter-web
        
			//数据库MySql
        
            mysql
            mysql-connector-java
            runtime
        
             //Lombok
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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



自行建表user

代码结构

代码详情 UserController.java
package com.xie.controller;

import com.xie.mapper.UserMapper;
import com.xie.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/queryUserList")
    public List queryUserList(){
        List userList = userMapper.queryUserList();
        for (User user : userList){
            System.out.println(user);
        }
        return userList;
    }
    @GetMapping("addUser")
    public String addUser(){
        userMapper.addUser(new User(106,"amao","123456"));
        return "ok";
    }
    @GetMapping("updateUser")
    public String updateUser(){
        userMapper.updateUser(new User(106,"amao","000456"));
        return "ok";
    }
    @GetMapping("deleteUser")
    public String deleteUser(){
        userMapper.deleteUser(106);
        return "ok";
    }
}

UserMapper.java
package com.xie.mapper;

import com.xie.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    List queryUserList();

    User queryUserById(int id);
    int addUser (User user);
    int updateUser(User user);
    int deleteUser(int id);
}

User.java
package com.xie.pojo;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private int id;
    private String name;
    private String pwd;
}

Web02Application.java

保持默认,不可更改

UserMapper.xml






    
        select * from department where id = #{id};
    
    
        insert into user (id,name ,pwd) values (#{id},#{name},#{pwd})
    
    
        update user set name = #{name},pwd=#{pwd} where id = #{id}
    
    
        delete from user where id = #{id}
    

application.properties

(个人配置文件)

spring.datasource.username=root
spring.datasource.password=095810
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=com.xie.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
测试连接 Web02ApplicationTests.java
package com.xie;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@SpringBootTest
class Web02ApplicationTests {

    //DI注入数据源
    @Autowired
    DataSource dataSource;

    @Test
    public void contextLoads() throws SQLException {
        //看一下默认数据源
        System.out.println(dataSource.getClass());
        //获得连接
        Connection connection =   dataSource.getConnection();
        System.out.println(connection);
        //关闭连接
        connection.close();
    }
}

连接结果

输出默认数据源,且获得连接

CURD测试
localhost:8080/queryUserList

localhost:8080/addUser  #以下操作均返回“ok”

localhost:8080/updateUser

localhost:8080/deleteUser

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

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

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