Idea 通过mybatis整合数据库资源大概过程

1.概要

  1. 在数据库创建数据表:表名不分大小写,可以用下划线;建表之前一定要心中有大概框架
  2. 通过maven创建SpringBoot工程:起好名字
  3. 配置pom.xml文件:添加依赖、插件等
  4. 配置application.properties文件:配置数据库
  5. 实体层:写实体类
  6. mapper:类对应的mapper文件和mapper.xml文件
  7. service
  8. controller
  9. 启动类:application

2. 配置application.properties文件

mybatis.mapper-locations=classpath:xx/xx/springboot/mapper/*.xml

spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8&useSSL=false

下载mysql connectorJ的jar包,放在JRE的lib文件里。
spring.datasource.url的后面按上述格式写主机IP地址的端口号,加上需要使用的数据库名。如果数据库的时区没有修改,运行程序时可能会出现时区不对的问题。再数据库名后面加上useUnicode=true&characterEncoding=utf8&useSSL=false即可。或者可以通过命令行手动修改数据库的时区(timezone)。

3.配置pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                            http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.learning.springboot</groupId>
    <artifactId>02-springboot--mybatis</artifactId>
    <version>1.0.0</version>
    
    <dependencies>
    
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
     
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.3</version>
        </dependency>
        
         </dependencies>
         
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <build>

        <resources>
        
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            
            <resource>
                <directory>src/main/java</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
            
        </resources>
        
    </build>
    
</project>

添加程序运行需要的插件和依赖

4.启动类:application

package com.learning.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
//@EnableTransactionManagement
public class Application {

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

@EnableTransactionManagement这个注解是事务管理的
注意:要保证application类在最外层,这样才能扫描到所有的文件。或者可以加上@MapScan()这个注解。

5.service接口类

StudentService.java

package com.learning.springboot.service;

import com.learning.springboot.mapper.StudentMapper;
import com.learning.springboot.model.Student;
//import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface StudentService {
//    public List<Student> getAllStudent();
//    public List<Student> getTwoTable();
    public List<Student> findStudentByCourseId();
//    @Transactional
//    int updateByPrimaryKeySelective();
//    int deleteByPrimaryKey();
//    int select();
//    int insert();
}

StudentServiceImpl.java

package com.learning.springboot.service.impl;


import com.learning.springboot.mapper.StudentMapper;
import com.learning.springboot.model.Student;
import com.learning.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

@Override
public List<Student> findStudentByCourseId() {
    return studentMapper.findStudentByCourseId();
}
}


6.MyBatisController.java

package com.learning.springboot.controller;

import com.learning.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyBatisController {

    @Autowired
    private StudentService studentService;
    
    @GetMapping("/boot/course")
    public Object Students() {
        return studentService.findStudentByCourseId();
    }
}

在程序中,加入了一个web,方便在浏览器直接看到数据库的查询结果或者报错。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值