1.配置文件解析
#user_mapper.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">
<!-- namespace命名空间 防止sql语句的id重名
namespace 命名 包名+类名/包名+mapper文件名
parameterType 指sql语句参数类型
resultType 返回结果类型
useGeneratedKeys="true" 使用自增主键
-->
<mapper namespace="com.xgc.entity.UserMapper">
<!-- id在该文件中唯一 -->
<select id="selectUser" resultType="com.xgc.entity.User">
select * from student where id = #{id}
</select>
<select id="selectAll" resultType="com.xgc.entity.User">
select * from student
</select>
<insert id="addUser" parameterType="com.xgc.entity.User" useGeneratedKeys="true">
insert into student(name,age) values (#{name},#{age})
</insert>
<update id="updateUser" parameterType="com.xgc.entity.User">
update student set name=#{name},age=#{age} where id=#{id}
</update>
<delete id="deleteUser">
delete from student where id=#{id}
</delete>
</mapper>
复制代码
|
#mybatis-config.xml
<?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 指mybatis可以配置多个环境 default指向默认的环境
每个SqlSessionFactory对应一个环境environment
-->
<environments default="development">
<environment id="development">
<!--
JDBC - 这个配置直接使用JDBC的提交和回滚功能。它依赖于从数据源获得连接来管理
事务的生存周期。
MANAGED - 这个配置基本上什么都不做。它从不提交或者回滚一个连接的事务。而是让
容器(例如:Spring 或者 J2EE应用服务器) 来管理事务的生存周期。
-->
<transactionManager type="JDBC" />
<!--
数据源类型:
UNPOOLED - 这个类型的数据源实现只是在每次需要的时候简单地打开和关闭连接。
POOLED - 这个数据源的实现缓存了JDBC连接对象,用于避免每次创建新的数据库连接时都初始
化和进行认证,加快程序响应。并发Web应用通常使用这种做法来获得快速响应。
JNDI - 这个数据源的配置是为了准备与像Spring 或应用服务器能够在外部或内部配置数据
源的容器一起使用,然后在JNDI上下文中引用它。
-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 定义映射sql语句文件 -->
<mapper resource="com/xgc/entity/user_mapper.xml"/>
</mappers>
</configuration>
复制代码
|
#MyBatisUtil.java
package com.xgc.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
/**
* 通过配置文件 创建SqlSessionFactory 是一个SqlSession的工厂类
* @return
* @throws IOException
*/
public static SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory;
}
/**
* SqlSession 通过id 找到对应的sql语句,执行sql语句
* @return
* @throws IOException
*/
public static SqlSession getSqlSession() throws IOException {
SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
return sqlSessionFactory.openSession();
}
}
复制代码
2.执行流程
读取核心配置文件 --> sqlSessionFactory类 --> sqlSession --> (执行相关操作)
3.优化配置文件
#mybatis-config.xml
<?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>
<properties resource="db.properties"/>
<typeAliases>
<!-- 为指定类型指定 别名 使得在mapper映射文件中可以简化引用 -->
<typeAlias type="com.xgc.entity.User" alias="User"/>
<!-- 为某个包下的所有类指定别名 默认别名是对应的类名 -->
<package name="com.xgc.entity"/>
</typeAliases>
<!-- environments 指mybatis可以配置多个环境 default指向默认的环境
每个SqlSessionFactory对应一个环境environment
-->
<environments default="development">
<environment id="development">
<!--
JDBC - 这个配置直接使用JDBC的提交和回滚功能。它依赖于从数据源获得连接来管理
事务的生存周期。
MANAGED - 这个配置基本上什么都不做。它从不提交或者回滚一个连接的事务。而是让
容器(例如:Spring 或者 J2EE应用服务器) 来管理事务的生存周期。
-->
<transactionManager type="JDBC" />
<!--
数据源类型:
UNPOOLED - 这个类型的数据源实现只是在每次需要的时候简单地打开和关闭连接。
POOLED - 这个数据源的实现缓存了JDBC连接对象,用于避免每次创建新的数据库连接时都初始
化和进行认证,加快程序响应。并发Web应用通常使用这种做法来获得快速响应。
JNDI - 这个数据源的配置是为了准备与像Spring 或应用服务器能够在外部或内部配置数据
源的容器一起使用,然后在JNDI上下文中引用它。
-->
<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>
<!-- 定义映射sql语句文件 -->
<mapper resource="com/xgc/entity/user_mapper.xml"/>
</mappers>
</configuration>
复制代码
|
#db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT
username=root
password=123456
复制代码
db.properties文件所在目录