mybatis-配置文件
目录
标签顺序
configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers?)
环境
/*
Navicat Premium Data Transfer
Source Server : school
Source Server Type : MySQL
Source Server Version : 80022
Source Host : localhost:3306
Source Schema : school
Target Server Type : MySQL
Target Server Version : 80022
File Encoding : 65001
Date: 10/11/2021 15:02:04
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for grade
-- ----------------------------
DROP TABLE IF EXISTS `grade`;
CREATE TABLE `grade` (
`gradeId` int(0) NOT NULL AUTO_INCREMENT,
`gradeName` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`gradeId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of grade
-- ----------------------------
INSERT INTO `grade` VALUES (1, 'S1');
INSERT INTO `grade` VALUES (2, 'Y1');
SET FOREIGN_KEY_CHECKS = 1;
package com.fly.entity;
/**
* @author 26414
*/
public class Grade {
private Integer gradeId;
private String gradeName;
@Override
public String toString() {
return "Grade{" +
"gradeId=" + gradeId +
", gradeName='" + gradeName + '\'' +
'}';
}
public Integer getGradeId() {
return gradeId;
}
public void setGradeId(Integer gradeId) {
this.gradeId = gradeId;
}
public String getGradeName() {
return gradeName;
}
public void setGradeName(String gradeName) {
this.gradeName = gradeName;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--这里填sql映射文件的位置-->
<mapper resource="mapper/GradeMapper.xml"/>
</mappers>
</configuration>
package com.fly.dao;
import com.fly.entity.Grade;
/**
* @author 26414
*/
public interface GradeMapper {
/**
* 根据id查询年级
* @param gradeId 年级id
* @return gradeId对应的年级
*/
Grade getGradeById(Integer gradeId);
}
properties
在类路径下建立dbconfig.properties文件
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
db.username=root
db.password=123456
#这里不能有空格
测试
@Test
public void test2() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过反射得到代理对象
GradeMapper mapper = sqlSession.getMapper(GradeMapper.class);
Grade grade = mapper.getGradeById(1);
System.out.println("mapper = " + mapper);
System.out.println("grade = " + grade);
sqlSession.close();
}
由于mybatis和spring整合后,数据库的配置都交给spring管理,这个标签了解即可
settings
typeAliases
给单个Java 类型设置别名
<!-- mybatis-config.xml -->
<typeAliases>
<!--默认是类名首字母小写,不区分大小写-->
<typeAlias type="com.fly.entity.Grade"/>
</typeAliases>
测试
@Test
public void test2() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过反射得到代理对象
GradeMapper mapper = sqlSession.getMapper(GradeMapper.class);
Grade grade = mapper.getGradeById(1);
System.out.println("mapper = " + mapper);
System.out.println("grade = " + grade);
sqlSession.close();
}
也可以使用alias属性自定义别名
<!-- mybatis-config.xml -->
<typeAliases>
<typeAlias type="com.fly.entity.Grade" alias="gra"/>
</typeAliases>
运行test2方法测试
给整个包下的类设置别名
<!-- mybatis-config.xml -->
<typeAliases>
<!--不区分大小写-->
<package name="com.fly.entity"/>
</typeAliases>
映射文件不用改,运行test2
@Alias
假如entity包下面有Grade类,entity包的子包也有Grade类,给entity包起别名就会报错,可以用@Alias单独设置别名
已经有的别名
environments
transactionManager
dataSource
databaseIdProvider
mappers
class属性
<mappers>
<!--这里填sql映射文件的位置-->
<!--
resource:引用类路径下的映射文件
url:引用网络路径或者磁盘路径的文件
-->
<!-- <mapper resource="mapper/GradeMapper.xml"/>-->
<!--
接口和映射文件必须在同一个包下面,并且名字相同
-->
<mapper class="com.fly.dao.GradeMapper"/>
</mappers>
运行test2方法测试
或者将sql语句写在方法上面
package com.fly.dao;
import com.fly.entity.Grade;
import org.apache.ibatis.annotations.Select;
/**
* @author 26414
*/
public interface GradeMapper2 {
/**
* 根据id查询年级
* @param gradeId 年级id
* @return gradeId对应的年级
*/
@Select("select * from grade where gradeId = #{gradeId}")
Grade getGradeById(Integer gradeId);
}
<mappers>
<!--mybatis-config.xml-->
<mapper class="com.fly.dao.GradeMapper2"/>
</mappers>
测试
@Test
public void test3() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过反射得到代理对象
GradeMapper2 mapper = sqlSession.getMapper(GradeMapper2.class);
Grade grade = mapper.getGradeById(1);
System.out.println("mapper = " + mapper);
System.out.println("grade = " + grade);
sqlSession.close();
}