Mybatis(首先导包)
1. 写mybatis.xml和JDBC.properties的配置文件:
<!--文件头名,规范该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、setting等-->
<configuration>
<!--加载properties的配置文件进来 -->
<properties resource="jdbc.properties"></properties>
<!--开启日志,将日志导入 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!--给实体类配置别名,方便mapper.xml中resultType省去包名 -->
<typeAliases>
<!--单独的给某一个文件起别名 -->
<typeAlias type="com.xxx.包名.类名" alias="别名"/>
<!--给整个包下面的文件起别名 这时候别名的名称就是类的名称 不区分大小写-->
<package name="com.xxx.类包"/>
</typeAliases>
<!--加载数据库环境 -->
<environments default="mysql">
<environment id="mysql">
<!--事务管理方式:JDBC -->
<transactionManager type="JDBC"></transactionManager>
<!--数据库连接池:POOLED -->
<dataSource type="POOLED">
<!--标签内的${}为替换符,替换properties配置文件中的属性 -->
<property name="driver" value="${m_driver}"></property>
<property name="url" value="${m_url}"></property>
<property name="username" value="${m_uname}"></property>
<property name="password" value="${m_pwd}"></property>
</dataSource>
</environment>
</environments>
<!--扫描mapper文件(必须要扫描,不然不能用) -->
<mappers>
<!--<mapper resource="com/xxx/mapper/FlowerMapper1.xml"></mapper>
<mapper resource="com/xxx/mapper/FlowerMapper2.xml"></mapper>
<mapper resource="com/xxx/mapper/FlowerMapper3.xml"></mapper>
<mapper resource="com/xxx/mapper/FlowerMapper.xml"></mapper>-->
<!--首先会找mapper包下的所有的[接口],然后去找和接口的名字相同的XML进行扫描-->
<package name="com.xxx.mapper"></package>
</mappers>
</configuration>
2.代理mapper
写接口mapper(存放要用的接口方法)和mapper.xml(标签中的namespace名为接口的包名(xml与接口关系绑定)方法标签、等标签中的id=接口中方法名)
3.动态SQL
mapper.xml中方法标签中使用 if where when set trim foreach bind include sql标签来根据实际情况动态编写sql语句。
重要掌握:< if >、< where >、< set >、< foreach >、< bind >