下面整理的是mybatis-spring连接mysql的一种方式,在开发时遇到的一些问题,作为笔记先记录下来。
- Dao的mapper文件和映射文件名称,一定要一致,否则在数据处理时,会提示绑定失败,找不到文件错误
- properties文件中的配置,其中都是可以源码属性信息对应的,会在启动时自动导入
- mapper 文件中需要加注解@Repository,否则在使用时会提示找不到相应的bean信息
- 单元测试时,需要加注解@SpringBootTest,否则虽然可以自动导入mapper变量,但是值为null
先总结这些,后面有问题再作记录
目前只是连接单个数据库,有些实际场景会有多个数据库连接操作,后面补充
项目的目录结构:
0、POM文件依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo04</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo04</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
</dependencies>
<build>
<!--资源文件管理-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
## 1、配置文件
配置文件中包括:数据连接信息和指定的映射文件
#数据库相关配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/test?useSSL=true&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=test
spring.datasource.password=123456
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
mybatis.mapper-locations=classpath*:com/example/dao/mapping/*.xml
2、DAO文件
UserInfoMapper.java
package com.example.dao.mapper;
import com.example.dao.pojo.UserInfo;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface UserInfoMapper {
Integer selectCount();
List<UserInfo> selectUserList();
}
UserInfoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.mapper.UserInfoMapper">
<select id="selectCount" resultType="java.lang.Integer">
select count(1) from userinfo;
</select>
<select id="selectUserList" resultType="com.example.dao.pojo.UserInfo">
select * from userinfo;
</select>
</mapper>
实体类UserInfo
package com.example.dao.pojo;
public class UserInfo {
private int id;
private String username;
private Integer gender;
private String interest;
public UserInfo(int id, String username, Integer gender, String interest) {
this.id = id;
this.username = username;
this.gender = gender;
this.interest = interest;
}
public String getInterest() {
return interest;
}
public void setInterest(String interest) {
this.interest = interest;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", gender=" + gender +
", interest='" + interest + '\'' +
'}';
}
}
3、单元测试
//由于是springboot项目,单元一定要记的添加该注解,否则在下面的自动注入时,变量为空
@SpringBootTest
class Demo04ApplicationTest {
@Autowired
private UserInfoMapper userInfoMapper;
//@Autowired
//private UserEventMapper userEventMapper;
@Test
public void test() {
Integer num = userInfoMapper.selectCount();
System.out.println(num);
List<UserInfo> userInfos = userInfoMapper.selectUserList();
System.out.println(userInfos);
//Integer eventCount = userEventMapper.selectCount();
//System.out.println(eventCount);
}
}
输出结果
2
[UserInfo{id=1, username='张三', gender=1, interest='学习'}, UserInfo{id=2, username='李四', gender=2, interest='旅游'}]
1
先到这里了,后面再加多数据连接操作!