新建我的项目
临时只须要勾选webjar包即可
导入jt.sql的数据库
筹备数据库工具~~~~
连贯数据库
增加jar包文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jt</groupId>
<artifactId>springboot_demo2_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_demo2_mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入插件lombok 主动的set/get/构造方法插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--引入数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--springBoot数据库连贯 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--spring整合mybatis 临时 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编辑POJO对象
@Data
@Accessors(chain = true)
public class User implements Serializable {
private Integer Id;
private String name;
private Integer age;
private String sex;
}
编辑UserDao接口
@Mapper //将接口交给Spring治理
public interface UserDao {
//查问user表的所有的记录
@Select("select * from user")
List<User> findAll();
}
编辑测试类
package com.jt;
import com.jt.dao.UserDao;
import com.jt.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringbootDemo2MybatisApplicationTests {
@Autowired
private UserDao userDao;
@Test
public void testFind(){
List<User> userList = userDao.findAll();
System.out.println(userList);
}
}
对于Mybatis配置阐明
前提:Mybatis进行映射时必须满足属性名称统一
业务阐明:
表: user_id user_name
对象: userId userName
解决方案: 开启mybatis驼峰规定映射
原理:字段名称user_id去除‘——’下滑线userid而后首字母大写userId 属性userId只有名称统一,则能够主动映射.
注意事项: 如果开启驼峰规定,则必须满足要求.
问: 对象的属性user_id 与字段user_id是否映射? 不能映射!
server:
port: 8090 #定义端口号
servlet:
context-path: / #定义我的项目公布门路
spring:
datasource:
#driver-class-name: com.mysql.jdbc.Driver 通知springboot程序采纳默认的配置
url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
mybatis:
#定义别名包
type-aliases-package: com.jt.pojo
#加载user表的mapper文件
mapper-locations: classpath:/mybatis/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
对于数据库url阐明
1.时区设定
serverTimezone=GMT%2B8 %2B= +号
2.编码格局设定
useUnicode=true&characterEncoding=utf8
3.是否主动的从新链接
autoReconnect=true
4.是否容许批量操作
allowMultiQueries=true
对于Mapper注解优化阐明
因为每个接口都须要增加Mapper注解导致代码繁琐.能够采纳包扫描的形式动静导入 代码如下
@SpringBootApplication
/**
*次要通知mapper的包门路,会主动的实现包扫描。
*留神:~~~~这里的参数是要扫描的包门路~~~~
*/
@MapperScan("com.jt.dao")
public class SpringbootDemo2MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemo2MybatisApplication.class, args);
}
}
发表回复