1.环境准备:
创建conf.xml文件和Mapper.xml文件 创建一个实体类与数据库中的表对应 创建Converter类继承BaseTypeHandler类
分析conf.xml文件:
1.设置别名:
2.配置一个数据库信息文件db.properties 关联此文件 在conf.xml文件中的标签下设置这一行
3.设置数据库环境:通过environments的default值和environment的id 来指定MyBatis运行时的数据库环境 ,,, environments中可有多个environment
-
<transactionManager type="JDBC"/> JDBC:利用JDBC方式处理事务 MANAGED: 将事物交于其他组件去托管(spring,jobss) , 默认会关闭连接. // 设置默认不关闭连接 <transactionManager type="MANAGED"/> <property name="closeConnection" value="false"/>
-
<dataSource type="POOLED"> <!-- 数据源类型: --> <!-- UNPOOLED: 传统的JDBC模式(每次访问数据库均需要打开关闭数据库 , 但是每次打开关闭数据库很费性能 , 因此不推荐使用) JNDI: 从tomcat中获取内置的数据库连接池(数据库连接池-数据源) POOLED: 使用数据库连接池 -->
6.根据db.properties 此文件配置信息配置
7.
(mapper.xml文件的路径)
- 配置数据库与java的数据类型
分析mapper.xml文件: 首先要设置一个mapper的接口.
1.设置namespase
<mapper namespace="mapper.StudentMapper">
parameterType:输入参数的类型
resultType: 查询返回结果值的类型 , 返回类型
<select id="queryStudentByStuno" parameterType="int" resultType="student">
select * from student where stuno = #{stuno}
</select>
单个参数#{可任意}
多个参数时#{必须和实体类中的属性名称一致}
2.接口中设置:
*1.方法名和mapper.xml文件中的标签的id值相同
* 2.方法的输入参数和mapper.xml文件中标签的parameterType类型一致
* 3.方法的返回值 和mapper.xml文件中的resultType类型一致
//查询一个
Student queryStudentByStuno(int stuno);
3.如果使用转换器
创建Converter类继承BaseTypeHandler类实现其方法
*ps:PreparedStatement对象
* i:PreparedStatement对象操作的参数的位置
* parameter:java值
* jdbcType:jdbc操作的数据库类型
resultType变成resultMap , 下增加resultMap标签 , , resultMap标签的id值和resultMap属性中的值一致搭建桥梁
resultMap标签中的type值为返回值类型 , , 主键用id 非主键用result
<!-- 查询:使用了类型转换器
1.如果类中的属性和表中的字段类型能够合理识别(String->varchara()),则可以使用resultType;否则(boolean -> int)使用resultMap
2.如果 类中的属性名 和表中的字段名能够合理识别(stuNo - stuno)则可以使用resultType;否则(id -> stuno)使用resultMap
-->
<select id="queryStudentByStunoWithConverter" parameterType="int" resultMap="studentResult">
select * from student where stuno = #{stuno}
</select>
<resultMap type="student" id="studentResult">
<!-- 分主键id 和 非主键result -->
<id property="stuNo" column="stuno" />
<result property="stuName" column="stuname" />
<result property="stuAge" column="stuage" />
<result property="graName" column="graname" />
<result property="stuSex" column="stusex" javaType="Boolean" jdbcType="INTEGER" />
</resultMap>
完整的cong.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"></properties>
<!--
<settings>
<setting name="cacheEnabled" value="false"/>
<setting name="lazyloadingEnabled" value="false"/>
</settings>
-->
<!-- 设置单个/多个别名 -->
<typeAliases>
<!-- 设置单个别名(别名 忽略大小写)
<typeAlias type="entity.Student" alias="student" />
-->
<!-- 批量定义别名(别名忽略大小写) , 以下会自动将该包中的所有类 批量定义别名 : 别名就是类名(不带包名 , 忽略大小写) -->
<package name="entity"/>
</typeAliases>
<typeHandlers>
<typeHandler handler="converter.BooleanAndIntConverter" javaType="Boolean" jdbcType="INTEGER" />
</typeHandlers>
<!-- 通过environments的default值和environment的id 来指定MyBatis运行时的数据库环境 -->
<environments default="development">
<!-- 开发环境(自己的计算机) -->
<environment id="development">
<!--
JDBC:利用JDBC方式处理事务
MANAGED: 将事物交于其他组件去托管(spring,jobss) , 默认会关闭连接.
// 设置默认不关闭连接
<transactionManager type="MANAGED"/>
<property name="closeConnection" value="false"/>
-->
<transactionManager type="JDBC"/>
<!-- 数据源类型: -->
<!--
UNPOOLED: 传统的JDBC模式(每次访问数据库均需要打开关闭数据库 , 但是每次打开关闭数据库很费性能 , 因此不推荐使用)
JNDI: 从tomcat中获取内置的数据库连接池(数据库连接池-数据源)
POOLED: 使用数据库连接池
-->
<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>
<!-- 真正的项目应该在发布的那台计算机上面运行 -->
<environment id="tomcat">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 配置数据库信息 -->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://59.110.237.22:3306/test?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 配置映射文件 -->
<mapper resource="mapper/studentMapper.xml"/>
</mappers>
</configuration>
最简单的conf.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 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="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
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:该mapper.xml映射文件的唯一标识 -->
<mapper namespace="mapper.StudentMapper">
<!-- 通过namespace.id确定select -->
<!-- parameterType:输入参数的类型
resultType: 查询返回结果值的类型 , 返回类型
-->
<!-- 查询单个学生 -->
<select id="queryStudentByStuno" parameterType="int" resultType="student">
select * from student where stuno = #{stuno}
</select>
<!-- 查询:使用了类型转换器
1.如果类中的属性和表中的字段类型能够合理识别(String->varchara()),则可以使用resultType;否则(boolean -> int)使用resultMap
2.如果 类中的属性名 和表中的字段名能够合理识别(stuNo - stuno)则可以使用resultType;否则(id -> stuno)使用resultMap
-->
<select id="queryStudentByStunoWithConverter" parameterType="int" resultMap="studentResult">
select * from student where stuno = #{stuno}
</select>
<resultMap type="student" id="studentResult">
<!-- 分主键id 和 非主键result -->
<id property="stuNo" column="stuno" />
<result property="stuName" column="stuname" />
<result property="stuAge" column="stuage" />
<result property="graName" column="graname" />
<result property="stuSex" column="stusex" javaType="Boolean" jdbcType="INTEGER" />
</resultMap>
<!-- 带转换器的增加学生 -->
<insert id="addStudentWithCoverter" parameterType="student" >
insert into student(stuno , stuname , stuage , graname , stusex) values(#{stuNo} , #{stuName} , #{stuAge} , #{graName} , #{stuSex , javaType=boolean , jdbcType=INTEGER })
</insert>
<!-- 增加学生 -->
<insert id="addStudent" parameterType="student" >
insert into student(stuno , stuname , stuage , graname) values(#{stuNo} , #{stuName} , #{stuAge} , #{graName})
</insert>
<!-- 修改学生 -->
<update id="updateStudentByStuno" parameterType="student" >
update student set stuname = #{stuName} , stuage = #{stuAge} , graname = #{graName} where stuno = #{stuNo}
</update>
<!-- 删除学生 -->
<delete id="deleteStudentByStuno" parameterType="int">
delete from student where stuno = #{stuno}
</delete>
<!-- 查询全部学生 -->
<select id="queryAllStudents" resultType="student">
select * from student
</select>
</mapper>
最简单的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">
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
db.properties文件
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/test?serverTimezone=UTC
username = root
password = 123456