1、mybatis相关配置的详细说明
2、mybatis接口绑定形式:配置和注解
全局配置文件详解:
<!--properties标签:读取配置文件,一般配置数据源-->
<!--<properties>-->
<!--<property name="db.properties" value="db.properties"/>-->
<!--</properties>-->
<!--settings标签:设置mybatis全局性配置-->
<!--<settings>-->
<!--打开延时加载的开关-->
<!--<setting name="lazyLoadingEnabled" value="true"/>-->
<!--将积极加载改为消极加载即按需加载-->
<!--<setting name="aggressiveLazyLoading" value="false"/>-->
<!--开启二级缓存-->
<!--<setting name="cacheEnabled" value="true"/>-->
<!--</settings>-->
<!--typeAliases标签:取别名-->
<typeAliases>
<!--
typeAlias:指定单个的类型取别名
type属性:指定的要取别名的类的全路径名
alias属性:取别名
-->
<typeAlias type="com.bean7.Student" alias="student"/>
<!--
package标签:批量取别名
name属性:指定要取别名的包路径
-->
<!--<package name="com.bean7"/>-->
</typeAliases>
<!--环境配置-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<!--读取配置-->
<!--<property name="password" value="${passsword}"/>-->
</dataSource>
</environment>
</environments>
<!--对象映射-->
<mappers>
<!--
mappers映射有三种方式:
resource方式:
对单个的mapper进行映射、
mapper.xml和mapper.java文件名可以不一样,可以不用存放在同一个目录下
class方式:
对单个的mapper进行映射
mapper.xml和mapper.java文件名必须一样,必须存放在同一个目录下
package方式:
批量对mapper映射
mapper.xml和mapper.java文件名必须一样,必须存放在同一个目录下
-->
<mapper resource="mapper7/StudentMapper.xml"/>
<!--<mapper class="com.tulun.dao5.StudentMapper"/>-->
<!--<package name="com.dao7"/>-->
</mappers>
mapper.xml配置文件详解
<!--
resultMap标签:指定返回类型
id属性:取名字
type属性:指定映射的JAVA对象,对应类的全路径
-->
<resultMap id="resultStudent" type="com.bean7.Student">
<!--
id标签:指定主键的映射关系
result标签:非主键的映射关系
column属性:指定数据库字段名
jdbcType属性:指定数据库字段的类型
property属性:指定java类的属性名
javaType属性:指定JAVA属性的类型
typeHandler属性:执行类型映射操作
-->
<id column="SID" property="SID" javaType="java.lang.Integer" jdbcType="INTEGER"></id>
<result column="Sname" property="name"></result>
<result column="Ssex" property="Ssex"></result>
<result column="Sage" property="Sage"></result>
</resultMap>
<!--
select查询操作标签
id属性:对应接口中方法名,必填项、且在同一个namespace,ID不能重复
#{XXX}:占位符 里面填写的是方法中的参数名
parameterType属性:指定入参类型
resultType属性:执行返回参数类型 指定全路径名
resultMap属性:执行返回参数类型
resultType和resultMap区别:
使用resultType是需要数据库字段和java类属性名保持一致,如果不一致则该属性无法完成映射
使用resultMap进行显性映射,可以解决数据库字段和JAVA类属性名不一致的问题
-->
<select id="getStudentById" parameterType="java.lang.Integer" resultMap="resultStudent">
select * from Student where SID = #{sid}
</select>
<!--插入操作-->
<insert id ="addStudent" parameterType="com.bean7.Student">
insert into Student (SID,Sname) values (#{SID},#{Sname})
</insert>
<!--删除操作-->
<delete id=""/>
<!--修改操作-->
<update id=""/>
//创建sqlSession,设置事务自动提交
SqlSession sqlSession = sqlSessionFactory.openSession(true);
sqlSession.commit();
//mybatis 默认是手动提交事务
编码详解:
SqlSessionFactoryBuilder:
创建会话工厂sqlSessionFactory
SqlSessionFactory:会话工厂
通过读取配置文件创建、配置文件读取一次即可,可以将SqlSessionFactory设置单例
通过SqlSessionFactory创建会话
SqlSession:会话
对数据库的CRUD操作都是通过SqlSession来操作的
SqlSession是线程不安全,将其设置为方法的局部变量
一级缓存是基于SqlSession来创建的,mybatis是默认开启一级缓存
mybatis接口绑定形式:配置和注解
配置:
mapper.xml和mapper.java的约定
mapper.xml文件中的namaspace属性和mapper.java中接口的类全路径名保持一致
mapper.xml文件中的操作(CRUD)的id属性和mapper.java中接口的方法名保持一致
mapper.xml文件中的操作的parameterType属性和mapper.java接口中对应的方法的入参类型保持一致
mapper.xml文件中的操作的resultType属性和mapper.java接口中对应方法的返回类型保持一致
注解形式:
需求:通过ID查询Student表中用户信息
@Select(“select * from Student where SID = #{sid}”)
public Student getStudentById(int sid);
// 需求:查询名字为’zhaolei’,性别为’nan’的用户
@Results({
}
)
@Select(“select * from Student where Sname = #{Sname} and Ssex= #{Ssex}”)
public Student getStudentByNameAndSex(@Param(“Sname”) String Sname,@Param(“Ssex”) String Ssex);
/**
* 当方法参数只有一个时,不需要使用@Param注解
* 当方法参数有多个是,需要@Param注解来讲方法形参和@Param参数名绑定
*/