玄子Share - Mybatis 项目模板使用指南
项目结构图
mybatis-config.xml 配置模板设置
参数
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="database.properties"/>
<settings>
<!-- <setting name="logImpl" value="LOG4J2"/>-->
<!-- <setting name="cacheEnabled" value="true"/>-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<!-- <typeAlias type="com.xuanzi.mybatis.entity.Student" alias="Student"/>-->
<!-- 等同于实体类上添加 @Alias("别名")-->
<package name="${PACKAGE_NAME}.entity"/>
<!--开启驼峰命名转换-->
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${@driver}"/>
<property name="url" value="${@url}"/>
<property name="username" value="${@username}"/>
<property name="password" value="${@password}"/>
<!--将此处的 @ 删掉-->
</dataSource>
</environment>
</environments>
<mappers>
<!-- <mapper resource="com/xuanzi/mybatis/dao/StudentMapper.xml"/>-->
<!-- <mapper class="com.xuanzi.mybatis.dao.StudentMapper"/>-->
<!-- 类名与XML文件名须一致-->
<!-- <mapper url="file:///com/xuanzi/mybatis/dao/StudentMapper.xml"/>-->
<package name="${PACKAGE_NAME}.dao"/>
</mappers>
</configuration>
<!--<build>-->
<!--<resources>-->
<!-- <resource>-->
<!-- <directory>src/main/java</directory>-->
<!-- <includes>-->
<!-- <include>**/*.xml</include>-->
<!-- <include>**/*.properties</include>-->
<!-- </includes>-->
<!-- </resource>-->
<!-- <resource>-->
<!-- <directory>src/main/resources</directory>-->
<!-- <includes>-->
<!-- <include>**/*.xml</include>-->
<!-- <include>**/*.properties</include>-->
<!-- </includes>-->
<!-- </resource>-->
<!--</resources>-->
<!--</build>-->
<!--将此内容添加至 pom.xml -->
需求
${PACKAGE_NAME}.entity
${PACKAGE_NAME}.dao
需要用到项目路径,所以需在项目内创建完成后再剪切到resources
文件夹${@password}
需要将此处的@
符号删除掉,使用Ctrl + F
查找替换@
database.properties 数据库配置模板设置
参数
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/myschool?useUnicode=TRUE&characterEncoding=UTF-8&serverTimezone=GMT-8
username=root
password=root
需求
- 将连接数据库的
myschool
改为自己的数据库
MybatisMapper.java 工具类模板设置
参数
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MybatisMapper {
private static final SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession(true);
// true 为自动提交事务
}
}
需求
- 注意
mybatis-config.xml
配置文件名称是否一致 openSession(true);
已开启自动提交事务
EntityMapper.xml XML 映射模板设置
参数
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${PACKAGE_NAME}.${NAME}">
</mapper>
需求
- 注意文件命名规范
运行 Main
public static void main(String[] args) {
SqlSession sqlSession = MybatisMapper.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.selectStudent();
studentList.forEach(System.out::println);
}
玄子Share - Mybatis 项目模板使用指南 8.4