掌握Mybatis核心配置;
Mybatis-config.xml 全局配置文件
Mybatis-config.xml配置文件包含了影响MyBatis 行为的设置和属性 信息,文档结构如下:
configuration 配置
properties 属性
settings 设置
typeAliases 类型命名
typeHandlers 类型处理器
objectFactory 对象工厂
plugins 插件
Environments 环境
mappers 映射器
properties全局参数 加载属性文件
<!--加载属性文件-->
<!-- 如果db.properties在文件夹properties内 ,路径可以写为pro读perties/db.properties-->
<properties resource="db.properties">
<!--下面配置的属性先被读取,然后读 resources指定的文件内容,
后边会覆盖前边的,也就是说数据库密码取123456-->
<!-- <property name="database.password" value="1234"/>-->
</properties>
MyBatis 将按照下面的顺序来加载属性:
在 properties 元素体内定义的属性首先被读取。
然后会读取properties 元素中resource或 url 加载的属性,它会覆盖 已读取的同名属性。
最后读取parameterType传递的属性,它会覆盖已读取的同名属性。
settings全局参数
Mybatis框架在运行时可以调整一些运行参数,比如:开启二级缓存、开 启延迟加载等
全局参数将会影响Mybatis的运行行为。
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
typeAliases全局参数
在mapper.xml中,定义的statement需要parameterType指定输入参数的 类型、需要resultType指定输出结果的映射类型。
如果在指定类型时输 入类型全路径,不方便进行开发,可以针对parameterType或resultType 指定的类型定义一些别名,方便开发。
<!--类型别名-->
<typeAliases>
<!--只能设置一个别名-->
<!--<typeAlias type="com.zx.domain.User" alias="u"/>-->
<!--所有包下类,类名首字母变小写-->
<package name="com.zx.domain"/>
</typeAliases>
typeHandlers全局参数
Mybatis中通过typeHandlers(类型处理器)完成jdbc类型和java类型的转换 。通常情况下,Mybatis提供的类型处理器满足日常需要,不需要自定义
environments全局参数
environments主要用于配置数据库相关,而且可以在里面配置多个 environment。
transactionManager
- MyBatis中有两种事务管理器类型
- JDBC:使用了JDBC的提交和回滚设置
- MANAGED(托管):而它让容器来管理事务的整个生命周期(比如spring、jee 应用服务器的上下文)
dataSource
- 用来配置基本的JDBC数据源连接信息
- 有三种内建的数据源类型UNPOOLED | POOLED | JNDI
和spring整合后 environments配置将废除
<!--可以配置多个环境 default:默认环境,默认连哪个-->
<environments default="mysql">
<!--配置mysql环境-->
<environment id="mysql">
<!--Mybatis 有两种事务管理类型:-->
<!--JDBC - 这个类型直接全部使用 JDBC 的提交和回滚功能。它依靠使用连
接的数据源来管理事务的作用域-->
<!--MANAGED - 这个类型什么不做 , 它从不提交 、 回滚和关闭连接 。
而是让窗口来管理事务的全部生命周期 。(比如说 Spring 或者
JAVAEE 服务器)-->
<transactionManager type="JDBC"></transactionManager>
<!--数据源类型有三种: UNPOOLED , POOLED , JNDI
UNPOOLED - 不用数据库连接池-->
<!--POOLED - 使用连接池,常用-->
<!--JNDI - 需要配置JNDI-->
<dataSource type="POOLED">
<property name="driver" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</dataSource>
</environment>
</environments>
db.properties
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false
database.username=root
database.password=123456
mappers全局参数
既然 MyBatis的行为已经由上述元素配置完了,我们现在就要定义SQL映 射语句了。
但是, 首先我们需要告诉MyBatis到哪里去找到这些语句。
这些语句简单告诉了MyBatis去哪里找映射文件。
其余的细节就是在每个 SQL映射文件中了,下面的部分我们来讨论SQL映射文件
<mappers>
<!-- <mapper resource="mapper/UserMapper.xml"></mapper>-->
<!--Invalid bound statement (not found): com.zx.dao.UserMapper.selectAll-->
<!-- <mapper class="com.zx.dao.UserMapper"/>-->
<package name="com.zx.dao"/>
</mappers>
mapper.xml映射文件
SQL映射文件的配置却非常简单对比JDBC 代码使用SQL映射文件配置 可以节省95%的代码量
需要配置的基本元素
- cache – 配置给定模式的缓存
- cache-ref – 从别的模式中引用一个缓存
- resultMap – 这是最复杂而却强大的一个元素了,它描述如何从结 果集中加载对象
- sql – 一个可以被其他语句复用的SQL 块
- insert – 映射INSERT 语句
- update – 映射UPDATE 语句
- delete – 映射DELEETE 语句
- select - 映射SELECT语句
<?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">
<!--测试类用user.调用下边的语句id-->
<!--必须写接口的完全限定名-->
<mapper namespace="com.zx.dao.UserMapper">
<select id="selectByNamePassword" parameterType="user" resultType="user">
select * from user
<where>
<if test="name!=null and name!=''">
and name like concat('%',#{name},'%')
</if>
<if test="password!=null and password!=''">
and password=#{password}
</if>
</where>
</select>
<insert id="insertSelective" parameterType="user">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id!=null">
id,
</if>
<if test="name!=null ">
name,
</if>
<if test="password!=null">
password,
</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id!=null">
#{id},
</if>
<if test="name!=null ">
#{name},
</if>
<if test="password!=null">
#{password},
</if>
</trim>
</insert>
<!--parameterType应该是com.zx.domain.User,但是在配置文件设置了别名可以用user-->
<insert id="insert" parameterType="user" keyProperty="id" useGeneratedKeys="true">
insert into user values(#{id},#{name},#{password})
</insert>
<!--集合中成员类型-->
<select id="selectAll" resultType="user">
select * from user
</select>
<update id="updateNameById" parameterType="user">
update user set name=#{name} where id= #{id}
</update>
<delete id="delete" parameterType="int">
delete from user where id=#{id}
</delete>
<select id="findByName" parameterType="String" resultType="user">
select * from user where name=#{name}
</select>
<!--封装类的别名int java int 的别名_int-->
<select id="findById" parameterType="int" resultType="user">
select * from user where id=#{id}
</select>
</mapper>
namespace区别不同的配置文件
掌握Mybatis核心接口和类;
(1)每个MyBatis的应用程序都以一个 SqlSessionFactory对象的实例为核心;
(2)首先获取SqlSessionFactoryBuilder对 象,可以根据XML配置文件或Configuration 类的实例构建该对象;
(3)然后获取SqlSessionFactory对象,该 对象实例可以通过 SqlSessionFactoryBuilder对象来获得。
(4)有了SqlSessionFactory对象之后,就 可以进而获取SqlSession实例,SqlSession 对象中完全包含以数据库为背景的所有执行 SQL操作的方法。可以用该实例直接执行已映 射的SQL语句
SqlSessionFactory
SqlSessionFactory的作用就是创建SqlSession实例的工厂。
通过SqlSessionFactory提供的openSession()方法来获取SqlSession 实例。
SqlSessionFactory一旦被创建,在应用执行期间都存在。
SqlSession
SqlSession是一个面向用户(程序员)的接口。
它提供了面向数据库 执行SQL命令所需的所有方法。
SqlSession对应着一次数据库会话, SqlSession是线程不安全的
掌握Mybatis原始dao开发方法;
原始dao开发方法 程序员需要写dao接口和dao实现类。
1、程序员需要写dao接口和dao实现类。
2、需要向dao实现类中注入SqlSessionFactory,在方法体内通过 SqlSessionFactory创建SqlSession
掌握Mybatis mapper代理开发方法
程序员只需要mapper接口和mapper.xml映射文件,Mybatis可以自动生成 mapper接口实现类代理对象
mapper代理开发规范
1、在mapper.xml中namespace等于mapper接口地址
<!--必须写接口的完全限定名-->
<mapper namespace="com.zx.dao.UserMapper">
2、mapper.java接口中的方法名和mapper.xml中statement的id一致
3、mapper.java接口中的方法输入参数类型和mapper.xml中statement的 parameterType指定的类型一致。
4、mapper.java接口中的方法返回值类型和mapper.xml中statement的 resultType指定的类型一致。
5、如果使用批量配置,接口类和配置文件在同包下并且同名
UserMapper.java
public interface UserMapper {
int insert(User user);
}
UserMapper.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">
<!--测试类用user.调用下边的语句id-->
<!--必须写接口的完全限定名-->
<mapper namespace="com.zx.dao.UserMapper">
<!--parameterType应该是com.zx.domain.User,但是在配置文件设置了别名可以用user-->
<insert id="insert" parameterType="user" keyProperty="id" useGeneratedKeys="true">
insert into user values(#{id},#{name},#{password})
</insert>
</mapper>
PS
需要做异常处理(try…catch或者throws)
类找不到异常(类名错误,别名错)
数据库密码错误
创建maven父工程过程(为了让其他工程继承该父工程中的配置)
1)创建maven工程,不选择模板
2)pom.xml文件中添加以下代码:
大多数工程需要的依赖
如果出现错误,在maven中重新配置
填写maven安装目录:
确定后刷新。
3)maven中的install
4)到本地仓库查看是否成功
其他工程如何继承父工程?
在pom.xml文件中加入:
配置log4j
src/main/resources/log4j.properties
log4j.rootLogger = debug,stdout
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.appender.stdout.Encoding=UTF-8
src/main/resources/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>
<settings>
<!-- 启用延迟加载
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>-->
<!-- 开启二级缓存-->
<!--<setting name="cacheEnabled" value="true"/>-->
<setting name="logImpl" value="LOG4J"/>
</settings>
</configuration>
pom.xml
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>