玄子Share - Mybatis 项目模板使用指南
项目结构图
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yzjDtIQx-1691124207425)(./assets/image-20230804121336329.png)]](https://img-blog.csdnimg.cn/6230ba954a744e82849427091e99c977.png)
mybatis-config.xml 配置模板设置
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sDYPyGGV-1691124207426)(./assets/image-20230804121442262.png)]](https://img-blog.csdnimg.cn/dfdec93c3fbe4a88b29fb91b774d195e.png)
参数
<?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 数据库配置模板设置
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VzInjx9u-1691124207426)(./assets/image-20230804123027590.png)]](https://img-blog.csdnimg.cn/45a39d18291149f2b893369d9f841431.png)
参数
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 工具类模板设置
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VxLsjcQr-1691124207426)(./assets/image-20230804123209247.png)]](https://img-blog.csdnimg.cn/1a74a103703e4ec09d17b32e403fc367.png)
参数
#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 映射模板设置
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ChZdQEEp-1691124207427)(./assets/image-20230804123656375.png)]](https://img-blog.csdnimg.cn/78743f1ce6b5418d974edb65c05c02df.png)
参数
<?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


















![[openCV]基于赛道追踪的智能车巡线方案V1](https://img-blog.csdnimg.cn/e950a36d0a6b4542b379b74e5eed3575.png)
