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

SpringBoot整合mybatis连接mysql实现CRUD

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

SpringBoot整合mybatis连接mysql实现CRUD

1.创建springBoot项目

目录结构如下,从第二步开始做起,不要直接全创了

2.导入pom依赖

创建项目的时候导入Web,Log4j,JDBC,Mysql驱动,等依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.springboot_mybatis
    springboot_mybatis
    0.0.1-SNAPSHOT
    springboot_mybatis
    Demo project for Spring Boot
    
        1.8
    
    
        
        
            com.alibaba
            druid
            1.1.21
        
        
        
        
            log4j
            log4j
            1.2.17
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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



3.resources下创建application.properties
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#ÕûºÏmybatis
mybatis.type-aliases-package=com.springboot_mybatis.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
4.在test中测试能否连接成功
@SpringBootTest
class SpringbootMybatisApplicationTests {

    @Autowired
    DataSource dataSource;

    @Test
    void contextLoads() throws SQLException {
        System.out.println(dataSource.getClass());
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
}
5.测试成功后,创建实体类User和Usermapper接口


User.class

@Component
public class User {
    private Integer id;
    private String username;
    private String password;

    public User() {
    }

    public User(Integer id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + ''' +
                ", password='" + password + ''' +
                '}';
    }
}

接口UserMapepr.class

@Mapper
@Repository
public interface UserMapper {

    //获取所有员工的信息
    List queryUserList();

    //通过id查询一个员工
    User queryUserById(int id);

    //新增一个员工
    int addUser(User user);

    //修改一个员工
    int updateUser(User user);

    //通过id删除员工信息
    int deleteUser(int id);

}

6.UserMapper接口的.xml文件

resource/mapper/UserMapper.xml







    
        select * from user where id=#{id};
    

    
        insert into user(username,password) values (#{username},#{password});
    

    
        update user set username=#{username},password=#{password} where id=#{id};
    

    
        delete from user where id =#{id};
    


7.我这里直接创建Controller,可以先创建一个Service进行封装,然后用Controller去调

controller/UserController.class

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/queryUserList")
    public List queryUserList() {
        List userList = userMapper.queryUserList();
        return userList;
    }

    @RequestMapping("/queryUserById/{id}")
    public User queryUserById(@PathVariable("id") Integer id) {
        User user = userMapper.queryUserById(id);
        return user;
    }

    @RequestMapping("/addUser")
    public String addUser() {
        User user = new User();
        user.setUsername("五毛");
        user.setPassword("5555555");
        userMapper.addUser(user);
        return "add_ok";
    }

    @RequestMapping("/updateUser")
    public String updateUser() {
        User user = new User(8, "五毛", "6666");
        userMapper.updateUser(user);
        return "update_ok";
    }

    @RequestMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") Integer id) {
        userMapper.deleteUser(id);
        return "delete_ok";
    }
}

8.启动主启动类进行测试,访问http://localhost:8080/+路径


StudentApplication.class

@SpringBootApplication
public class StudentApplication {

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

}

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

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

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