1.概要
- 在数据库创建数据表:表名不分大小写,可以用下划线;建表之前一定要心中有大概框架
- 通过maven创建SpringBoot工程:起好名字
- 配置pom.xml文件:添加依赖、插件等
- 配置application.properties文件:配置数据库
- 实体层:写实体类
- mapper:类对应的mapper文件和mapper.xml文件
- service
- controller
- 启动类: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,方便在浏览器直接看到数据库的查询结果或者报错。