目录
1. springboot整合mybatis
1.1 添加pom.xml依赖
1.2 新建jdbc.properties 文件添加以下内容
1.3 新建generatorConfig.xml 文件添加以下内容 (自动生成代码类)
1.4 修改application.properties 文件 添加以下内容
1.5 修改主类MapperScan
1.6 编写接口实现类进行测试
2. springboot整合mybatis-plus
2.1 添加pom.xml依赖
2.2 自动生成代码类
2.3 修改application.properties 文件 添加以下内容
2.4 修改主方法MapperScan
2.5 编写测试类开始测试
如果你想要复制我的代码来操作实验的话请看以下说明:
springboot整合mybatis主包名要为 com.jmh.ssm
springboot整合mybatis-plus主包名为 com.jmh.ssmmp
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
mysql
mysql-connector-java
5.1.44
true
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名称?useUnicode=true&characterEncoding=UTF-8
jdbc.username=数据库账号
jdbc.password=数据库密码
生成项目结构
#配置tomcat
server:
port: 8080
servlet:
context-path: /ssm
#配置jdbc
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/你的数据库名称?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: 数据库账号
password: 数据库密码
#配置mybatis
mybatis:
#配置SQL映射文件路径
mapper-locations: classpath:mapper/*.xml
#配置别名
type-aliases-package: com.jmh.ssm.model
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true
#配置日志输出
logging:
level:
com.jmh.ssm.mapper: debug
package com.jmh.ssm;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan({"com.jmh.ssm.mapper"})
@SpringBootApplication
public class SsmApplication {
public static void main(String[] args) {
SpringApplication.run(SsmApplication.class, args);
}
}
package com.jmh.ssm.service.impl;
import com.jmh.ssm.mapper.BookMapper;
import com.jmh.ssm.model.Book;
import com.jmh.ssm.service.IBookService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 蒋明辉
* @data 2022/9/28 17:06
*/
@Service
public class BookServiceImpl implements IBookService {
//注入
@Resource
private BookMapper bookMapper;
@Override
public int deleteByPrimaryKey(Integer id) {
return 0;
}
@Override
public int insert(Book record) {
return 0;
}
@Override
public int insertSelective(Book record) {
return 0;
}
@Override
public Book selectByPrimaryKey(Integer id) {
return bookMapper.selectByPrimaryKey(id);
}
@Override
public int updateByPrimaryKeySelective(Book record) {
return 0;
}
@Override
public int updateByPrimaryKey(Book record) {
return 0;
}
}
package com.jmh.ssm.service.impl;
import com.jmh.ssm.model.Book;
import com.jmh.ssm.service.IBookService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.List;
import static org.junit.Assert.*;
/**
* @author 蒋明辉
* @data 2022/9/28 17:08
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class BookServiceImplTest {
//注入
@Resource
private IBookService bookService;
@Test
public void selectByPrimaryKey() {
Book book = bookService.selectByPrimaryKey(9);
System.out.println(book);
}
@Test
public void listBook(){
List books =
bookService.listBook(null);
for (Book book : books) {
System.out.println(book);
}
}
}
com.baomidou
mybatis-plus-boot-starter
3.5.2
com.baomidou
mybatis-plus-generator
3.5.2
org.springframework.boot
spring-boot-starter-freemarker
2.7.4
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
package com.jmh.ssmmp.generator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.Collections;
/**
* @author 蒋明辉
* @data 2022/9/28 19:48
*/
public class MybatisPlusGenerator {
public static void main(String[] args) {
FastAutoGenerator.create(
"jdbc:mysql://localhost:3306/db_layui?useUnicode=true&characterEncoding=UTF-8&useSSL=false",
"root",
"1234")
.globalConfig(builder -> {
builder.author("jmh") // 设置作者
//.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("E:\\springbootProject\\ssm-mp\\src\\main\\java"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com.jmh.ssmmp") // 设置父包名
//.moduleName("system") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.xml, "E:\\springbootProject\\ssm-mp\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude("t_book_vue") // 设置需要生成的表名
.addTablePrefix("t_", "c_"); // 设置过滤表前缀
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}
项目结构
#配置tomcat
server:
port: 8080
servlet:
context-path: /ssm
#配置jdbc
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/你的数据库名称?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: 你的数据库账号
password: 你的数据库密码
#配置mybatis
mybatis-plus:
#配置SQL映射文件路径
mapper-locations: classpath:mapper/*.xml
#配置别名
type-aliases-package: com.jmh.ssmmp.model
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true
#配置日志输出
logging:
level:
com.jmh.ssmmp.mapper: debug
package com.jmh.ssmmp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan({"com.jmh.ssmmp.mapper"})
public class SsmMpApplication {
public static void main(String[] args) {
SpringApplication.run(SsmMpApplication.class, args);
}
}
package com.jmh.ssmmp.service.impl;
import com.jmh.ssmmp.entity.BookVue;
import com.jmh.ssmmp.service.IBookVueService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.awt.print.Book;
import java.util.List;
import static org.junit.Assert.*;
/**
* @author 蒋明辉
* @data 2022/9/28 20:22
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class BookVueServiceImplTest {
//注入
@Resource
private IBookVueService bookVueService;
@Test
public void list(){
List list = bookVueService.list();
for (BookVue vue : list) {
System.out.println(vue);
}
}
}